introduced common base class for truncation strategies.

This commit is contained in:
snusnu 2009-05-07 05:06:49 +02:00
parent 5bf0863cc5
commit 971cdc9460
3 changed files with 63 additions and 48 deletions

View File

@ -1,6 +1,7 @@
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter
def truncate_table(table_name)
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
@ -25,31 +26,12 @@ module ActiveRecord
end
end
end
end
module DatabaseCleaner::ActiveRecord
class Truncation
def initialize(options={})
if !options.empty? && !(options.keys - [:only, :except]).empty?
raise ArgumentError, "The only valid options are :only and :except. You specified #{options.keys.join(',')}."
end
if options.has_key?(:only) && options.has_key?(:except)
raise ArgumentError, "You may only specify either :only or :either. Doing both doesn't really make sense does it?"
end
@only = options[:only]
@tables_to_exclude = (options[:except] || []) << 'schema_migrations'
end
def start
# no-op
end
class Truncation < ::DatabaseCleaner::TruncationBase
def clean
connection.disable_referential_integrity do
@ -57,9 +39,9 @@ module DatabaseCleaner::ActiveRecord
connection.truncate_table table_name
end
end
end
end
private
private
def tables_to_truncate
(@only || connection.tables) - @tables_to_exclude
@ -68,9 +50,13 @@ module DatabaseCleaner::ActiveRecord
def connection
::ActiveRecord::Base.connection
end
# overwritten
def migration_storage_name
'schema_migrations'
end
end
end

View File

@ -90,5 +90,48 @@ module DatabaseCleaner
end
end
# common base class for truncation strategies
class TruncationBase
def initialize(options = {})
if !options.empty? && !(options.keys - [:only, :except]).empty?
raise ArgumentError, "The only valid options are :only and :except. You specified #{options.keys.join(',')}."
end
if options.has_key?(:only) && options.has_key?(:except)
raise ArgumentError, "You may only specify either :only or :either. Doing both doesn't really make sense does it?"
end
@only = options[:only]
@tables_to_exclude = (options[:except] || [])
if migration_storage = migration_storage_name
@tables_to_exclude << migration_storage
end
end
def start
# no-op
end
def clean
raise NotImplementedError
end
private
def tables_to_truncate
raise NotImplementedError
end
# overwrite in subclasses
# default implementation given because migration storage need not be present
def migration_storage_name
nil
end
end
end

View File

@ -110,30 +110,12 @@ module DataMapper
end
end
end
module DatabaseCleaner::DataMapper
class Truncation
def initialize(options={})
if !options.empty? && !(options.keys - [:only, :except]).empty?
raise ArgumentError, "The only valid options are :only and :except. You specified #{options.keys.join(',')}."
end
if options.has_key?(:only) && options.has_key?(:except)
raise ArgumentError, "You may only specify either :only or :either. Doing both doesn't really make sense does it?"
end
@only = options[:only]
@tables_to_exclude = (options[:except] || []) << 'migration_info' # dm-migrations calls it like so
end
class Truncation < ::DatabaseCleaner::TruncationBase
def start(repository = :default)
# no-op
end
def clean(repository = :default)
adapter = DataMapper.repository(repository).adapter
adapter.disable_referential_integrity do
@ -141,14 +123,18 @@ module DatabaseCleaner::DataMapper
adapter.truncate_table table_name
end
end
end
private
end
private
def tables_to_truncate(repository = :default)
(@only || DataMapper.repository(repository).adapter.storage_names(repository)) - @tables_to_exclude
end
# overwritten
def migration_storage_name
'migration_info'
end
end
end