From f451054745a36d361127ddd128c362df7f38547d Mon Sep 17 00:00:00 2001 From: Rhett Sutphin Date: Tue, 15 May 2012 10:22:47 -0500 Subject: [PATCH 1/7] Use a single statement for DataMapper truncation on PostgreSQL. This change aligns the behavior of the DM PostgreSQL truncation adapter with the ActiveRecord one. A single statement is faster and safer than lots of `TRUNCATE TABLE ... CASCADE` statements. While this commit only implements single statements for PostgreSQL, it provides the extension point necessary for other adapters to switch to single statements in the future. --- lib/database_cleaner/data_mapper/truncation.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/database_cleaner/data_mapper/truncation.rb b/lib/database_cleaner/data_mapper/truncation.rb index 6625558..a759b23 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 @@ -112,6 +118,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? @@ -153,9 +165,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 From 3f1b833a1baccccd1bbd4a1b779491ffba23dacf Mon Sep 17 00:00:00 2001 From: Avin Mathew Date: Wed, 9 Oct 2013 12:31:59 +1000 Subject: [PATCH 2/7] Support Oracle JDBC Adapter Oracle does not support semi-colons in TRUNCATE TABLE statements and throws an `ORA-00911: invalid character` error. So use the same syntax with a JdbcOracleConnection as used by the OracleEnhancedAdapter. --- lib/database_cleaner/active_record/truncation.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 From 6da38c4c1a565bbcc62ed9b84c2574537c6ebae2 Mon Sep 17 00:00:00 2001 From: Ben Mabey Date: Wed, 9 Oct 2013 10:01:41 -0600 Subject: [PATCH 3/7] fixes Rakefile... --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" From 5a5f9fad00dd9a4769cb0e4f9ed90515a57def49 Mon Sep 17 00:00:00 2001 From: Navin Peiris Date: Mon, 11 Nov 2013 15:46:45 +1100 Subject: [PATCH 4/7] Fixing minor typo due to a case error in README --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 356ab342a455fffa38190459b1f4e0f804b2d63d Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Mon, 18 Nov 2013 22:48:20 -0800 Subject: [PATCH 5/7] Transactions should not be joinable --- lib/database_cleaner/active_record/transaction.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/database_cleaner/active_record/transaction.rb b/lib/database_cleaner/active_record/transaction.rb index 551485c..5ac0dc6 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 From dedecd0b44ac84af3c7b6ff2ab29b811054bf771 Mon Sep 17 00:00:00 2001 From: Potapov Sergey Date: Fri, 6 Dec 2013 17:18:39 +0200 Subject: [PATCH 6/7] Use respond_to? with true to check inspect private/protected methods --- lib/database_cleaner/active_record/transaction.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/database_cleaner/active_record/transaction.rb b/lib/database_cleaner/active_record/transaction.rb index 551485c..bc7696e 100644 --- a/lib/database_cleaner/active_record/transaction.rb +++ b/lib/database_cleaner/active_record/transaction.rb @@ -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 From 83a9e99925db3aa6b01331c43ed86820c0ee8ab9 Mon Sep 17 00:00:00 2001 From: "Peter M. Goldstein" Date: Fri, 27 Dec 2013 10:50:10 -0800 Subject: [PATCH 7/7] Changes to get specs to green. Remove Ruby version related dotfiles to allow use of multiple Ruby versions --- .gitignore | 3 +++ .rbenv-version | 1 - .rvmrc | 1 - .travis.yml | 5 ++--- spec/database_cleaner/active_record/transaction_spec.rb | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) delete mode 100644 .rbenv-version delete mode 100644 .rvmrc 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/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)