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

Allow table_name_prefix and table_name_suffix have $ #30048

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ def format_options(options)
end

def remove_prefix_and_suffix(table)
table.gsub(/^(#{@options[:table_name_prefix]})(.+)(#{@options[:table_name_suffix]})$/, "\\2")
prefix = Regexp.escape(@options[:table_name_prefix].to_s)
suffix = Regexp.escape(@options[:table_name_suffix].to_s)
table.sub(/\A#{prefix}(.+)#{suffix}\z/, "\\1")
end

def ignored?(table_name)
Expand Down
25 changes: 25 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,31 @@ def test_schema_dump_with_table_name_prefix_and_suffix
$stdout = original
end

def test_schema_dump_with_table_name_prefix_and_suffix_regexp_escape
original, $stdout = $stdout, StringIO.new
ActiveRecord::Base.table_name_prefix = "foo$"
ActiveRecord::Base.table_name_suffix = "$bar"

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

output = perform_schema_dump
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.connection.supports_foreign_keys?
assert_no_match %r{add_foreign_key "foo\$.+\$bar"}, output
assert_no_match %r{add_foreign_key "[^"]+", "foo\$.+\$bar"}, output
end
ensure
migration.migrate(:down)

ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ""
$stdout = original
end

def test_schema_dump_with_table_name_prefix_and_ignoring_tables
original, $stdout = $stdout, StringIO.new

Expand Down