Last active
November 17, 2016 01:49
-
-
Save csexton/5cfa0823674e53af00d116b5dfeeabdb to your computer and use it in GitHub Desktop.
Migrate url to template_data hstore
This file contains hidden or 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
class AddTemplateDataToContents < ActiveRecord::Migration[5.0] | |
def up | |
add_column :contents, :template_data, :hstore, default: '', null: false | |
execute <<~END | |
UPDATE contents | |
SET template_data = hstore('url', subquery.url) | |
FROM (SELECT id, url FROM contents) as subquery | |
WHERE contents.id=subquery.id; | |
END | |
remove_column :contents, :url, :string | |
end | |
def down | |
add_column :contents, :url, :string | |
execute <<~END | |
UPDATE contents | |
SET url = subquery.turl FROM (SELECT id, template_data->'url' as turl FROM contents) as subquery | |
WHERE contents.id=subquery.id; | |
END | |
remove_column :contents, :template_data | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment