Skip to content

Commit

Permalink
Fix remove_prefix_and_suffix in ActiveRecord::SchemaDumper
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kamipo committed Apr 14, 2024
1 parent 5ccd259 commit 0944512
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/schema_dumper.rb
Expand Up @@ -70,7 +70,7 @@ def dump(stream)
private
attr_accessor :table_name

def initialize(connection, version, options = {})
def initialize(connection, options = {})
@connection = connection
@version = connection.pool.migration_context.current_version rescue nil
@options = options
Expand Down
18 changes: 16 additions & 2 deletions activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -493,20 +493,27 @@ def down

def test_schema_dump_with_table_name_prefix_and_suffix
ActiveRecord::Base.establish_connection(:arunit2) unless in_memory_db?
ActiveRecord::Base.lease_connection.drop_table "dogs", if_exists: true
ActiveRecord::Base.lease_connection.drop_table "dog_owners", if_exists: true
original, $stdout = $stdout, StringIO.new
ActiveRecord::Base.table_name_prefix = "foo_"
ActiveRecord::Base.table_name_suffix = "_bar"

migration = CreateDogMigration.new
migration.migrate(:up)

output = dump_table_schema("dog_owners", "dogs")
output = dump_table_schema("foo_dog_owners_bar", "foo_dogs_bar")

assert_match %r{create_table "dog_owners"}, output
assert_match %r{create_table "dogs"}, output
assert_match %r{t\.index \["name"\], name: "index_foo_dogs_bar_on_name"}, output
assert_no_match %r{create_table "foo_.+_bar"}, output
assert_no_match %r{add_index "foo_.+_bar"}, output
assert_no_match %r{create_table "schema_migrations"}, output
assert_no_match %r{create_table "ar_internal_metadata"}, output

if ActiveRecord::Base.lease_connection.supports_foreign_keys?
assert_match %r{add_foreign_key "dogs", "dog_owners", column: "owner_id"}, output
assert_no_match %r{add_foreign_key "foo_.+_bar"}, output
assert_no_match %r{add_foreign_key "[^"]+", "foo_.+_bar"}, output
end
Expand All @@ -520,20 +527,27 @@ def test_schema_dump_with_table_name_prefix_and_suffix

def test_schema_dump_with_table_name_prefix_and_suffix_regexp_escape
ActiveRecord::Base.establish_connection(:arunit2) unless in_memory_db?
ActiveRecord::Base.lease_connection.drop_table "dogs", if_exists: true
ActiveRecord::Base.lease_connection.drop_table "dog_owners", if_exists: true
original, $stdout = $stdout, StringIO.new
ActiveRecord::Base.table_name_prefix = "foo$"
ActiveRecord::Base.table_name_suffix = "$bar"

migration = CreateDogMigration.new
migration.migrate(:up)

output = dump_table_schema("dog_owners", "dog")
output = dump_table_schema("foo$dog_owners$bar", "foo$dog$bar")

assert_match %r{create_table "dog_owners"}, output
assert_match %r{create_table "dogs"}, output
assert_match %r{t\.index \["name"\], name: "index_foo\$dogs\$bar_on_name"}, output
assert_no_match %r{create_table "foo\$.+\$bar"}, output
assert_no_match %r{add_index "foo\$.+\$bar"}, output
assert_no_match %r{create_table "schema_migrations"}, output
assert_no_match %r{create_table "ar_internal_metadata"}, output

if ActiveRecord::Base.lease_connection.supports_foreign_keys?
assert_match %r{add_foreign_key "dogs", "dog_owners", column: "owner_id"}, output
assert_no_match %r{add_foreign_key "foo\$.+\$bar"}, output
assert_no_match %r{add_foreign_key "[^"]+", "foo\$.+\$bar"}, output
end
Expand Down

0 comments on commit 0944512

Please sign in to comment.