1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Handling add/remove to/from migration edge cases

Making sure the table name is parsed correctly when an add/remove column migration have 'from'/'to' in the table name.
This commit is contained in:
Guilherme Reis Campos 2017-08-02 23:41:41 +10:00
parent 090eaa7e1b
commit 1cc6c74db2
2 changed files with 23 additions and 1 deletions

View file

@ -28,7 +28,7 @@ module ActiveRecord
def set_local_assigns!
@migration_template = "migration.rb"
case file_name
when /^(add|remove)_.*_(?:to|from)_(.*)/
when /^(add)_.*_to_(.*)/, /^(remove)_.*?_from_(.*)/
@migration_action = $1
@table_name = normalize_table_name($2)
when /join_table/

View file

@ -48,6 +48,17 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
end
end
def test_add_migration_with_table_having_from_in_title
migration = "add_email_address_to_blacklisted_from_campaign"
run_generator [migration, "email_address:string"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_column :blacklisted_from_campaigns, :email_address, :string/, change)
end
end
end
def test_remove_migration_with_indexed_attribute
migration = "remove_title_body_from_posts"
run_generator [migration, "title:string:index", "body:text"]
@ -73,6 +84,17 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
end
end
def test_remove_migration_with_table_having_to_in_title
migration = "remove_email_address_from_sent_to_user"
run_generator [migration, "email_address:string"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/remove_column :sent_to_users, :email_address, :string/, change)
end
end
end
def test_remove_migration_with_references_options
migration = "remove_references_from_books"
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]