introduced common base class for truncation strategies.
This commit is contained in:
parent
5bf0863cc5
commit
971cdc9460
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
module ConnectionAdapters
|
module ConnectionAdapters
|
||||||
|
|
||||||
class MysqlAdapter
|
class MysqlAdapter
|
||||||
def truncate_table(table_name)
|
def truncate_table(table_name)
|
||||||
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
|
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
|
||||||
|
@ -25,31 +26,12 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
module DatabaseCleaner::ActiveRecord
|
module DatabaseCleaner::ActiveRecord
|
||||||
class Truncation
|
class Truncation < ::DatabaseCleaner::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] || []) << 'schema_migrations'
|
|
||||||
end
|
|
||||||
|
|
||||||
def start
|
|
||||||
# no-op
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def clean
|
def clean
|
||||||
connection.disable_referential_integrity do
|
connection.disable_referential_integrity do
|
||||||
|
@ -69,8 +51,12 @@ module DatabaseCleaner::ActiveRecord
|
||||||
::ActiveRecord::Base.connection
|
::ActiveRecord::Base.connection
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# overwritten
|
||||||
|
def migration_storage_name
|
||||||
|
'schema_migrations'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -91,4 +91,47 @@ 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
|
end
|
||||||
|
|
|
@ -110,29 +110,11 @@ module DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
module DatabaseCleaner::DataMapper
|
module DatabaseCleaner::DataMapper
|
||||||
class Truncation
|
class Truncation < ::DatabaseCleaner::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] || []) << 'migration_info' # dm-migrations calls it like so
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def start(repository = :default)
|
|
||||||
# no-op
|
|
||||||
end
|
|
||||||
|
|
||||||
def clean(repository = :default)
|
def clean(repository = :default)
|
||||||
adapter = DataMapper.repository(repository).adapter
|
adapter = DataMapper.repository(repository).adapter
|
||||||
|
@ -149,6 +131,10 @@ module DatabaseCleaner::DataMapper
|
||||||
(@only || DataMapper.repository(repository).adapter.storage_names(repository)) - @tables_to_exclude
|
(@only || DataMapper.repository(repository).adapter.storage_names(repository)) - @tables_to_exclude
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# overwritten
|
||||||
|
def migration_storage_name
|
||||||
|
'migration_info'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue