introduced common base class for truncation strategies.
This commit is contained in:
parent
5bf0863cc5
commit
971cdc9460
|
@ -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
|
||||
|
@ -69,8 +51,12 @@ module DatabaseCleaner::ActiveRecord
|
|||
::ActiveRecord::Base.connection
|
||||
end
|
||||
|
||||
end
|
||||
# overwritten
|
||||
def migration_storage_name
|
||||
'schema_migrations'
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -91,4 +91,47 @@ module DatabaseCleaner
|
|||
|
||||
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
|
||||
|
|
|
@ -110,29 +110,11 @@ 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
|
||||
|
||||
|
||||
def start(repository = :default)
|
||||
# no-op
|
||||
end
|
||||
class Truncation < ::DatabaseCleaner::TruncationBase
|
||||
|
||||
def clean(repository = :default)
|
||||
adapter = DataMapper.repository(repository).adapter
|
||||
|
@ -141,14 +123,18 @@ module DatabaseCleaner::DataMapper
|
|||
adapter.truncate_table table_name
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def tables_to_truncate(repository = :default)
|
||||
(@only || DataMapper.repository(repository).adapter.storage_names(repository)) - @tables_to_exclude
|
||||
end
|
||||
|
||||
end
|
||||
# overwritten
|
||||
def migration_storage_name
|
||||
'migration_info'
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue