Skip to content

Instantly share code, notes, and snippets.

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 emptyflask/4a5da8948a87c916fe8ec52d2aa59bd9 to your computer and use it in GitHub Desktop.
Save emptyflask/4a5da8948a87c916fe8ec52d2aa59bd9 to your computer and use it in GitHub Desktop.
DB view migration wrapper using Scenic
require 'migration/rebuild'
class ConvertFilmIdsToStrings < ActiveRecord::Migration[5.1]
include Migration::Rebuild
EVENT_VIEWS = {
# view: version
eventbase_events: 26,
eventbase_entities: 34,
admin_entity_stats: 8,
facebook_events: 5
}
def up
rebuild(EVENT_VIEWS) do
remove_foreign_key :screenings, :films
change_column :films, :id, :string
change_column :screenings, :film_id, :string
add_foreign_key :screenings, :films
end
end
def down
rebuild(EVENT_VIEWS) do
remove_foreign_key :screenings, :films
change_column :films, :id, :integer, using: 'id::integer'
change_column :screenings, :film_id, :integer, using: 'id::integer'
add_foreign_key :screenings, :films
end
end
end
module Migration
module Rebuild
def rebuild(views=[])
views.each {|view, version| drop_view view}
yield
views.each {|view, version| create_view view, version: version}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment