Created
October 5, 2011 12:42
-
-
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 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
# 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