cirandas.net

ref: master

db/migrate/20170505164823_move_from_hstore_to_jsonb.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class MoveFromHstoreToJsonb < ActiveRecord::Migration
  def up
    %w[profiles articles tasks blocks users].each do |table|
      connection.execute "ALTER TABLE #{table} ALTER COLUMN metadata SET DEFAULT null"
      connection.execute "DROP INDEX index_#{table}_on_metadata"

      connection.execute "ALTER TABLE #{table} ALTER COLUMN metadata TYPE JSONB USING CAST(metadata as JSONB)"
      connection.execute "ALTER TABLE #{table} ALTER COLUMN metadata SET DEFAULT '{}'"
      add_index table, :metadata, using: :gin
    end
  end
end