diff --git a/lib/database_cleaner/active_record/truncation.rb b/lib/database_cleaner/active_record/truncation.rb index d4997de..3854565 100644 --- a/lib/database_cleaner/active_record/truncation.rb +++ b/lib/database_cleaner/active_record/truncation.rb @@ -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 diff --git a/lib/database_cleaner/configuration.rb b/lib/database_cleaner/configuration.rb index 8a8e668..95a3306 100644 --- a/lib/database_cleaner/configuration.rb +++ b/lib/database_cleaner/configuration.rb @@ -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 diff --git a/lib/database_cleaner/data_mapper/truncation.rb b/lib/database_cleaner/data_mapper/truncation.rb index 39dcf50..b8014e5 100644 --- a/lib/database_cleaner/data_mapper/truncation.rb +++ b/lib/database_cleaner/data_mapper/truncation.rb @@ -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