diff --git a/.gitignore b/.gitignore index 26dd738..0151906 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,7 @@ examples/db/*.db examples/config/database.yml db/config.yml db/test.sqlite3 +.rbenv-version +.rvmrc +.ruby-version .vagrant diff --git a/.rbenv-version b/.rbenv-version deleted file mode 100644 index 0a95b9f..0000000 --- a/.rbenv-version +++ /dev/null @@ -1 +0,0 @@ -1.9.2-p290 diff --git a/.rvmrc b/.rvmrc deleted file mode 100644 index 134d9ca..0000000 --- a/.rvmrc +++ /dev/null @@ -1 +0,0 @@ -rvm 1.9.3 diff --git a/.travis.yml b/.travis.yml index b64f642..4ab0296 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: ruby rvm: - 1.9.3 - 2.0.0 + - 2.1.0 # TODO: make this work with the regular rake command script: "bundle exec rake spec" gemfile: @@ -11,7 +12,5 @@ before_script: - psql -c 'create database database_cleaner_test;' -U postgres - cp db/sample.config.yml db/config.yml services: + - redis-server - mongodb -matrix: - allow_failures: - - rvm: 2.0.0 diff --git a/README.markdown b/README.markdown index 01dce55..dcba379 100644 --- a/README.markdown +++ b/README.markdown @@ -314,7 +314,7 @@ Usage beyond that remains the same with `DatabaseCleaner.start` calling any setu Sequel DatabaseCleaner[:sequel] - Multiple databases supported; specify Databasecleaner[:sequel, {:connection => Sequel.connect(uri)}] + Multiple databases supported; specify DatabaseCleaner[:sequel, {:connection => Sequel.connect(uri)}] Redis diff --git a/Rakefile b/Rakefile index 5e4ba34..e437614 100644 --- a/Rakefile +++ b/Rakefile @@ -14,7 +14,7 @@ begin s.description = "Strategies for cleaning databases. Can be used to ensure a clean state for testing." s.files = FileList["[A-Z]*.*", "{examples,lib,features,spec}/**/*", "Rakefile", "cucumber.yml"] s.authors = ["Ben Mabey"] - s.licence = 'MIT' + # s.licence = 'MIT' end rescue LoadError puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com" diff --git a/lib/database_cleaner/active_record/transaction.rb b/lib/database_cleaner/active_record/transaction.rb index 551485c..acec99b 100644 --- a/lib/database_cleaner/active_record/transaction.rb +++ b/lib/database_cleaner/active_record/transaction.rb @@ -19,7 +19,7 @@ module DatabaseCleaner::ActiveRecord end end if connection_class.connection.respond_to?(:begin_transaction) - connection_class.connection.begin_transaction + connection_class.connection.begin_transaction joinable: false else connection_class.connection.begin_db_transaction end @@ -36,7 +36,7 @@ module DatabaseCleaner::ActiveRecord end # The below is for handling after_commit hooks.. see https://github.com/bmabey/database_cleaner/issues/99 - if connection_class.connection.respond_to?(:rollback_transaction_records) + if connection_class.connection.respond_to?(:rollback_transaction_records, true) connection_class.connection.send(:rollback_transaction_records, true) end diff --git a/lib/database_cleaner/active_record/truncation.rb b/lib/database_cleaner/active_record/truncation.rb index 89801f4..b53986b 100755 --- a/lib/database_cleaner/active_record/truncation.rb +++ b/lib/database_cleaner/active_record/truncation.rb @@ -114,13 +114,13 @@ module DatabaseCleaner def truncate_table(table_name) begin execute("TRUNCATE TABLE #{quote_table_name(table_name)};") - rescue ActiveRecord::StatementInvalid + rescue ::ActiveRecord::StatementInvalid execute("DELETE FROM #{quote_table_name(table_name)};") end end end - module OracleEnhancedAdapter + module OracleAdapter def truncate_table(table_name) execute("TRUNCATE TABLE #{quote_table_name(table_name)}") end @@ -176,7 +176,13 @@ module ActiveRecord #Apply adapter decoraters where applicable (adapter should be loaded) AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractAdapter } - JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::TruncateOrDelete } if defined?(JdbcAdapter) + if defined?(JdbcAdapter) + if defined?(OracleJdbcConnection) + JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleAdapter } + else + JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::TruncateOrDelete } + end + end AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(AbstractMysqlAdapter) Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(Mysql2Adapter) SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::SQLiteAdapter } if defined?(SQLiteAdapter) @@ -184,7 +190,7 @@ module ActiveRecord PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::PostgreSQLAdapter } if defined?(PostgreSQLAdapter) IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::IBM_DBAdapter } if defined?(IBM_DBAdapter) SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::TruncateOrDelete } if defined?(SQLServerAdapter) - OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleEnhancedAdapter } if defined?(OracleEnhancedAdapter) + OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleAdapter } if defined?(OracleEnhancedAdapter) end end diff --git a/lib/database_cleaner/data_mapper/truncation.rb b/lib/database_cleaner/data_mapper/truncation.rb index 98207b5..acb9732 100644 --- a/lib/database_cleaner/data_mapper/truncation.rb +++ b/lib/database_cleaner/data_mapper/truncation.rb @@ -10,6 +10,12 @@ module DataMapper raise NotImplementedError end + def truncate_tables(table_names) + table_names.each do |table_name| + adapter.truncate_table table_name + end + end + end class MysqlAdapter < DataObjectsAdapter @@ -136,6 +142,12 @@ module DataMapper execute("TRUNCATE TABLE #{quote_name(table_name)} RESTART IDENTITY CASCADE;") end + # override to use a single statement + def truncate_tables(table_names) + quoted_names = table_names.collect { |n| quote_name(n) }.join(', ') + execute("TRUNCATE TABLE #{quoted_names} RESTART IDENTITY;") + end + # FIXME # copied from activerecord def supports_disable_referential_integrity? @@ -177,9 +189,7 @@ module DatabaseCleaner def clean(repository = self.db) adapter = ::DataMapper.repository(repository).adapter adapter.disable_referential_integrity do - tables_to_truncate(repository).each do |table_name| - adapter.truncate_table table_name - end + adapter.truncate_tables(tables_to_truncate(repository)) end end diff --git a/spec/database_cleaner/active_record/transaction_spec.rb b/spec/database_cleaner/active_record/transaction_spec.rb index 2fe6f87..a1bf915 100644 --- a/spec/database_cleaner/active_record/transaction_spec.rb +++ b/spec/database_cleaner/active_record/transaction_spec.rb @@ -58,7 +58,7 @@ module DatabaseCleaner connection.should_receive(:open_transactions).and_return(1) connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(true) - connection.stub(:respond_to?).with(:rollback_transaction_records).and_return(false) + connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) @@ -75,7 +75,7 @@ module DatabaseCleaner it "should decrement connection via ActiveRecord::Base if connection won't" do connection.should_receive(:open_transactions).and_return(1) connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction_records).and_return(false) + connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) @@ -112,7 +112,7 @@ module DatabaseCleaner it "should decrement connection via ActiveRecord::Base if connection won't" do connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:rollback_transaction_records).and_return(false) + connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) connection.stub(:respond_to?).with(:rollback_transaction).and_return(true) connection.stub(:rollback_transaction)