Created
April 24, 2016 17:03
-
-
Save rposborne/9e6eacdb545741185f754663c7c3e172 to your computer and use it in GitHub Desktop.
Convert JSON to JSONB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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