database_cleaner-active_record 1.8.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.travis.yml +15 -3
- data/Appraisals +15 -0
- data/Gemfile +9 -4
- data/README.md +36 -43
- data/bin/setup +0 -1
- data/database_cleaner-active_record.gemspec +11 -20
- data/gemfiles/.bundle/config +2 -0
- data/gemfiles/rails_5.1.gemfile +14 -0
- data/gemfiles/rails_5.2.gemfile +14 -0
- data/gemfiles/rails_6.0.gemfile +14 -0
- data/gemfiles/rails_6.1.gemfile +14 -0
- data/lib/database_cleaner/active_record.rb +3 -2
- data/lib/database_cleaner/active_record/base.rb +45 -55
- data/lib/database_cleaner/active_record/deletion.rb +49 -86
- data/lib/database_cleaner/active_record/transaction.rb +10 -47
- data/lib/database_cleaner/active_record/truncation.rb +185 -214
- data/lib/database_cleaner/active_record/version.rb +1 -1
- metadata +31 -39
- data/Gemfile.lock +0 -73
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2c9c7514dc1bea223af68a8a3822b1fb07e227911897166432f6018ab5b23a9
|
4
|
+
data.tar.gz: b3217f7ad1128617b17be1872207df59317204aa7912bca0a00e0aab77490d15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74e5baa1c2eb6de6b39213d9d192dcb649ff069d8ac5a9ad3d9347beb40b68a15c29a16149e2338098d246db3fdb6a7a22baef9911f4be5fb1954560d87e2b81
|
7
|
+
data.tar.gz: 991af4358084057df8efd4a05d7fa0eb75756ee8f940a7f2bd2e2da2a65a64a1bd96f668c355948e16b04c33b407276d2076f5bec5b47631e6682dd0a5221408
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,5 +1,17 @@
|
|
1
|
-
sudo: false
|
2
1
|
language: ruby
|
2
|
+
services:
|
3
|
+
- mysql
|
4
|
+
- postgresql
|
3
5
|
rvm:
|
4
|
-
- 2.
|
5
|
-
|
6
|
+
- 2.5
|
7
|
+
- 2.6
|
8
|
+
- 2.7
|
9
|
+
gemfile:
|
10
|
+
- gemfiles/rails_5.1.gemfile
|
11
|
+
- gemfiles/rails_5.2.gemfile
|
12
|
+
- gemfiles/rails_6.0.gemfile
|
13
|
+
- gemfiles/rails_6.1.gemfile
|
14
|
+
before_install:
|
15
|
+
- bin/setup
|
16
|
+
cache:
|
17
|
+
bundler: true
|
data/Appraisals
ADDED
data/Gemfile
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
2
|
|
3
|
-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
|
-
|
5
|
-
# Specify your gem's dependencies in database_cleaner-active_record.gemspec
|
6
3
|
gemspec
|
7
4
|
|
8
|
-
gem "database_cleaner",
|
5
|
+
gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
|
6
|
+
|
7
|
+
gem "rails", "~>5.2"
|
8
|
+
gem "byebug"
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem "simplecov", require: false
|
12
|
+
gem "codecov", require: false
|
13
|
+
end
|
data/README.md
CHANGED
@@ -2,11 +2,14 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner-active_record.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner-active_record)
|
4
4
|
[![Code Climate](https://codeclimate.com/github/DatabaseCleaner/database_cleaner-active_record/badges/gpa.svg)](https://codeclimate.com/github/DatabaseCleaner/database_cleaner-active_record)
|
5
|
+
[![codecov](https://codecov.io/gh/DatabaseCleaner/database_cleaner-active_record/branch/master/graph/badge.svg)](https://codecov.io/gh/DatabaseCleaner/database_cleaner-active_record)
|
5
6
|
|
6
7
|
Clean your ActiveRecord databases with Database Cleaner.
|
7
8
|
|
8
9
|
See https://github.com/DatabaseCleaner/database_cleaner for more information.
|
9
10
|
|
11
|
+
For support or to discuss development please use the [Google Group](https://groups.google.com/group/database_cleaner).
|
12
|
+
|
10
13
|
## Installation
|
11
14
|
|
12
15
|
```ruby
|
@@ -18,26 +21,11 @@ end
|
|
18
21
|
|
19
22
|
## Supported Strategies
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
<table>
|
24
|
-
<tbody>
|
25
|
-
<tr>
|
26
|
-
<th>Truncation</th>
|
27
|
-
<th>Transaction</th>
|
28
|
-
<th>Deletion</th>
|
29
|
-
</tr>
|
30
|
-
<tr>
|
31
|
-
<td> Yes</td>
|
32
|
-
<td> <b>Yes</b></td>
|
33
|
-
<td> Yes</td>
|
34
|
-
</tr>
|
35
|
-
</tbody>
|
36
|
-
</table>
|
37
|
-
|
38
|
-
(Default strategy is denoted in bold)
|
24
|
+
Three strategies are supported:
|
39
25
|
|
40
|
-
|
26
|
+
* Transaction (default)
|
27
|
+
* Truncation
|
28
|
+
* Deletion
|
41
29
|
|
42
30
|
## What strategy is fastest?
|
43
31
|
|
@@ -53,36 +41,41 @@ So what is fastest out of `:deletion` and `:truncation`? Well, it depends on you
|
|
53
41
|
|
54
42
|
Some people report much faster speeds with `:deletion` while others say `:truncation` is faster for them. The best approach therefore is it try all options on your test suite and see what is faster.
|
55
43
|
|
56
|
-
|
44
|
+
## Strategy configuration options
|
57
45
|
|
58
|
-
|
46
|
+
The transaction strategy accepts no options.
|
59
47
|
|
60
|
-
|
61
|
-
<tbody>
|
62
|
-
<tr>
|
63
|
-
<th>ORM</th>
|
64
|
-
<th>How to access</th>
|
65
|
-
<th>Notes</th>
|
66
|
-
</tr>
|
67
|
-
<tr>
|
68
|
-
<td> Active Record </td>
|
69
|
-
<td> <code>DatabaseCleaner[:active_record]</code></td>
|
70
|
-
<td> Connection specified as <code>:symbol</code> keys, loaded from <code>config/database.yml</code>. You may also pass in the ActiveRecord model under the <code>:model</code> key.</td>
|
71
|
-
</tr>
|
72
|
-
</tbody>
|
73
|
-
</table>
|
48
|
+
The truncation and deletion strategies may accept the following options:
|
74
49
|
|
75
|
-
|
50
|
+
* `:only` and `:except` may take a list of table names:
|
76
51
|
|
77
|
-
|
52
|
+
```ruby
|
53
|
+
# Only truncate the "users" table.
|
54
|
+
DatabaseCleaner[:active_record].strategy = :truncation, { only: ["users"] }
|
78
55
|
|
79
|
-
|
80
|
-
|
56
|
+
# Delete all tables except the "users" table.
|
57
|
+
DatabaseCleaner[:active_record].strategy = :deletion, { except: ["users"] }
|
58
|
+
```
|
81
59
|
|
82
|
-
|
60
|
+
* `:pre_count` - When set to `true` this will check each table for existing rows before truncating or deleting it. This can speed up test suites when many of the tables are never populated. Defaults to `false`. (Also, see the section on [What strategy is fastest?](#what-strategy-is-fastest))
|
83
61
|
|
84
62
|
* `:cache_tables` - When set to `true` the list of tables to truncate or delete from will only be read from the DB once, otherwise it will be read before each cleanup run. Set this to `false` if (1) you create and drop tables in your tests, or (2) you change Postgres schemas (`ActiveRecord::Base.connection.schema_search_path`) in your tests (for example, in a multitenancy setup with each tenant in a different Postgres schema). Defaults to `true`.
|
85
63
|
|
64
|
+
## Adapter configuration options
|
65
|
+
|
66
|
+
`#db` defaults to the default ActiveRecord database, but can be specified manually in a few ways:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# ActiveRecord connection key
|
70
|
+
DatabaseCleaner[:active_record].db = :logs
|
71
|
+
|
72
|
+
# Back to default:
|
73
|
+
DatabaseCleaner[:active_record].db = :default
|
74
|
+
|
75
|
+
# Multiple databases can be specified:
|
76
|
+
DatabaseCleaner[:active_record, db: :default]
|
77
|
+
DatabaseCleaner[:active_record, db: :logs]
|
78
|
+
```
|
86
79
|
|
87
80
|
## Common Errors
|
88
81
|
|
@@ -92,11 +85,11 @@ If you are using Postgres and have foreign key constraints, the truncation strat
|
|
92
85
|
|
93
86
|
To silence these warnings set the following log level in your `postgresql.conf` file:
|
94
87
|
|
95
|
-
```
|
88
|
+
```
|
96
89
|
client_min_messages = warning
|
97
90
|
```
|
98
91
|
|
99
|
-
|
92
|
+
You can also add this parameter to your database.yml file:
|
100
93
|
|
101
94
|
<pre>
|
102
95
|
test:
|
@@ -107,4 +100,4 @@ test:
|
|
107
100
|
|
108
101
|
## COPYRIGHT
|
109
102
|
|
110
|
-
See [LICENSE] for details.
|
103
|
+
See [LICENSE](LICENSE) for details.
|
data/bin/setup
CHANGED
@@ -1,12 +1,9 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "database_cleaner/active_record/version"
|
1
|
+
require_relative "./lib/database_cleaner/active_record/version"
|
5
2
|
|
6
3
|
Gem::Specification.new do |spec|
|
7
4
|
spec.name = "database_cleaner-active_record"
|
8
5
|
spec.version = DatabaseCleaner::ActiveRecord::VERSION
|
9
|
-
spec.authors = ["Ernesto Tagwerker"]
|
6
|
+
spec.authors = ["Ernesto Tagwerker", "Micah Geisel"]
|
10
7
|
spec.email = ["ernesto@ombulabs.com"]
|
11
8
|
|
12
9
|
spec.summary = "Strategies for cleaning databases using ActiveRecord. Can be used to ensure a clean state for testing."
|
@@ -21,20 +18,14 @@ Gem::Specification.new do |spec|
|
|
21
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
19
|
spec.require_paths = ["lib"]
|
23
20
|
|
24
|
-
spec.add_dependency "database_cleaner", "~>
|
25
|
-
spec.add_dependency "activerecord"
|
26
|
-
|
27
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
-
spec.add_development_dependency "bundler", "~> 1.16"
|
29
|
-
spec.add_development_dependency "rspec", "~> 3.0"
|
21
|
+
spec.add_dependency "database_cleaner-core", "~>2.0.0"
|
22
|
+
spec.add_dependency "activerecord", ">= 5.a"
|
30
23
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
spec.add_development_dependency "activerecord-jdbc-adapter"
|
39
|
-
end
|
24
|
+
spec.add_development_dependency "bundler"
|
25
|
+
spec.add_development_dependency "appraisal"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
spec.add_development_dependency "mysql2"
|
29
|
+
spec.add_development_dependency "pg"
|
30
|
+
spec.add_development_dependency "sqlite3"
|
40
31
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
|
6
|
+
gem "rails", "~>5.1.0"
|
7
|
+
gem "byebug"
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem "simplecov", require: false
|
11
|
+
gem "codecov", require: false
|
12
|
+
end
|
13
|
+
|
14
|
+
gemspec path: "../"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
|
6
|
+
gem "rails", "~>5.2.0"
|
7
|
+
gem "byebug"
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem "simplecov", require: false
|
11
|
+
gem "codecov", require: false
|
12
|
+
end
|
13
|
+
|
14
|
+
gemspec path: "../"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
|
6
|
+
gem "rails", "~>6.0.0"
|
7
|
+
gem "byebug"
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem "simplecov", require: false
|
11
|
+
gem "codecov", require: false
|
12
|
+
end
|
13
|
+
|
14
|
+
gemspec path: "../"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
|
6
|
+
gem "rails", "~>6.1.0"
|
7
|
+
gem "byebug"
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem "simplecov", require: false
|
11
|
+
gem "codecov", require: false
|
12
|
+
end
|
13
|
+
|
14
|
+
gemspec path: "../"
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'database_cleaner/active_record/version'
|
2
|
-
require 'database_cleaner'
|
3
|
-
require 'database_cleaner/active_record/deletion'
|
2
|
+
require 'database_cleaner/core'
|
4
3
|
require 'database_cleaner/active_record/transaction'
|
5
4
|
require 'database_cleaner/active_record/truncation'
|
5
|
+
require 'database_cleaner/active_record/deletion'
|
6
6
|
|
7
|
+
DatabaseCleaner[:active_record].strategy = :transaction
|
@@ -1,94 +1,76 @@
|
|
1
1
|
require 'active_record'
|
2
|
-
require 'database_cleaner/
|
2
|
+
require 'database_cleaner/strategy'
|
3
3
|
require 'erb'
|
4
4
|
require 'yaml'
|
5
5
|
|
6
6
|
module DatabaseCleaner
|
7
7
|
module ActiveRecord
|
8
|
-
def self.available_strategies
|
9
|
-
%w[truncation transaction deletion]
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.default_strategy
|
13
|
-
:transaction
|
14
|
-
end
|
15
|
-
|
16
8
|
def self.config_file_location=(path)
|
17
9
|
@config_file_location = path
|
18
10
|
end
|
19
11
|
|
20
12
|
def self.config_file_location
|
21
|
-
@config_file_location ||=
|
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
|
13
|
+
@config_file_location ||= "#{Dir.pwd}/config/database.yml"
|
27
14
|
end
|
28
15
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
attr_accessor :connection_hash
|
33
|
-
|
34
|
-
def db=(desired_db)
|
35
|
-
@db = desired_db
|
36
|
-
load_config
|
16
|
+
class Base < DatabaseCleaner::Strategy
|
17
|
+
def self.migration_table_name
|
18
|
+
::ActiveRecord::SchemaMigration.table_name
|
37
19
|
end
|
38
20
|
|
39
|
-
def
|
40
|
-
|
21
|
+
def self.exclusion_condition(column_name)
|
22
|
+
<<~SQL
|
23
|
+
#{column_name} <> '#{DatabaseCleaner::ActiveRecord::Base.migration_table_name}'
|
24
|
+
AND #{column_name} <> '#{::ActiveRecord::Base.internal_metadata_table_name}'
|
25
|
+
SQL
|
41
26
|
end
|
42
27
|
|
43
|
-
def
|
44
|
-
|
45
|
-
|
46
|
-
@connection_hash = valid_config(connection_details)[self.db.to_s]
|
47
|
-
end
|
28
|
+
def db=(*)
|
29
|
+
super
|
30
|
+
load_config
|
48
31
|
end
|
49
32
|
|
50
|
-
|
51
|
-
if !::ActiveRecord::Base.configurations.nil? && !::ActiveRecord::Base.configurations.empty?
|
52
|
-
if connection_file != ::ActiveRecord::Base.configurations
|
53
|
-
return ::ActiveRecord::Base.configurations
|
54
|
-
end
|
55
|
-
end
|
56
|
-
connection_file
|
57
|
-
end
|
33
|
+
attr_accessor :connection_hash
|
58
34
|
|
59
35
|
def connection_class
|
60
36
|
@connection_class ||= if db && !db.is_a?(Symbol)
|
61
37
|
db
|
62
38
|
elsif connection_hash
|
63
|
-
lookup_from_connection_pool || establish_connection
|
39
|
+
(lookup_from_connection_pool rescue nil) || establish_connection
|
64
40
|
else
|
65
41
|
::ActiveRecord::Base
|
66
42
|
end
|
67
43
|
end
|
68
44
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
::ActiveRecord
|
45
|
+
private
|
46
|
+
|
47
|
+
def load_config
|
48
|
+
if db != :default && db.is_a?(Symbol) && File.file?(DatabaseCleaner::ActiveRecord.config_file_location)
|
49
|
+
connection_details = YAML::load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result)
|
50
|
+
@connection_hash = valid_config(connection_details, db.to_s)
|
74
51
|
end
|
75
52
|
end
|
76
53
|
|
77
|
-
def
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
end
|
82
|
-
result
|
54
|
+
def valid_config(connection_file, db)
|
55
|
+
return connection_file[db] unless (active_record_config_hash = active_record_config_hash_for(db))
|
56
|
+
|
57
|
+
active_record_config_hash
|
83
58
|
end
|
84
59
|
|
85
|
-
|
60
|
+
def active_record_config_hash_for(db)
|
61
|
+
if ::ActiveRecord.version >= Gem::Version.new('6.1')
|
62
|
+
::ActiveRecord::Base.configurations&.configs_for(name: db)&.configuration_hash
|
63
|
+
else
|
64
|
+
::ActiveRecord::Base.configurations[db]
|
65
|
+
end
|
66
|
+
end
|
86
67
|
|
87
68
|
def lookup_from_connection_pool
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
69
|
+
return unless ::ActiveRecord::Base.respond_to?(:descendants)
|
70
|
+
|
71
|
+
database_name = connection_hash['database'] || connection_hash[:database]
|
72
|
+
::ActiveRecord::Base.descendants.select(&:connection_pool).detect do |model|
|
73
|
+
database_for(model) == database_name
|
92
74
|
end
|
93
75
|
end
|
94
76
|
|
@@ -96,6 +78,14 @@ module DatabaseCleaner
|
|
96
78
|
::ActiveRecord::Base.establish_connection(connection_hash)
|
97
79
|
::ActiveRecord::Base
|
98
80
|
end
|
81
|
+
|
82
|
+
def database_for(model)
|
83
|
+
if model.connection_pool.respond_to?(:db_config) # ActiveRecord >= 6.1
|
84
|
+
model.connection_pool.db_config.configuration_hash[:database]
|
85
|
+
else
|
86
|
+
model.connection_pool.spec.config[:database]
|
87
|
+
end
|
88
|
+
end
|
99
89
|
end
|
100
90
|
end
|
101
91
|
end
|