Skip to content

Instantly share code, notes, and snippets.

@estum
Last active October 11, 2022 12:29
Show Gist options
  • Save estum/7a07fc82b34dbe2f0cc6 to your computer and use it in GitHub Desktop.
Save estum/7a07fc82b34dbe2f0cc6 to your computer and use it in GitHub Desktop.

Revisions

  1. estum revised this gist Jan 23, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions migrate_hstore_to_json.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    class MigrateHstoreToJson < ActiveRecord::Migration
    def up
    rename_column :posts, :data, :data_hstore
    add_column :posts, :data, :jsonb, default: {}, null: false, index: {using: 'gin'}
    execute 'UPDATE "posts" SET "data" = json_object(hstore_to_matrix("data_hstore"))'
    add_column :posts, :data, :jsonb, default: {}, null: false, index: { using: 'gin' }
    execute 'UPDATE "posts" SET "data" = json_object(hstore_to_matrix("data_hstore"))::jsonb'
    remove_column :posts, :data_hstore
    end

  2. estum created this gist Jan 21, 2015.
    15 changes: 15 additions & 0 deletions migrate_hstore_to_json.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    class MigrateHstoreToJson < ActiveRecord::Migration
    def up
    rename_column :posts, :data, :data_hstore
    add_column :posts, :data, :jsonb, default: {}, null: false, index: {using: 'gin'}
    execute 'UPDATE "posts" SET "data" = json_object(hstore_to_matrix("data_hstore"))'
    remove_column :posts, :data_hstore
    end

    def down
    rename_column :posts, :data, :data_jsonb
    add_column :posts, :data, :hstore, default: {}, null: false
    execute 'UPDATE "posts" SET "data" = (SELECT hstore(key, value) FROM jsonb_each_text("data_jsonb"))'
    remove_column :posts, :data_jsonb
    end
    end