database_cleaner-active_record 1.99.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db0629196991321d7ca040c181f5e10feffb27a307c0db0f3a0aeaced412eaaa
4
- data.tar.gz: 1b298eeb459d1903cba80e1031b5bbd24ea146fe6d4ad8ed2d4eeb93eb6cf254
3
+ metadata.gz: 997c59d22ce3ef9178c0b69c3d30b98d2e1126ddf099b5630bf0fbc272c76280
4
+ data.tar.gz: c8fc2baa15ceb2d6efb1b6b3a0890868dad0b5b51c78929b39564cc6e2be0c0f
5
5
  SHA512:
6
- metadata.gz: 69e20a7c75b34cadff9745bafae326a3d160cf285d9f2c37f4078363ceb9e88976cd22a3cc8f8789815f79843aa42c7b70b0549236447829377adc3af28e7e67
7
- data.tar.gz: d7a27c45270537e4d72a09e8a1c23f95931f6bd1aec979aa9bba1318eab9a64e4395fc36398bdd8a77b0173b8d7c5cace9e42d68f351cd1e9249c5c330545f79
6
+ metadata.gz: 6ddd99b203a2a712c8adbf01de42dbc751894ffe52f61cc540ebc116ceb9efc0ad8bb7cdeba08630d77b6754ac81fcad19adb1031d9c84bb4d7cbfcddd250675
7
+ data.tar.gz: fd9f66f85aa80d0247f9bbb753aad05d58b129723d36676ef09927091630a6e65025180dbb01fb7e9d31f4180933df5f98103c98ba71b0f4f2ff9ea639e31916
@@ -0,0 +1,80 @@
1
+ name: Tests
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ name: 'Ruby: ${{ matrix.ruby }}, Rails: ${{ matrix.rails }}'
8
+ runs-on: 'ubuntu-22.04'
9
+ strategy:
10
+ fail-fast: false
11
+ matrix:
12
+ ruby: ['3.2', '3.1', '3.0', '2.7', '2.6', '2.5']
13
+ rails: ['5.1', '5.2', '6.0', '6.1', '7.0', '7.1']
14
+ exclude:
15
+ - ruby: '3.2'
16
+ rails: '5.1'
17
+ - ruby: '3.2'
18
+ rails: '5.2'
19
+ - ruby: '3.2'
20
+ rails: '6.0'
21
+ - ruby: '3.2'
22
+ rails: '6.1'
23
+ - ruby: '3.1'
24
+ rails: '5.1'
25
+ - ruby: '3.1'
26
+ rails: '5.2'
27
+ - ruby: '3.1'
28
+ rails: '6.0'
29
+ - ruby: '3.0'
30
+ rails: '5.1'
31
+ - ruby: '3.0'
32
+ rails: '5.2'
33
+ - ruby: '2.6'
34
+ rails: '7.0'
35
+ - ruby: '2.5'
36
+ rails: '7.0'
37
+ - ruby: '2.5'
38
+ rails: '7.1'
39
+ - ruby: '2.6'
40
+ rails: '7.1'
41
+ env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
42
+ BUNDLE_GEMFILE: gemfiles/rails_${{ matrix.rails }}.gemfile
43
+ steps:
44
+ - uses: actions/checkout@v3
45
+ - name: Set up Ruby ${{ matrix.ruby }}
46
+ uses: ruby/setup-ruby@v1
47
+ with:
48
+ ruby-version: ${{ matrix.ruby }}
49
+ bundler-cache: true # 'bundle install' and cache
50
+ rubygems: ${{ matrix.ruby == '2.5' && 'default' || 'latest' }}
51
+ - name: Copy config file
52
+ run: cp spec/support/sample.config.yml spec/support/config.yml
53
+ - name: Run tests
54
+ run: bundle exec rake
55
+
56
+ services:
57
+ mysql:
58
+ image: mysql:5.7
59
+ env:
60
+ MYSQL_ALLOW_EMPTY_PASSWORD: yes
61
+ ports:
62
+ - 3306:3306
63
+ options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
64
+
65
+ postgres:
66
+ # Docker Hub image
67
+ image: postgres
68
+ # Provide the password for postgres
69
+ env:
70
+ POSTGRES_USER: postgres
71
+ POSTGRES_PASSWORD: postgres
72
+ ports:
73
+ - 5432:5432
74
+ # Set health checks to wait until postgres has started
75
+ options: >-
76
+ --health-cmd pg_isready
77
+ --health-interval 10s
78
+ --health-timeout 5s
79
+ --health-retries 5
80
+
data/.gitignore CHANGED
@@ -8,6 +8,11 @@
8
8
  /spec/support/config.yml
