Skip to content

Instantly share code, notes, and snippets.

@csexton
Last active November 17, 2016 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csexton/5cfa0823674e53af00d116b5dfeeabdb to your computer and use it in GitHub Desktop.
Save csexton/5cfa0823674e53af00d116b5dfeeabdb to your computer and use it in GitHub Desktop.
Migrate url to template_data hstore
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