database_cleaner 1.7.0 → 1.8.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CONTRIBUTE.markdown +1 -2
- data/Gemfile.lock +130 -91
- data/History.rdoc +1 -1
- data/README.markdown +71 -226
- data/Rakefile +12 -8
- data/adapters/database_cleaner-active_record/lib/database_cleaner-active_record.rb +1 -0
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record.rb +6 -0
- data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/base.rb +13 -4
- data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/deletion.rb +19 -19
- data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/transaction.rb +0 -0
- data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/truncation.rb +22 -19
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/version.rb +5 -0
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner-couch_potato.rb +1 -0
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato.rb +11 -0
- data/{lib → adapters/database_cleaner-couch_potato/lib}/database_cleaner/couch_potato/base.rb +0 -0
- data/{lib → adapters/database_cleaner-couch_potato/lib}/database_cleaner/couch_potato/truncation.rb +0 -0
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/version.rb +5 -0
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner-data_mapper.rb +1 -0
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper.rb +4 -0
- data/{lib → adapters/database_cleaner-data_mapper/lib}/database_cleaner/data_mapper/base.rb +4 -0
- data/{lib → adapters/database_cleaner-data_mapper/lib}/database_cleaner/data_mapper/transaction.rb +0 -0
- data/{lib → adapters/database_cleaner-data_mapper/lib}/database_cleaner/data_mapper/truncation.rb +4 -3
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/version.rb +5 -0
- data/adapters/database_cleaner-mongo/lib/database_cleaner-mongo.rb +1 -0
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo.rb +10 -0
- data/{lib → adapters/database_cleaner-mongo/lib}/database_cleaner/mongo/base.rb +0 -0
- data/{lib → adapters/database_cleaner-mongo/lib}/database_cleaner/mongo/truncation.rb +0 -0
- data/{lib → adapters/database_cleaner-mongo/lib}/database_cleaner/mongo/truncation_mixin.rb +0 -0
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/version.rb +5 -0
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner-mongo_mapper.rb +1 -0
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper.rb +4 -0
- data/{lib → adapters/database_cleaner-mongo_mapper/lib}/database_cleaner/mongo_mapper/base.rb +4 -0
- data/{lib → adapters/database_cleaner-mongo_mapper/lib}/database_cleaner/mongo_mapper/truncation.rb +0 -0
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/version.rb +5 -0
- data/adapters/database_cleaner-mongoid/lib/database_cleaner-mongoid.rb +1 -0
- data/{lib → adapters/database_cleaner-mongoid/lib}/database_cleaner/mongo2/truncation_mixin.rb +0 -0
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid.rb +10 -0
- data/{lib → adapters/database_cleaner-mongoid/lib}/database_cleaner/mongoid/base.rb +0 -0
- data/{lib → adapters/database_cleaner-mongoid/lib}/database_cleaner/mongoid/truncation.rb +0 -0
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/version.rb +5 -0
- data/adapters/database_cleaner-moped/lib/database_cleaner-moped.rb +1 -0
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped.rb +10 -0
- data/{lib → adapters/database_cleaner-moped/lib}/database_cleaner/moped/base.rb +1 -1
- data/{lib → adapters/database_cleaner-moped/lib}/database_cleaner/moped/truncation.rb +0 -0
- data/{lib → adapters/database_cleaner-moped/lib}/database_cleaner/moped/truncation_base.rb +4 -0
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped/version.rb +5 -0
- data/adapters/database_cleaner-neo4j/lib/database_cleaner-neo4j.rb +1 -0
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j.rb +6 -0
- data/{lib → adapters/database_cleaner-neo4j/lib}/database_cleaner/neo4j/base.rb +4 -0
- data/{lib → adapters/database_cleaner-neo4j/lib}/database_cleaner/neo4j/deletion.rb +0 -0
- data/{lib → adapters/database_cleaner-neo4j/lib}/database_cleaner/neo4j/transaction.rb +0 -0
- data/{lib → adapters/database_cleaner-neo4j/lib}/database_cleaner/neo4j/truncation.rb +0 -0
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/version.rb +5 -0
- data/adapters/database_cleaner-ohm/lib/database_cleaner-ohm.rb +1 -0
- data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm.rb +10 -0
- data/{lib → adapters/database_cleaner-ohm/lib}/database_cleaner/ohm/truncation.rb +4 -0
- data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm/version.rb +5 -0
- data/adapters/database_cleaner-redis/lib/database_cleaner-redis.rb +1 -0
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis.rb +4 -0
- data/{lib → adapters/database_cleaner-redis/lib}/database_cleaner/redis/base.rb +4 -0
- data/{lib → adapters/database_cleaner-redis/lib}/database_cleaner/redis/truncation.rb +0 -0
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis/version.rb +5 -0
- data/adapters/database_cleaner-sequel/lib/database_cleaner-sequel.rb +1 -0
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel.rb +6 -0
- data/{lib → adapters/database_cleaner-sequel/lib}/database_cleaner/sequel/base.rb +4 -0
- data/{lib → adapters/database_cleaner-sequel/lib}/database_cleaner/sequel/deletion.rb +0 -0
- data/{lib → adapters/database_cleaner-sequel/lib}/database_cleaner/sequel/transaction.rb +0 -0
- data/{lib → adapters/database_cleaner-sequel/lib}/database_cleaner/sequel/truncation.rb +16 -15
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/version.rb +5 -0
- data/lib/database_cleaner.rb +36 -1
- data/lib/database_cleaner/base.rb +85 -103
- data/lib/database_cleaner/configuration.rb +93 -83
- data/lib/database_cleaner/deprecation.rb +21 -0
- data/lib/database_cleaner/null_strategy.rb +6 -6
- data/lib/database_cleaner/orm_autodetector.rb +31 -0
- data/lib/database_cleaner/safeguard.rb +38 -3
- data/lib/database_cleaner/spec.rb +2 -0
- data/lib/database_cleaner/spec/database_helper.rb +82 -0
- data/lib/database_cleaner/spec/shared_examples.rb +15 -0
- data/lib/database_cleaner/version.rb +3 -0
- metadata +111 -46
- data/VERSION.yml +0 -4
- data/lib/database_cleaner/mongo2/base.rb +0 -16
data/Rakefile
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
require "bundler"
|
3
|
+
require "bundler/gem_tasks"
|
3
4
|
Bundler.setup
|
4
5
|
|
5
6
|
require 'rake'
|
6
7
|
require 'rspec/core'
|
7
8
|
require 'rspec/core/rake_task'
|
8
9
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
9
|
-
|
10
10
|
spec.pattern = FileList['spec/**/*_spec.rb']
|
11
11
|
end
|
12
12
|
|
@@ -15,15 +15,19 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
|
|
15
15
|
spec.rcov = true
|
16
16
|
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
Cucumber::Rake::Task.new(:features)
|
21
|
-
rescue LoadError
|
22
|
-
puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
|
23
|
-
end
|
18
|
+
require 'cucumber/rake/task'
|
19
|
+
Cucumber::Rake::Task.new(:features)
|
24
20
|
|
25
|
-
|
21
|
+
desc "Run adapter test suites"
|
22
|
+
task :adapters do
|
23
|
+
Dir["adapters/*"].each do |adapter_dir|
|
24
|
+
Dir.chdir adapter_dir do
|
25
|
+
sh "bundle exec rake"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
26
29
|
|
30
|
+
task :default => [:spec, :features, :adapters]
|
27
31
|
|
28
32
|
desc "Cleans the project of any tmp file that should not be included in the gemspec."
|
29
33
|
task :clean do
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/active_record"
|
data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/base.rb
RENAMED
@@ -1,20 +1,29 @@
|
|
1
|
-
require 'database_cleaner/generic/base'
|
2
1
|
require 'active_record'
|
2
|
+
require 'database_cleaner/generic/base'
|
3
3
|
require 'erb'
|
4
|
+
require 'yaml'
|
4
5
|
|
5
6
|
module DatabaseCleaner
|
6
7
|
module ActiveRecord
|
7
|
-
|
8
8
|
def self.available_strategies
|
9
9
|
%w[truncation transaction deletion]
|
10
10
|
end
|
11
11
|
|
12
|
+
def self.default_strategy
|
13
|
+
:transaction
|
14
|
+
end
|
15
|
+
|
12
16
|
def self.config_file_location=(path)
|
13
17
|
@config_file_location = path
|
14
18
|
end
|
15
19
|
|
16
20
|
def self.config_file_location
|
17
|
-
@config_file_location ||=
|
21
|
+
@config_file_location ||= begin
|
22
|
+
# Has DC.app_root been set? Check in this intrusive way to avoid triggering deprecation warnings if it hasn't.
|
23
|
+
app_root = DatabaseCleaner.send(:configuration).instance_variable_get(:@app_root)
|
24
|
+
root = app_root || Dir.pwd
|
25
|
+
"#{root}/config/database.yml"
|
26
|
+
end
|
18
27
|
end
|
19
28
|
|
20
29
|
module Base
|
@@ -79,7 +88,7 @@ module DatabaseCleaner
|
|
79
88
|
if ::ActiveRecord::Base.respond_to?(:descendants)
|
80
89
|
database_name = connection_hash["database"] || connection_hash[:database]
|
81
90
|
models = ::ActiveRecord::Base.descendants
|
82
|
-
models.detect { |m| m.connection_pool.spec.config[:database] == database_name }
|
91
|
+
models.select(&:connection_pool).detect { |m| m.connection_pool.spec.config[:database] == database_name }
|
83
92
|
end
|
84
93
|
end
|
85
94
|
|
data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/deletion.rb
RENAMED
@@ -1,7 +1,6 @@
|
|
1
|
-
require 'active_record
|
1
|
+
require 'active_record'
|
2
2
|
require 'active_record/connection_adapters/abstract_adapter'
|
3
3
|
require "database_cleaner/generic/truncation"
|
4
|
-
require 'database_cleaner/active_record/base'
|
5
4
|
require 'database_cleaner/active_record/truncation'
|
6
5
|
|
7
6
|
module DatabaseCleaner
|
@@ -53,30 +52,31 @@ module DatabaseCleaner::ActiveRecord
|
|
53
52
|
end
|
54
53
|
|
55
54
|
def tables_with_new_rows(connection)
|
56
|
-
|
57
|
-
stats = table_stats_query(connection, @db_name)
|
55
|
+
stats = table_stats_query(connection)
|
58
56
|
if stats != ''
|
59
|
-
connection.
|
57
|
+
connection.select_values(stats)
|
60
58
|
else
|
61
59
|
[]
|
62
60
|
end
|
63
61
|
end
|
64
62
|
|
65
|
-
def table_stats_query(connection
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
63
|
+
def table_stats_query(connection)
|
64
|
+
@table_stats_query ||= build_table_stats_query(connection)
|
65
|
+
ensure
|
66
|
+
@table_stats_query = nil unless @cache_tables
|
67
|
+
end
|
68
|
+
|
69
|
+
def build_table_stats_query(connection)
|
70
|
+
tables = connection.select_values(<<-SQL)
|
71
|
+
SELECT table_name
|
72
|
+
FROM information_schema.tables
|
73
|
+
WHERE table_schema = database()
|
74
|
+
AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
|
75
|
+
SQL
|
76
|
+
queries = tables.map do |table|
|
77
|
+
"(SELECT #{connection.quote(table)} FROM #{connection.quote_table_name(table)} LIMIT 1)"
|
79
78
|
end
|
79
|
+
queries.join(' UNION ALL ')
|
80
80
|
end
|
81
81
|
|
82
82
|
def information_schema_exists? connection
|
File without changes
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'active_record/base'
|
2
|
-
|
2
|
+
require 'database_cleaner/active_record/base'
|
3
3
|
require 'active_record/connection_adapters/abstract_adapter'
|
4
4
|
|
5
5
|
#Load available connection adapters
|
6
6
|
%w(
|
7
|
-
abstract_mysql_adapter postgresql_adapter sqlite3_adapter mysql_adapter mysql2_adapter
|
7
|
+
abstract_mysql_adapter postgresql_adapter sqlite3_adapter mysql_adapter mysql2_adapter oracle_enhanced_adapter
|
8
8
|
).each do |known_adapter|
|
9
9
|
begin
|
10
10
|
require "active_record/connection_adapters/#{known_adapter}"
|
@@ -27,7 +27,11 @@ module DatabaseCleaner
|
|
27
27
|
|
28
28
|
def database_cleaner_table_cache
|
29
29
|
# the adapters don't do caching (#130) but we make the assumption that the list stays the same in tests
|
30
|
-
@database_cleaner_tables ||=
|
30
|
+
@database_cleaner_tables ||= database_tables
|
31
|
+
end
|
32
|
+
|
33
|
+
def database_tables
|
34
|
+
::ActiveRecord::VERSION::MAJOR >= 5 ? data_sources : tables
|
31
35
|
end
|
32
36
|
|
33
37
|
def truncate_table(table_name)
|
@@ -63,22 +67,21 @@ module DatabaseCleaner
|
|
63
67
|
select_value("SELECT EXISTS (SELECT 1 FROM #{quote_table_name(table)} LIMIT 1)").to_i
|
64
68
|
end
|
65
69
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
# was ever inserted into.
|
70
|
-
def has_been_used?(table)
|
71
|
-
if has_rows?(table)
|
72
|
-
true
|
73
|
-
else
|
74
|
-
# Patch for MysqlAdapter with ActiveRecord 3.2.7 later
|
75
|
-
# select_value("SELECT 1") #=> "1"
|
76
|
-
select_value(<<-SQL).to_i > 1 # returns nil if not present
|
77
|
-
SELECT Auto_increment
|
70
|
+
def auto_increment_value(table)
|
71
|
+
select_value(<<-SQL).to_i
|
72
|
+
SELECT auto_increment
|
78
73
|
FROM information_schema.tables
|
79
|
-
WHERE table_name='#{table}'
|
80
|
-
|
81
|
-
|
74
|
+
WHERE table_name = '#{table}'
|
75
|
+
AND table_schema = database()
|
76
|
+
SQL
|
77
|
+
end
|
78
|
+
|
79
|
+
# This method tells us if the given table has been inserted into since its
|
80
|
+
# last truncation. Note that the table might have been populated, which
|
81
|
+
# increased the auto-increment counter, but then cleaned again such that
|
82
|
+
# it appears empty now.
|
83
|
+
def has_been_used?(table)
|
84
|
+
has_rows?(table) || auto_increment_value(table) > 1
|
82
85
|
end
|
83
86
|
|
84
87
|
def has_rows?(table)
|
@@ -244,7 +247,7 @@ module DatabaseCleaner::ActiveRecord
|
|
244
247
|
private
|
245
248
|
|
246
249
|
def tables_to_truncate(connection)
|
247
|
-
tables_in_db = cache_tables? ? connection.database_cleaner_table_cache : connection.
|
250
|
+
tables_in_db = cache_tables? ? connection.database_cleaner_table_cache : connection.database_tables
|
248
251
|
to_reject = (@tables_to_exclude + connection.database_cleaner_view_cache)
|
249
252
|
(@only || tables_in_db).reject do |table|
|
250
253
|
if ( m = table.match(/([^.]+)$/) )
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/couch_potato"
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "database_cleaner/couch_potato/version"
|
2
|
+
require "database_cleaner"
|
3
|
+
require "database_cleaner/couch_potato/base"
|
4
|
+
require "database_cleaner/couch_potato/truncation"
|
5
|
+
|
6
|
+
module DatabaseCleaner::CouchPotato
|
7
|
+
def self.default_strategy
|
8
|
+
:truncation
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
data/{lib → adapters/database_cleaner-couch_potato/lib}/database_cleaner/couch_potato/base.rb
RENAMED
File without changes
|
data/{lib → adapters/database_cleaner-couch_potato/lib}/database_cleaner/couch_potato/truncation.rb
RENAMED
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/data_mapper"
|
data/{lib → adapters/database_cleaner-data_mapper/lib}/database_cleaner/data_mapper/transaction.rb
RENAMED
File without changes
|
data/{lib → adapters/database_cleaner-data_mapper/lib}/database_cleaner/data_mapper/truncation.rb
RENAMED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "database_cleaner/generic/truncation"
|
2
2
|
require 'database_cleaner/data_mapper/base'
|
3
|
+
require 'data_mapper'
|
3
4
|
|
4
5
|
module DataMapper
|
5
6
|
module Adapters
|
@@ -20,7 +21,7 @@ module DataMapper
|
|
20
21
|
|
21
22
|
class MysqlAdapter < DataObjectsAdapter
|
22
23
|
|
23
|
-
# taken from
|
24
|
+
# taken from https://github.com/godfat/dm-mapping/tree/master
|
24
25
|
def storage_names(repository = :default)
|
25
26
|
select 'SHOW TABLES'
|
26
27
|
end
|
@@ -44,7 +45,7 @@ module DataMapper
|
|
44
45
|
|
45
46
|
module SqliteAdapterMethods
|
46
47
|
|
47
|
-
# taken from
|
48
|
+
# taken from https://github.com/godfat/dm-mapping/tree/master
|
48
49
|
def storage_names(repository = :default)
|
49
50
|
# activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 177
|
50
51
|
sql = <<-SQL
|
@@ -92,7 +93,7 @@ module DataMapper
|
|
92
93
|
# maybe codes below gets some postgres/datamapper user going, though.
|
93
94
|
class PostgresAdapter < DataObjectsAdapter
|
94
95
|
|
95
|
-
# taken from
|
96
|
+
# taken from https://github.com/godfat/dm-mapping/tree/master
|
96
97
|
def storage_names(repository = :default)
|
97
98
|
sql = <<-SQL
|
98
99
|
SELECT table_name FROM "information_schema"."tables"
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/mongo"
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/mongo_mapper"
|
data/{lib → adapters/database_cleaner-mongo_mapper/lib}/database_cleaner/mongo_mapper/truncation.rb
RENAMED
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/mongoid"
|
data/{lib → adapters/database_cleaner-mongoid/lib}/database_cleaner/mongo2/truncation_mixin.rb
RENAMED
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/moped"
|
File without changes
|
@@ -13,6 +13,7 @@ module DatabaseCleaner
|
|
13
13
|
else
|
14
14
|
collections.each { |c| session[c].find.remove_all unless @tables_to_exclude.include?(c) }
|
15
15
|
end
|
16
|
+
wait_for_removals_to_finish
|
16
17
|
true
|
17
18
|
end
|
18
19
|
|
@@ -35,6 +36,9 @@ module DatabaseCleaner
|
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
39
|
+
def wait_for_removals_to_finish
|
40
|
+
session.command(getlasterror: 1)
|
41
|
+
end
|
38
42
|
end
|
39
43
|
end
|
40
44
|
end
|