database_cleaner 1.7.0 → 1.8.5
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 +45 -1
- data/README.markdown +74 -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 +25 -20
- 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 +2 -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/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/truncation.rb +35 -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/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/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongo1_truncation_mixin.rb +26 -0
- data/{lib/database_cleaner/mongo2/truncation_mixin.rb → adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongo2_truncation_mixin.rb} +3 -4
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongoid_truncation_mixin.rb +65 -0
- data/{lib → adapters/database_cleaner-mongoid/lib}/database_cleaner/mongoid/truncation.rb +6 -6
- 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 +12 -0
- data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm/truncation.rb +24 -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 +92 -105
- data/lib/database_cleaner/configuration.rb +87 -83
- data/lib/database_cleaner/deprecation.rb +26 -0
- data/lib/database_cleaner/null_strategy.rb +6 -6
- data/lib/database_cleaner/orm_autodetector.rb +41 -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 -44
- data/VERSION.yml +0 -4
- data/lib/database_cleaner/mongo2/base.rb +0 -16
- data/lib/database_cleaner/mongo_mapper/truncation.rb +0 -19
- data/lib/database_cleaner/ohm/truncation.rb +0 -15
File without changes
|
data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongo1_truncation_mixin.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module DatabaseCleaner
|
2
|
+
module Mongoid
|
3
|
+
module Mongo1TruncationMixin
|
4
|
+
def clean
|
5
|
+
if @only
|
6
|
+
collections.each { |c| c.send(truncate_method_name) if @only.include?(c.name) }
|
7
|
+
else
|
8
|
+
collections.each { |c| c.send(truncate_method_name) unless @tables_to_exclude.include?(c.name) }
|
9
|
+
end
|
10
|
+
true
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def collections
|
16
|
+
database.collections.select { |c| c.name !~ /^system\./ }
|
17
|
+
end
|
18
|
+
|
19
|
+
def truncate_method_name
|
20
|
+
# This constant only exists in the 2.x series.
|
21
|
+
defined?(::Mongo::VERSION) ? :delete_many : :remove
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module DatabaseCleaner
|
2
|
-
module
|
3
|
-
module
|
4
|
-
|
2
|
+
module Mongoid
|
3
|
+
module Mongo2TruncationMixin
|
5
4
|
def clean
|
6
5
|
if @only
|
7
6
|
collections.each { |c| database[c].find.delete_many if @only.include?(c) }
|
@@ -33,7 +32,7 @@ module DatabaseCleaner
|
|
33
32
|
# name
|
34
33
|
# end
|
35
34
|
end
|
36
|
-
|
37
35
|
end
|
38
36
|
end
|
39
37
|
end
|
38
|
+
|
data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongoid_truncation_mixin.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'database_cleaner/generic/base'
|
2
|
+
require 'database_cleaner/generic/truncation'
|
3
|
+
|
4
|
+
module DatabaseCleaner
|
5
|
+
module Mongoid
|
6
|
+
module MongoidTruncationMixin
|
7
|
+
include ::DatabaseCleaner::Generic::Base
|
8
|
+
include ::DatabaseCleaner::Generic::Truncation
|
9
|
+
|
10
|
+
def db=(desired_db)
|
11
|
+
@db = desired_db
|
12
|
+
end
|
13
|
+
|
14
|
+
def db
|
15
|
+
@db ||= :default
|
16
|
+
end
|
17
|
+
|
18
|
+
def host_port=(desired_host)
|
19
|
+
@host = desired_host
|
20
|
+
end
|
21
|
+
|
22
|
+
def host
|
23
|
+
@host ||= '127.0.0.1:27017'
|
24
|
+
end
|
25
|
+
|
26
|
+
def db_version
|
27
|
+
@db_version ||= session.command('buildinfo' => 1)['version']
|
28
|
+
end
|
29
|
+
|
30
|
+
def clean
|
31
|
+
if @only
|
32
|
+
collections.each { |c| session[c].find.remove_all if @only.include?(c) }
|
33
|
+
else
|
34
|
+
collections.each { |c| session[c].find.remove_all unless @tables_to_exclude.include?(c) }
|
35
|
+
end
|
36
|
+
wait_for_removals_to_finish
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def collections
|
43
|
+
if db != :default
|
44
|
+
session.use(db)
|
45
|
+
end
|
46
|
+
|
47
|
+
if db_version.split('.').first.to_i >= 3
|
48
|
+
session.command(listCollections: 1, filter: { 'name' => { '$not' => /.?system\.|\$/ } })['cursor']['firstBatch'].map do |collection|
|
49
|
+
collection['name']
|
50
|
+
end
|
51
|
+
else
|
52
|
+
session['system.namespaces'].find(name: { '$not' => /\.system\.|\$/ }).to_a.map do |collection|
|
53
|
+
_, name = collection['name'].split('.', 2)
|
54
|
+
name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def wait_for_removals_to_finish
|
60
|
+
session.command(getlasterror: 1)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'database_cleaner/mongoid/base'
|
2
2
|
require 'database_cleaner/generic/truncation'
|
3
|
-
require 'database_cleaner/
|
4
|
-
require 'database_cleaner/
|
5
|
-
require 'database_cleaner/
|
3
|
+
require 'database_cleaner/mongoid/mongo1_truncation_mixin'
|
4
|
+
require 'database_cleaner/mongoid/mongo2_truncation_mixin'
|
5
|
+
require 'database_cleaner/mongoid/mongoid_truncation_mixin'
|
6
6
|
require 'mongoid/version'
|
7
7
|
|
8
8
|
module DatabaseCleaner
|
@@ -13,7 +13,7 @@ module DatabaseCleaner
|
|
13
13
|
|
14
14
|
if ::Mongoid::VERSION < '3'
|
15
15
|
|
16
|
-
include ::DatabaseCleaner::
|
16
|
+
include ::DatabaseCleaner::Mongoid::Mongo1TruncationMixin
|
17
17
|
|
18
18
|
private
|
19
19
|
|
@@ -23,7 +23,7 @@ module DatabaseCleaner
|
|
23
23
|
|
24
24
|
elsif ::Mongoid::VERSION < '5'
|
25
25
|
|
26
|
-
include ::DatabaseCleaner::
|
26
|
+
include ::DatabaseCleaner::Mongoid::MongoidTruncationMixin
|
27
27
|
|
28
28
|
private
|
29
29
|
|
@@ -41,7 +41,7 @@ module DatabaseCleaner
|
|
41
41
|
|
42
42
|
else
|
43
43
|
|
44
|
-
include ::DatabaseCleaner::
|
44
|
+
include ::DatabaseCleaner::Mongoid::Mongo2TruncationMixin
|
45
45
|
|
46
46
|
end
|
47
47
|
end
|
@@ -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
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/neo4j"
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/ohm"
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "database_cleaner/ohm/version"
|
2
|
+
require "database_cleaner"
|
3
|
+
require "database_cleaner/ohm/truncation"
|
4
|
+
|
5
|
+
DatabaseCleaner.deprecate "The Ohm adapter for DatabaseCleaner is deprecated, and will be removed in v2.0. Please use the Redis adapter instead."
|
6
|
+
|
7
|
+
module DatabaseCleaner::Ohm
|
8
|
+
def self.default_strategy
|
9
|
+
:truncation
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
begin # when database_cleaner-ohm is loaded as a gem
|
2
|
+
require 'database_cleaner/redis/truncation'
|
3
|
+
rescue LoadError # when database_cleaner is loaded as a gem
|
4
|
+
$LOAD_PATH.unshift File.expand_path("#{File.dirname(__FILE__)}/../../../../../adapters/database_cleaner-redis/lib")
|
5
|
+
require 'database_cleaner/redis/truncation'
|
6
|
+
end
|
7
|
+
|
8
|
+
module DatabaseCleaner
|
9
|
+
module Ohm
|
10
|
+
def self.available_strategies
|
11
|
+
%w(truncation)
|
12
|
+
end
|
13
|
+
|
14
|
+
class Truncation < ::DatabaseCleaner::Redis::Truncation
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def default_redis
|
19
|
+
::Ohm.redis
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/redis"
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
require "database_cleaner/sequel"
|
File without changes
|
File without changes
|
@@ -14,39 +14,40 @@ module DatabaseCleaner
|
|
14
14
|
def clean
|
15
15
|
return unless dirty?
|
16
16
|
|
17
|
+
tables = tables_to_truncate(db)
|
18
|
+
|
19
|
+
# Count rows before truncating
|
20
|
+
if pre_count?
|
21
|
+
tables = pre_count_tables(tables)
|
22
|
+
end
|
23
|
+
|
17
24
|
case db.database_type
|
18
25
|
when :postgres
|
19
26
|
# PostgreSQL requires all tables with FKs to be truncates in the same command, or have the CASCADE keyword
|
20
27
|
# appended. Bulk truncation without CASCADE is:
|
21
28
|
# * Safer. Tables outside of tables_to_truncate won't be affected.
|
22
29
|
# * Faster. Less roundtrips to the db.
|
23
|
-
unless
|
24
|
-
|
25
|
-
db.run "TRUNCATE TABLE #{
|
30
|
+
unless tables.empty?
|
31
|
+
tables_sql = tables.map { |t| %("#{t}") }.join(',')
|
32
|
+
db.run "TRUNCATE TABLE #{tables_sql} RESTART IDENTITY;"
|
26
33
|
end
|
27
34
|
else
|
28
|
-
|
29
|
-
|
30
|
-
if pre_count?
|
31
|
-
# Count rows before truncating
|
32
|
-
pre_count_truncate_tables(db, tables)
|
33
|
-
else
|
34
|
-
# Truncate each table normally
|
35
|
-
truncate_tables(db, tables)
|
36
|
-
end
|
35
|
+
truncate_tables(db, tables)
|
37
36
|
end
|
38
37
|
end
|
39
38
|
|
40
39
|
private
|
41
40
|
|
42
|
-
def
|
43
|
-
tables
|
44
|
-
truncate_tables(db, tables)
|
41
|
+
def pre_count_tables tables
|
42
|
+
tables.reject { |table| db[table.to_sym].count == 0 }
|
45
43
|
end
|
46
44
|
|
47
45
|
def truncate_tables(db, tables)
|
48
46
|
tables.each do |table|
|
49
47
|
db[table.to_sym].truncate
|
48
|
+
if db.database_type == :sqlite && db.table_exists?(:sqlite_sequence)
|
49
|
+
db[:sqlite_sequence].where(name: table).delete
|
50
|
+
end
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
data/lib/database_cleaner.rb
CHANGED
@@ -1,12 +1,47 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
|
2
|
+
require 'database_cleaner/version'
|
2
3
|
require 'database_cleaner/configuration'
|
4
|
+
require 'database_cleaner/deprecation'
|
5
|
+
require 'forwardable'
|
3
6
|
|
4
7
|
module DatabaseCleaner
|
5
8
|
class << self
|
6
|
-
|
9
|
+
extend Forwardable
|
10
|
+
delegate [
|
11
|
+
:[],
|
12
|
+
:app_root=,
|
13
|
+
:app_root,
|
14
|
+
:logger=,
|
15
|
+
:logger,
|
16
|
+
:cleaners,
|
17
|
+
:cleaners=,
|
18
|
+
:strategy=,
|
19
|
+
:orm=,
|
20
|
+
:start,
|
21
|
+
:clean,
|
22
|
+
:clean_with,
|
23
|
+
:cleaning,
|
24
|
+
|
25
|
+
# TODO remove in 2.0
|
26
|
+
:clean!,
|
27
|
+
:clean_with!,
|
28
|
+
:init_cleaners,
|
29
|
+
:add_cleaner,
|
30
|
+
:connections,
|
31
|
+
:remove_duplicates,
|
32
|
+
] => :configuration
|
33
|
+
|
34
|
+
attr_accessor :allow_remote_database_url, :allow_production, :url_whitelist
|
7
35
|
|
8
36
|
def can_detect_orm?
|
37
|
+
DatabaseCleaner.deprecate "Calling `DatabaseCleaner.can_detect_orm?` is deprecated, and will be removed in database_cleaner 2.0 with no replacement."
|
9
38
|
DatabaseCleaner::Base.autodetect_orm
|
10
39
|
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def configuration
|
44
|
+
@configuration ||= Configuration.new
|
45
|
+
end
|
11
46
|
end
|
12
47
|
end
|