9
9
  /tmp/
10
10
  !/tmp/.keep
11
+ /.ruby-version
12
+ /.byebug_history
11
13
 
12
14
  # rspec failure tracking
13
15
  .rspec_status
16
+
17
+ /Gemfile.lock
18
+ /gemfiles/*.lock
data/.travis.yml CHANGED
@@ -1,4 +1,25 @@
1
1
  language: ruby
2
+ services:
3
+ - mysql
4
+ - postgresql
2
5
  rvm:
3
- - 2.2.9
4
- before_install: gem install bundler -v 1.17.2
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
+ gemfile: gemfiles/rails_5.1.gemfile
19
+ - rvm: 3.0
20
+ gemfile: gemfiles/rails_5.2.gemfile
21
+
22
+ before_install:
23
+ - bin/setup
24
+ cache:
25
+ bundler: true
data/Appraisals ADDED
@@ -0,0 +1,23 @@
1
+ appraise "rails-5.1" do
2
+ gem "rails", "~> 5.1.0"
3
+ end
4
+
5
+ appraise "rails-5.2" do
6
+ gem "rails", "~> 5.2.0"
7
+ end
8
+
9
+ appraise "rails-6.0" do
10
+ gem "rails", "~> 6.0.0"
11
+ end
12
+
13
+ appraise "rails-6.1" do
14
+ gem "rails", "~> 6.1.0"
15
+ end
16
+
17
+ appraise "rails-7.0" do
18
+ gem "rails", "~> 7.0.0"
19
+ end
20
+
21
+ appraise "rails-7.1" do
22
+ gem "rails", "~> 7.1.0"
23
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Development (unreleased)
2
+
3
+ ## v2.1.0 2023-02-17
4
+
5
+ * Add Ruby 3.2 to CI matrix: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/79
6
+ * Add Rails 7.1 support: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/78
7
+ * Add WHERE clause to make `ruby-spanner-activerecord` happy: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/77
data/Gemfile CHANGED
@@ -1,8 +1,12 @@
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", path: "../.."
5
+ gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
+
7
+ gem "rails", "~>5.2"
8
+
9
+ group :test do
10
+ gem "simplecov", require: false
11
+ gem "codecov", require: false
12
+ end
data/README.md CHANGED
@@ -1,12 +1,15 @@
1
1
  # Database Cleaner Adapter for ActiveRecord
2
2
 
3
- [![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner-active_record.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner-active_record)
3
+ [![Tests](https://github.com/DatabaseCleaner/database_cleaner-active_record/actions/workflows/ci.yml/badge.svg)](https://github.com/DatabaseCleaner/database_cleaner-active_record/actions/workflows/ci.yml)
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
- Here is an overview of the supported strategies:
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
- For support or to discuss development please use the [Google Group](https://groups.google.com/group/database_cleaner).
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
- If you are using ActiveRecord then take a look at the [additional options](#additional-activerecord-options-for-truncation) available for `:truncation`.
44
+ ## Strategy configuration options
57
45
 
58
- ## Configuration options
46
+ The transaction strategy accepts no options.
59
47
 
60
- <table>
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
- ### Additional ActiveRecord options for Truncation
50
+ * `:only` and `:except` may take a list of table names:
76
51
 
77
- The following options are available for ActiveRecord's `:truncation` strategy _only_ for MySQL and Postgres.
52
+ ```ruby
53
+ # Only truncate the "users" table.
54
+ DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Truncation.new(only: ["users"])
78
55
 
79
- * `:pre_count` - When set to `true` this will check each table for existing rows before truncating it. This can speed up test suites when many of the tables to be truncated are never populated. Defaults to `:false`. (Also, see the section on [What strategy is fastest?](#what-strategy-is-fastest))
80
- * `:reset_ids` - This only matters when `:pre_count` is used, and it will make sure that a tables auto-incrementing id is reset even if there are no rows in the table (e.g. records were created in the test but also removed before DatabaseCleaner gets to it). Defaults to `true`.
56
+ # Delete all tables except the "users" table.
57
+ DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Deletion.new(except: ["users"])
58
+ ```
81
59
 
82
- The following option is available for ActiveRecord's `:truncation` and `:deletion` strategy for any DB.
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
- ```ruby
88
+ ```
96
89
  client_min_messages = warning
97
90
  ```
98
91
 
99
- For ActiveRecord, you add the following parameter in your database.yml file:
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
@@ -5,4 +5,3 @@ set -vx
5
5
 
6
6
  bundle check || bundle install
7
7
  cp spec/support/sample.config.yml spec/support/config.yml
8
-
@@ -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."
@@ -18,23 +15,17 @@ Gem::Specification.new do |spec|
18
15
  f.match(%r{^(test|spec|features)/})
19
16
  end
20
17
  spec.bindir = "exe"
21
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.executables = []
22
19
  spec.require_paths = ["lib"]
23
20
 
24
- spec.add_dependency "database_cleaner", "~> 1.99.0"
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
- unless RUBY_PLATFORM =~ /java/
32
- spec.add_development_dependency 'mysql', '~> 2.9.1'
33
- spec.add_development_dependency 'mysql2'
34
- spec.add_development_dependency "activerecord-mysql2-adapter"
35
- spec.add_development_dependency 'pg'
36
- spec.add_development_dependency "sqlite3"
37
- else
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,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,13 @@
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
+
8
+ group :test do
9
+ gem "simplecov", require: false
10
+ gem "codecov", require: false
11
+ end
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
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
+
8
+ group :test do
9
+ gem "simplecov", require: false
10
+ gem "codecov", require: false
11
+ end
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
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
+
8
+ group :test do
9
+ gem "simplecov", require: false
10
+ gem "codecov", require: false
11
+ end
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,20 @@
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
+
8
+ group :test do
9
+ gem "simplecov", require: false
10
+ gem "codecov", require: false
11
+ end
12
+
13
+ if RUBY_VERSION >= '3.1'
14
+ gem 'net-smtp', require: false
15
+ gem 'net-imap', require: false
16
+ gem 'net-pop', require: false
17
+ end
18
+
19
+
20
+ gemspec path: "../"
@@ -0,0 +1,13 @@
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", "~> 7.0.0"
7
+
8
+ group :test do
9
+ gem "simplecov", require: false
10
+ gem "codecov", require: false
11
+ end
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
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", github: 'rails/rails' # "~> 7.1.0"
7
+
8
+ group :test do
9
+ gem "simplecov", require: false
10
+ gem "codecov", require: false
11
+ end
12
+
13
+ gemspec path: "../"
@@ -1,94 +1,85 @@
1
1
  require 'active_record'
2
- require 'database_cleaner/generic/base'
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 ||= 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
13
+ @config_file_location ||= "#{Dir.pwd}/config/database.yml"
27
14
  end
28
15
 
29
- module Base
30
- include ::DatabaseCleaner::Generic::Base
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
+ if Gem::Version.new("6.0.0") <= ::ActiveRecord.version
19
+ ::ActiveRecord::Base.connection.schema_migration.table_name
20
+ else
21
+ ::ActiveRecord::SchemaMigration.table_name
22
+ end
37
23
  end
38
24
 
39
- def db
40
- @db ||= super
25
+ def self.exclusion_condition(column_name)
26
+ <<~SQL
27
+ #{column_name} <> '#{DatabaseCleaner::ActiveRecord::Base.migration_table_name}'
28
+ AND #{column_name} <> '#{::ActiveRecord::Base.internal_metadata_table_name}'
29
+ SQL
41
30
  end
42
31
 
43
- def load_config
44
- if self.db != :default && self.db.is_a?(Symbol) && File.file?(ActiveRecord.config_file_location)
45
- connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result)
46
- @connection_hash = valid_config(connection_details)[self.db.to_s]
47
- end
32
+ def db=(*)
33
+ super
34
+ load_config
48
35
  end
49
36
 
50
- def valid_config(connection_file)
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
37
+ attr_accessor :connection_hash
58
38
 
59
39
  def connection_class
60
40
  @connection_class ||= if db && !db.is_a?(Symbol)
61
41
  db
62
42
  elsif connection_hash
63
- lookup_from_connection_pool || establish_connection
43
+ (lookup_from_connection_pool rescue nil) || establish_connection
64
44
  else
65
45
  ::ActiveRecord::Base
66
46
  end
67
47
  end
68
48
 
69
- def self.migration_table_name
70
- if ::ActiveRecord::VERSION::MAJOR < 5
71
- ::ActiveRecord::Migrator.schema_migrations_table_name
72
- else
73
- ::ActiveRecord::SchemaMigration.table_name
49
+ private
50
+
51
+ def load_config
52
+ if db != :default && db.is_a?(Symbol) && File.file?(DatabaseCleaner::ActiveRecord.config_file_location)
53
+ connection_details =
54
+ if RUBY_VERSION.match?(/\A2\.5/)
55
+ YAML.safe_load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result, [], [], true)
56
+ else
57
+ YAML.safe_load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result, aliases: true)
58
+ end
59
+ @connection_hash = valid_config(connection_details, db.to_s)
74
60
  end
75
61
  end
76
62
 
77
- def self.exclusion_condition(column_name)
78
- result = " #{column_name} <> '#{::DatabaseCleaner::ActiveRecord::Base.migration_table_name}' "
79
- if ::ActiveRecord::VERSION::MAJOR >= 5
80
- result += " AND #{column_name} <> '#{::ActiveRecord::Base.internal_metadata_table_name}' "
81
- end
82
- result
63
+ def valid_config(connection_file, db)
64
+ return connection_file[db] unless (active_record_config_hash = active_record_config_hash_for(db))
65
+
66
+ active_record_config_hash
83
67
  end
84
68
 
85
- private
69
+ def active_record_config_hash_for(db)
70
+ if ::ActiveRecord.version >= Gem::Version.new('6.1')
71
+ ::ActiveRecord::Base.configurations&.configs_for(name: db)&.configuration_hash
72
+ else
73
+ ::ActiveRecord::Base.configurations[db]
74
+ end
75
+ end
86
76
 
87
77
  def lookup_from_connection_pool
88
- if ::ActiveRecord::Base.respond_to?(:descendants)
89
- database_name = connection_hash["database"] || connection_hash[:database]
90
- models = ::ActiveRecord::Base.descendants
91
- models.select(&:connection_pool).detect { |m| m.connection_pool.spec.config[:database] == database_name }
78
+ return unless ::ActiveRecord::Base.respond_to?(:descendants)
79
+
80
+ database_name = connection_hash['database'] || connection_hash[:database]
81
+ ::ActiveRecord::Base.descendants.select(&:connection_pool).detect do |model|
82
+ database_for(model) == database_name
92
83
  end
93
84
  end
94
85
 
@@ -96,6 +87,14 @@ module DatabaseCleaner
96
87
  ::ActiveRecord::Base.establish_connection(connection_hash)
97
88
  ::ActiveRecord::Base
98
89
  end
90
+
91
+ def database_for(model)
92
+ if model.connection_pool.respond_to?(:db_config) # ActiveRecord >= 6.1
93
+ model.connection_pool.db_config.configuration_hash[:database]
94
+ else
95
+ model.connection_pool.spec.config[:database]
96
+ end
97
+ end
99
98
  end
100
99
  end
101
100
  end