database_cleaner-active_record 1.99.0 → 2.0.1
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 +23 -2
- 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/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 +184 -223
- data/lib/database_cleaner/active_record/version.rb +1 -1
- data/lib/database_cleaner/active_record.rb +3 -2
- 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: 0f4e22827466f77ef287a8cfa7cf75078940f69f4e3b222676d7cb30d74edb74
|
4
|
+
data.tar.gz: 942936724f8ef0e4a0107044aef35b203e6e8d23c1a6d241d452342eb1d8ae17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8116e18c0f97636ab92457c6025ef7978547aba2553c747e8120b7ac528fe583f6f1cc76cbc78385c2a26fa6a6a348d37c95c5440186404259a4d50b39200b9d
|
7
|
+
data.tar.gz: e847363c1be911931358c9b1cb3f09717e302b2e89aa609ce366baa1ec6664fbf3935b4c52b6785cfac25f36794992a118edc9846774a485e26fa7aeaa27896e
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,4 +1,25 @@
|
|
1
1
|
language: ruby
|
2
|
+
services:
|
3
|
+
- mysql
|
4
|
+
- postgresql
|
2
5
|
rvm:
|
3
|
-
- 2.
|
4
|
-
|
6
|
+
- 2.5
|
7
|
+
- 2.6
|
8
|
+
- 2.7
|
9
|
+
- 3.0
|
10
|
+
gemfile:
|
11
|
+
- gemfiles/rails_5.1.gemfile
|
12
|
+
- gemfiles/rails_5.2.gemfile
|
13
|
+
- gemfiles/rails_6.0.gemfile
|
14
|
+
- gemfiles/rails_6.1.gemfile
|
15
|
+
jobs:
|
16
|
+
exclude: # Unsupported combos: https://www.fastruby.io/blog/ruby/rails/versions/compatibility-table.html
|
17
|
+
- rvm: 3.0
|
18
|
+
gemfiles/rails_5.1.gemfile
|
19
|
+
- rvm: 3.0
|
20
|
+
gemfiles/rails_5.2.gemfile
|
21
|
+
|
22
|
+
before_install:
|
23
|
+
- bin/setup
|
24
|
+
cache:
|
25
|
+
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,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
|
@@ -1,107 +1,70 @@
|
|
1
1
|
require 'active_record'
|
2
|
-
require 'active_record/connection_adapters/abstract_adapter'
|
3
|
-
require "database_cleaner/generic/truncation"
|
4
2
|
require 'database_cleaner/active_record/truncation'
|
5
3
|
|
6
4
|
module DatabaseCleaner
|
7
|
-
module
|
8
|
-
|
9
|
-
def
|
10
|
-
|
5
|
+
module ActiveRecord
|
6
|
+
class Deletion < Truncation
|
7
|
+
def clean
|
8
|
+
connection.disable_referential_integrity do
|
9
|
+
if pre_count? && connection.respond_to?(:pre_count_tables)
|
10
|
+
delete_tables(connection, connection.pre_count_tables(tables_to_truncate(connection)))
|
11
|
+
else
|
12
|
+
delete_tables(connection, tables_to_truncate(connection))
|
13
|
+
end
|
14
|
+
end
|
11
15
|
end
|
12
|
-
end
|
13
16
|
|
14
|
-
|
15
|
-
def delete_table(table_name)
|
16
|
-
execute("DELETE FROM #{quote_table_name(table_name)};")
|
17
|
-
end
|
18
|
-
end
|
17
|
+
private
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def delete_tables(connection, table_names)
|
20
|
+
table_names.each do |table_name|
|
21
|
+
delete_table(connection, table_name)
|
22
|
+
end
|
23
23
|
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
module ActiveRecord
|
29
|
-
module ConnectionAdapters
|
30
|
-
AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractDeleteAdapter }
|
31
|
-
|
32
|
-
JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(JdbcAdapter)
|
33
|
-
AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(AbstractMysqlAdapter)
|
34
|
-
Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(Mysql2Adapter)
|
35
|
-
SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLiteAdapter)
|
36
|
-
SQLite3Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLite3Adapter)
|
37
|
-
PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(PostgreSQLAdapter)
|
38
|
-
IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(IBM_DBAdapter)
|
39
|
-
SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLServerAdapter)
|
40
|
-
OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleDeleteAdapter } if defined?(OracleEnhancedAdapter)
|
41
|
-
end
|
42
|
-
end
|
43
24
|
|
44
|
-
|
45
|
-
|
46
|
-
def tables_to_truncate(connection)
|
47
|
-
if information_schema_exists?(connection)
|
48
|
-
(@only || tables_with_new_rows(connection)) - @tables_to_exclude
|
49
|
-
else
|
50
|
-
super
|
25
|
+
def delete_table connection, table_name
|
26
|
+
connection.execute("DELETE FROM #{connection.quote_table_name(table_name)}")
|
51
27
|
end
|
52
|
-
end
|
53
28
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
29
|
+
def tables_to_truncate(connection)
|
30
|
+
if information_schema_exists?(connection)
|
31
|
+
@except += connection.database_cleaner_view_cache + migration_storage_names
|
32
|
+
(@only.any? ? @only : tables_with_new_rows(connection)) - @except
|
33
|
+
else
|
34
|
+
super
|
35
|
+
end
|
60
36
|
end
|
61
|
-
end
|
62
37
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
38
|
+
def tables_with_new_rows(connection)
|
39
|
+
stats = table_stats_query(connection)
|
40
|
+
if stats != ''
|
41
|
+
connection.select_values(stats)
|
42
|
+
else
|
43
|
+
[]
|
44
|
+
end
|
45
|
+
end
|
68
46
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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)"
|
47
|
+
def table_stats_query(connection)
|
48
|
+
@table_stats_query ||= build_table_stats_query(connection)
|
49
|
+
ensure
|
50
|
+
@table_stats_query = nil unless @cache_tables
|
78
51
|
end
|
79
|
-
queries.join(' UNION ALL ')
|
80
|
-
end
|
81
52
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
53
|
+
def build_table_stats_query(connection)
|
54
|
+
tables = connection.select_values(<<-SQL)
|
55
|
+
SELECT table_name
|
56
|
+
FROM information_schema.tables
|
57
|
+
WHERE table_schema = database()
|
58
|
+
AND #{self.class.exclusion_condition('table_name')};
|
59
|
+
SQL
|
60
|
+
queries = tables.map do |table|
|
61
|
+
"(SELECT #{connection.quote(table)} FROM #{connection.quote_table_name(table)} LIMIT 1)"
|
90
62
|
end
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
class Deletion < Truncation
|
95
|
-
if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
|
96
|
-
include SelectiveTruncation
|
97
|
-
end
|
63
|
+
queries.join(' UNION ALL ')
|
64
|
+
end
|
98
65
|
|
99
|
-
|
100
|
-
|
101
|
-
connection.disable_referential_integrity do
|
102
|
-
tables_to_truncate(connection).each do |table_name|
|
103
|
-
connection.delete_table table_name
|
104
|
-
end
|
66
|
+
def information_schema_exists? connection
|
67
|
+
connection.adapter_name == "Mysql2"
|
105
68
|
end
|
106
69
|
end
|
107
70
|
end
|