Skip to content

Instantly share code, notes, and snippets.

@rposborne
Created April 24, 2016 17:03
Show Gist options
  • Save rposborne/9e6eacdb545741185f754663c7c3e172 to your computer and use it in GitHub Desktop.
Save rposborne/9e6eacdb545741185f754663c7c3e172 to your computer and use it in GitHub Desktop.
Convert JSON to JSONB
# frozen_string_literal: true
class ConvertJsonFieldsToJsonB < ActiveRecord::Migration
def change
reversible do |dir|
dir.up do
migrate_json_to_jsonb :table, :attribute
end
dir.down do
migrate_jsonb_to_json :table, :attribute
end
end
end
def migrate_json_to_jsonb(table, attribute)
change_column table, attribute, "jsonb USING #{attribute}::JSONB", default: {}, null: false
end
def migrate_jsonb_to_json(table, attribute)
change_column table, attribute, "json USING #{attribute}::JSON'", default: {}, null: false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment