Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling add/remove to/from migration edge cases #30011

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def create_migration_file
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/
Expand Down
22 changes: 22 additions & 0 deletions railties/test/generators/migration_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ def test_add_migration_with_attributes
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"]
Expand All @@ -73,6 +84,17 @@ def test_remove_migration_with_attributes
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}"]
Expand Down