Merge remote-tracking branch 'origin/master' into cleaning

This commit is contained in:
Ethan 2014-01-23 17:53:30 -05:00
commit 587d0ac6c8
10 changed files with 35 additions and 19 deletions

3
.gitignore vendored
View file

@ -9,4 +9,7 @@ examples/db/*.db
examples/config/database.yml
db/config.yml
db/test.sqlite3
.rbenv-version
.rvmrc
.ruby-version
.vagrant

View file

@ -1 +0,0 @@
1.9.2-p290

1
.rvmrc
View file

@ -1 +0,0 @@
rvm 1.9.3

View file

@ -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

View file

@ -314,7 +314,7 @@ Usage beyond that remains the same with `DatabaseCleaner.start` calling any setu
<tr>
<td> Sequel</td>
<td> <code>DatabaseCleaner[:sequel]</code></td>
<td> Multiple databases supported; specify <code>Databasecleaner[:sequel, {:connection =&gt; Sequel.connect(uri)}]</code></td>
<td> Multiple databases supported; specify <code>DatabaseCleaner[:sequel, {:connection =&gt; Sequel.connect(uri)}]</code></td>
</tr>
<tr>
<td>Redis</td>

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)