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 fe7eb07
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/schema_dumper.rb
Original file line number Diff line number Diff line change
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
30 changes: 20 additions & 10 deletions activerecord/test/cases/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,21 +473,21 @@ def test_do_not_dump_foreign_keys_when_bypassed_by_config
end
end

class CreateDogMigration < ActiveRecord::Migration::Current
class CreateCatMigration < ActiveRecord::Migration::Current
def up
create_table("dog_owners") do |t|
create_table("cat_owners") do |t|
end

create_table("dogs") do |t|
create_table("cats") do |t|
t.column :name, :string
t.references :owner
t.index [:name]
t.foreign_key :dog_owners, column: "owner_id"
t.foreign_key :cat_owners, column: "owner_id"
end
end
def down
drop_table("dogs")
drop_table("dog_owners")
drop_table("cats")
drop_table("cat_owners")
end
end

Expand All @@ -497,16 +497,21 @@ def test_schema_dump_with_table_name_prefix_and_suffix
ActiveRecord::Base.table_name_prefix = "foo_"
ActiveRecord::Base.table_name_suffix = "_bar"

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

output = dump_table_schema("dog_owners", "dogs")
output = dump_table_schema("foo_cat_owners_bar", "foo_cats_bar")

assert_match %r{create_table "cat_owners"}, output
assert_match %r{create_table "cats"}, output
assert_match %r{t\.index \["name"\], name: "index_foo_cats_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 "cats", "cat_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 @@ -524,16 +529,21 @@ def test_schema_dump_with_table_name_prefix_and_suffix_regexp_escape
ActiveRecord::Base.table_name_prefix = "foo$"
ActiveRecord::Base.table_name_suffix = "$bar"

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

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

assert_match %r{create_table "cat_owners"}, output
assert_match %r{create_table "cats"}, output
assert_match %r{t\.index \["name"\], name: "index_foo\$cats\$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 "cats", "cat_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 fe7eb07

Please sign in to comment.