1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Respect table name prefix/suffix for truncate_all

This commit is contained in:
Ryuta Kamizono 2019-04-04 02:17:33 +09:00
parent 9bccf4604f
commit fe0145c580
5 changed files with 67 additions and 5 deletions

View file

@ -17,7 +17,7 @@ module ActiveRecord
end end
def table_name def table_name
"#{table_name_prefix}#{ActiveRecord::Base.internal_metadata_table_name}#{table_name_suffix}" "#{table_name_prefix}#{internal_metadata_table_name}#{table_name_suffix}"
end end
def []=(key, value) def []=(key, value)
@ -44,6 +44,10 @@ module ActiveRecord
end end
end end
end end
def drop_table
connection.drop_table table_name, if_exists: true
end
end end
end end
end end

View file

@ -19,7 +19,7 @@ module ActiveRecord
end end
def table_name def table_name
"#{table_name_prefix}#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}" "#{table_name_prefix}#{schema_migrations_table_name}#{table_name_suffix}"
end end
def table_exists? def table_exists?

View file

@ -186,8 +186,8 @@ module ActiveRecord
ActiveRecord::Base.connected_to(database: { truncation: configuration }) do ActiveRecord::Base.connected_to(database: { truncation: configuration }) do
table_names = ActiveRecord::Base.connection.tables table_names = ActiveRecord::Base.connection.tables
table_names -= [ table_names -= [
ActiveRecord::Base.schema_migrations_table_name, SchemaMigration.table_name,
ActiveRecord::Base.internal_metadata_table_name InternalMetadata.table_name
] ]
ActiveRecord::Base.connection.truncate_tables(*table_names) ActiveRecord::Base.connection.truncate_tables(*table_names)

View file

@ -155,7 +155,7 @@ if ActiveRecord::Base.connection.supports_foreign_keys?
class ForeignKeyChangeColumnWithSuffixTest < ForeignKeyChangeColumnTest class ForeignKeyChangeColumnWithSuffixTest < ForeignKeyChangeColumnTest
setup do setup do
ActiveRecord::Base.table_name_suffix = "_p" ActiveRecord::Base.table_name_suffix = "_s"
end end
teardown do teardown do

View file

@ -50,6 +50,8 @@ module ActiveRecord
protected_environments = ActiveRecord::Base.protected_environments protected_environments = ActiveRecord::Base.protected_environments
current_env = ActiveRecord::Base.connection.migration_context.current_environment current_env = ActiveRecord::Base.connection.migration_context.current_environment
InternalMetadata[:environment] = current_env
assert_called_on_instance_of( assert_called_on_instance_of(
ActiveRecord::MigrationContext, ActiveRecord::MigrationContext,
:current_version, :current_version,
@ -73,6 +75,9 @@ module ActiveRecord
def test_raises_an_error_when_called_with_protected_environment_which_name_is_a_symbol def test_raises_an_error_when_called_with_protected_environment_which_name_is_a_symbol
protected_environments = ActiveRecord::Base.protected_environments protected_environments = ActiveRecord::Base.protected_environments
current_env = ActiveRecord::Base.connection.migration_context.current_environment current_env = ActiveRecord::Base.connection.migration_context.current_environment
InternalMetadata[:environment] = current_env
assert_called_on_instance_of( assert_called_on_instance_of(
ActiveRecord::MigrationContext, ActiveRecord::MigrationContext,
:current_version, :current_version,
@ -951,11 +956,22 @@ module ActiveRecord
fixtures :authors, :author_addresses fixtures :authors, :author_addresses
def setup
SchemaMigration.create_table
SchemaMigration.create!(version: "foo")
InternalMetadata.create_table
InternalMetadata.create!(key: "foo", value: "bar")
end
def teardown def teardown
SchemaMigration.drop_table
InternalMetadata.drop_table
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler } ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler }
end end
def test_truncate_tables def test_truncate_tables
assert_operator SchemaMigration.count, :>, 0
assert_operator InternalMetadata.count, :>, 0
assert_operator Author.count, :>, 0 assert_operator Author.count, :>, 0
assert_operator AuthorAddress.count, :>, 0 assert_operator AuthorAddress.count, :>, 0
@ -969,12 +985,54 @@ module ActiveRecord
) )
end end
assert_operator SchemaMigration.count, :>, 0
assert_operator InternalMetadata.count, :>, 0
assert_equal 0, Author.count assert_equal 0, Author.count
assert_equal 0, AuthorAddress.count assert_equal 0, AuthorAddress.count
ensure ensure
ActiveRecord::Base.configurations = old_configurations ActiveRecord::Base.configurations = old_configurations
end end
end end
class DatabaseTasksTruncateAllWithPrefixTest < DatabaseTasksTruncateAllTest
setup do
ActiveRecord::Base.table_name_prefix = "p_"
SchemaMigration.reset_table_name
SchemaMigration.reset_column_information
InternalMetadata.reset_table_name
InternalMetadata.reset_column_information
end
teardown do
ActiveRecord::Base.table_name_prefix = nil
SchemaMigration.reset_table_name
SchemaMigration.reset_column_information
InternalMetadata.reset_table_name
InternalMetadata.reset_column_information
end
end
class DatabaseTasksTruncateAllWithSuffixTest < DatabaseTasksTruncateAllTest
setup do
ActiveRecord::Base.table_name_suffix = "_s"
SchemaMigration.reset_table_name
SchemaMigration.reset_column_information
InternalMetadata.reset_table_name
InternalMetadata.reset_column_information
end
teardown do
ActiveRecord::Base.table_name_suffix = nil
SchemaMigration.reset_table_name
SchemaMigration.reset_column_information
InternalMetadata.reset_table_name
InternalMetadata.reset_column_information
end
end
end end
class DatabaseTasksTruncateAllWithMultipleDatabasesTest < ActiveRecord::TestCase class DatabaseTasksTruncateAllWithMultipleDatabasesTest < ActiveRecord::TestCase