Skip to content

Commit fe7eb07

Browse files
committed
Fix remove_prefix_and_suffix in ActiveRecord::SchemaDumper
Accidentally `remove_prefix_and_suffix` in `ActiveRecord::SchemaDumper` has broken due to table name prefix and suffix options is assigned to unused `version` argument in `SchemaDumper#initialize` added in rails#51162.
1 parent 5ccd259 commit fe7eb07

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

activerecord/lib/active_record/schema_dumper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def dump(stream)
7070
private
7171
attr_accessor :table_name
7272

73-
def initialize(connection, version, options = {})
73+
def initialize(connection, options = {})
7474
@connection = connection
7575
@version = connection.pool.migration_context.current_version rescue nil
7676
@options = options

activerecord/test/cases/schema_dumper_test.rb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,21 +473,21 @@ def test_do_not_dump_foreign_keys_when_bypassed_by_config
473473
end
474474
end
475475

476-
class CreateDogMigration < ActiveRecord::Migration::Current
476+
class CreateCatMigration < ActiveRecord::Migration::Current
477477
def up
478-
create_table("dog_owners") do |t|
478+
create_table("cat_owners") do |t|
479479
end
480480

481-
create_table("dogs") do |t|
481+
create_table("cats") do |t|
482482
t.column :name, :string
483483
t.references :owner
484484
t.index [:name]
485-
t.foreign_key :dog_owners, column: "owner_id"
485+
t.foreign_key :cat_owners, column: "owner_id"
486486
end
487487
end
488488
def down
489-
drop_table("dogs")
490-
drop_table("dog_owners")
489+
drop_table("cats")
490+
drop_table("cat_owners")
491491
end
492492
end
493493

@@ -497,16 +497,21 @@ def test_schema_dump_with_table_name_prefix_and_suffix
497497
ActiveRecord::Base.table_name_prefix = "foo_"
498498
ActiveRecord::Base.table_name_suffix = "_bar"
499499

500-
migration = CreateDogMigration.new
500+
migration = CreateCatMigration.new
501501
migration.migrate(:up)
502502

503-
output = dump_table_schema("dog_owners", "dogs")
503+
output = dump_table_schema("foo_cat_owners_bar", "foo_cats_bar")
504+
505+
assert_match %r{create_table "cat_owners"}, output
506+
assert_match %r{create_table "cats"}, output
507+
assert_match %r{t\.index \["name"\], name: "index_foo_cats_bar_on_name"}, output
504508
assert_no_match %r{create_table "foo_.+_bar"}, output
505509
assert_no_match %r{add_index "foo_.+_bar"}, output
506510
assert_no_match %r{create_table "schema_migrations"}, output
507511
assert_no_match %r{create_table "ar_internal_metadata"}, output
508512

509513
if ActiveRecord::Base.lease_connection.supports_foreign_keys?
514+
assert_match %r{add_foreign_key "cats", "cat_owners", column: "owner_id"}, output
510515
assert_no_match %r{add_foreign_key "foo_.+_bar"}, output
511516
assert_no_match %r{add_foreign_key "[^"]+", "foo_.+_bar"}, output
512517
end
@@ -524,16 +529,21 @@ def test_schema_dump_with_table_name_prefix_and_suffix_regexp_escape
524529
ActiveRecord::Base.table_name_prefix = "foo$"
525530
ActiveRecord::Base.table_name_suffix = "$bar"
526531

527-
migration = CreateDogMigration.new
532+
migration = CreateCatMigration.new
528533
migration.migrate(:up)
529534

530-
output = dump_table_schema("dog_owners", "dog")
535+
output = dump_table_schema("foo$cat_owners$bar", "foo$cat$bar")
536+
537+
assert_match %r{create_table "cat_owners"}, output
538+
assert_match %r{create_table "cats"}, output
539+
assert_match %r{t\.index \["name"\], name: "index_foo\$cats\$bar_on_name"}, output
531540
assert_no_match %r{create_table "foo\$.+\$bar"}, output
532541
assert_no_match %r{add_index "foo\$.+\$bar"}, output
533542
assert_no_match %r{create_table "schema_migrations"}, output
534543
assert_no_match %r{create_table "ar_internal_metadata"}, output
535544

536545
if ActiveRecord::Base.lease_connection.supports_foreign_keys?
546+
assert_match %r{add_foreign_key "cats", "cat_owners", column: "owner_id"}, output
537547
assert_no_match %r{add_foreign_key "foo\$.+\$bar"}, output
538548
assert_no_match %r{add_foreign_key "[^"]+", "foo\$.+\$bar"}, output
539549
end

0 commit comments

Comments
 (0)