提交 fe0145c5 编写于 作者: R Ryuta Kamizono

Respect table name prefix/suffix for `truncate_all`

上级 9bccf460
......@@ -17,7 +17,7 @@ def primary_key
end
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
def []=(key, value)
......@@ -44,6 +44,10 @@ def create_table
end
end
end
def drop_table
connection.drop_table table_name, if_exists: true
end
end
end
end
......@@ -19,7 +19,7 @@ def primary_key
end
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
def table_exists?
......
......@@ -186,8 +186,8 @@ def truncate_tables(configuration)
ActiveRecord::Base.connected_to(database: { truncation: configuration }) do
table_names = ActiveRecord::Base.connection.tables
table_names -= [
ActiveRecord::Base.schema_migrations_table_name,
ActiveRecord::Base.internal_metadata_table_name
SchemaMigration.table_name,
InternalMetadata.table_name
]
ActiveRecord::Base.connection.truncate_tables(*table_names)
......
......@@ -155,7 +155,7 @@ class ForeignKeyChangeColumnWithPrefixTest < ForeignKeyChangeColumnTest
class ForeignKeyChangeColumnWithSuffixTest < ForeignKeyChangeColumnTest
setup do
ActiveRecord::Base.table_name_suffix = "_p"
ActiveRecord::Base.table_name_suffix = "_s"
end
teardown do
......
......@@ -50,6 +50,8 @@ def test_raises_an_error_when_called_with_protected_environment
protected_environments = ActiveRecord::Base.protected_environments
current_env = ActiveRecord::Base.connection.migration_context.current_environment
InternalMetadata[:environment] = current_env
assert_called_on_instance_of(
ActiveRecord::MigrationContext,
:current_version,
......@@ -73,6 +75,9 @@ def test_raises_an_error_when_called_with_protected_environment
def test_raises_an_error_when_called_with_protected_environment_which_name_is_a_symbol
protected_environments = ActiveRecord::Base.protected_environments
current_env = ActiveRecord::Base.connection.migration_context.current_environment
InternalMetadata[:environment] = current_env
assert_called_on_instance_of(
ActiveRecord::MigrationContext,
:current_version,
......@@ -951,11 +956,22 @@ class DatabaseTasksTruncateAllTest < ActiveRecord::TestCase
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
SchemaMigration.drop_table
InternalMetadata.drop_table
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler }
end
def test_truncate_tables
assert_operator SchemaMigration.count, :>, 0
assert_operator InternalMetadata.count, :>, 0
assert_operator Author.count, :>, 0
assert_operator AuthorAddress.count, :>, 0
......@@ -969,12 +985,54 @@ def test_truncate_tables
)
end
assert_operator SchemaMigration.count, :>, 0
assert_operator InternalMetadata.count, :>, 0
assert_equal 0, Author.count
assert_equal 0, AuthorAddress.count
ensure
ActiveRecord::Base.configurations = old_configurations
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
class DatabaseTasksTruncateAllWithMultipleDatabasesTest < ActiveRecord::TestCase
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册