Skip to content

Instantly share code, notes, and snippets.

View izikaj's full-sized avatar

Andrii izikaj

  • Full Stack Engineer at Railsware
View GitHub Profile
@izikaj
izikaj / migrate_hstore_to_json.rb
Last active January 19, 2022 08:02 — forked from estum/migrate_hstore_to_json.rb
Ruby on Rails & Postgres: Reversible migrate hstore column to jsonb with contents
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" = jsonb_object(hstore_to_matrix("data_hstore"))'
remove_column :posts, :data_hstore
end
def down
rename_column :posts, :data, :data_jsonb