diff options
Diffstat (limited to 'railties')
4 files changed, 31 insertions, 2 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 877decf90f..24eda9da03 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Automatically generate add/remove column commands in specially named migrations like AddLocationToEvent. Closes #9006 [zenspider] + * Default to plural table name in Rails Generator if ActiveRecord is not present. Closes #8963 [evan] * Added rake routes for listing all the defined routes in the system #8795 [josh] diff --git a/railties/lib/rails_generator/generators/components/migration/USAGE b/railties/lib/rails_generator/generators/components/migration/USAGE index 152e2abdd6..c3f8ce0987 100644 --- a/railties/lib/rails_generator/generators/components/migration/USAGE +++ b/railties/lib/rails_generator/generators/components/migration/USAGE @@ -3,8 +3,22 @@ Description: CamelCased or under_scored, as an argument. A migration class is generated in db/migrate prefixed by the latest migration number. + You can name your migration in either of these formats to generate add/remove + column lines: AddColumnToTable or RemoveColumnFromTable + Example: `./script/generate migration AddSslFlag` With 4 existing migrations, this creates the AddSslFlag migration in db/migrate/005_add_ssl_flag.rb + + `./script/generate migration AddSslFlagToAccount` + + This will create the AddSslFlagToAccount in db/migrate/005_add_ssl_flag_to_account.rb with + this in the Up migration: + + add_column :accounts, :ssl_flag, :type, :null => :no?, :default => :maybe? + + And this in the Down migration: + + remove_column :accounts, :ssl_flag
\ No newline at end of file diff --git a/railties/lib/rails_generator/generators/components/migration/migration_generator.rb b/railties/lib/rails_generator/generators/components/migration/migration_generator.rb index a0d0d472c2..2fd348e47b 100644 --- a/railties/lib/rails_generator/generators/components/migration/migration_generator.rb +++ b/railties/lib/rails_generator/generators/components/migration/migration_generator.rb @@ -4,4 +4,17 @@ class MigrationGenerator < Rails::Generator::NamedBase m.migration_template 'migration.rb', 'db/migrate' end end + + def auto_migration direction + case class_name.underscore + when /^(add|remove)_(.*)_(?:to|from)_(.*)/ then + action, col, tbl = $1, $2, $3.pluralize + + unless (action == "add") ^ (direction == :up) then + %(\n add_column :#{tbl}, :#{col}, :type, :null => :no?, :default => :maybe?) + else + %(\n remove_column :#{tbl}, :#{col}) + end + end + end end diff --git a/railties/lib/rails_generator/generators/components/migration/templates/migration.rb b/railties/lib/rails_generator/generators/components/migration/templates/migration.rb index 2600610fc6..86edcac261 100644 --- a/railties/lib/rails_generator/generators/components/migration/templates/migration.rb +++ b/railties/lib/rails_generator/generators/components/migration/templates/migration.rb @@ -1,7 +1,7 @@ class <%= class_name.underscore.camelize %> < ActiveRecord::Migration - def self.up + def self.up<%= auto_migration :up %> end - def self.down + def self.down<%= auto_migration :down %> end end |