Skip to content

Instantly share code, notes, and snippets.

@lee
Created October 5, 2011 12:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lee/1264336 to your computer and use it in GitHub Desktop.
Save lee/1264336 to your computer and use it in GitHub Desktop.
One way to use Rails Migrations for things other than db:migrate
# This stuff is from a Rails 2 app, but I don't think *too* much has changed in 3.0
class MaintenanceMigrator < ActiveRecord::Migrator
def self.schema_migrations_table_name
"#{super}_maintenance"
end
end
# To Run
MaintenanceMigrator.migrate("db/maintenance/")
MaintenanceMigrator.migrate("db/maintenance/", '20111001173645')
MaintenanceMigrator.run(:up, "db/maintenance/")
MaintenanceMigrator.run(:down, "db/maintenance/", '20110501173645')
# And a generator
class MaintenanceGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
m.migration_template 'migration.rb', 'db/maintenance', :assigns => get_local_assigns
end
end
private
def get_local_assigns
returning(assigns = {}) do
if class_name.underscore =~ /^(add|remove)_.*_(?:to|from)_(.*)/
assigns[:migration_action] = $1
assigns[:table_name] = $2.pluralize
else
assigns[:attributes] = []
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment