database_cleaner-active_record 2.0.1 → 2.2.2

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: 0f4e22827466f77ef287a8cfa7cf75078940f69f4e3b222676d7cb30d74edb74
4
- data.tar.gz: 942936724f8ef0e4a0107044aef35b203e6e8d23c1a6d241d452342eb1d8ae17
3
+ metadata.gz: f4009a9e2733cc775473d2357dedf4005e14abc0be5a1b2841241dc9ac0a8afd
4
+ data.tar.gz: 89a9750d057f21fb543f2f7ca93926c48df4e2b08fec1276589063ca42254146
5
5
  SHA512:
6
- metadata.gz: 8116e18c0f97636ab92457c6025ef7978547aba2553c747e8120b7ac528fe583f6f1cc76cbc78385c2a26fa6a6a348d37c95c5440186404259a4d50b39200b9d
7
- data.tar.gz: e847363c1be911931358c9b1cb3f09717e302b2e89aa609ce366baa1ec6664fbf3935b4c52b6785cfac25f36794992a118edc9846774a485e26fa7aeaa27896e
6
+ metadata.gz: 0daef1af314e3a7f432257e7b6051076d7963e685861b5b232811bcb9bcc6cdf0d9a020b980536ca9533c804c0aebe258dbaf6ef642a96c56950be6faf50ce3a
7
+ data.tar.gz: 3166ab8f41926b33a005f458ad601de7f2cc0fff3c2a36151a5c217271fe3dd84a7db332c448ece8aafab396d5e730c3b4760acfc5b56edd6129201e12977f86
@@ -0,0 +1,94 @@
1
+ name: Tests
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ name: 'Ruby: ${{ matrix.ruby }}, Rails: ${{ matrix.rails }}, Channel: ${{ matrix.channel }}'
8
+ runs-on: 'ubuntu-22.04'
9
+ strategy:
10
+ fail-fast: false
11
+ matrix:
12
+ ruby: ['3.4', '3.3', '3.2', '3.1']
13
+ rails: ['6.1', '7.0', '7.1', '7.2', '8.0']
14
+ channel: ['stable']
15
+
16
+ include:
17
+ - ruby: 'ruby-head'
18
+ rails: 'edge'
19
+ channel: 'experimental'
20
+ - ruby: 'ruby-head'
21
+ rails: '8.0'
22
+ channel: 'experimental'
23
+ - ruby: 'ruby-head'
24
+ rails: '7.2'
25
+ channel: 'experimental'
26
+ - ruby: 'ruby-head'
27
+ rails: '7.1'
28
+ channel: 'experimental'
29
+
30
+ - ruby: '3.4'
31
+ rails: 'edge'
32
+ channel: 'experimental'
33
+ - ruby: '3.3'
34
+ rails: 'edge'
35
+ channel: 'experimental'
36
+ - ruby: '3.2'
37
+ rails: 'edge'
38
+ channel: 'experimental'
39
+
40
+ exclude:
41
+ - ruby: '3.4'
42
+ rails: '6.1'
43
+
44
+ - ruby: '3.3'
45
+ rails: '6.1'
46
+
47
+ - ruby: '3.2'
48
+ rails: '6.1'
49
+
50
+ - ruby: '3.1'
51
+ rails: '8.0'
52
+
53
+ continue-on-error: ${{ matrix.channel != 'stable' }}
54
+
55
+ env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
56
+ BUNDLE_GEMFILE: gemfiles/rails_${{ matrix.rails }}.gemfile
57
+ steps:
58
+ - uses: actions/checkout@v4
59
+ - name: Set up Ruby ${{ matrix.ruby }}
60
+ uses: ruby/setup-ruby@v1
61
+ with:
62
+ ruby-version: ${{ matrix.ruby }}
63
+ bundler-cache: true # 'bundle install' and cache
64
+ rubygems: ${{ matrix.ruby == '2.5' && 'default' || 'latest' }}
65
+ - name: Copy config file
66
+ run: cp spec/support/sample.config.yml spec/support/config.yml
67
+ - name: Run tests
68
+ run: bundle exec rake
69
+
70
+ services:
71
+ mysql:
72
+ image: mysql:5.7
73
+ env:
74
+ MYSQL_ALLOW_EMPTY_PASSWORD: yes
75
+ ports:
76
+ - 3306:3306
77
+ options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
78
+
79
+ postgres:
80
+ # Docker Hub image
81
+ image: postgres
82
+ # Provide the password for postgres
83
+ env:
84
+ POSTGRES_USER: postgres
85
+ POSTGRES_PASSWORD: postgres
86
+ ports:
87
+ - 5432:5432
88
+ # Set health checks to wait until postgres has started
89
+ options: >-
90
+ --health-cmd pg_isready
91
+ --health-interval 10s
92
+ --health-timeout 5s
93
+ --health-retries 5
94
+
data/Appraisals CHANGED
@@ -1,15 +1,22 @@
1
- appraise "rails-5.1" do
2
- gem "rails", "~>5.1.0"
1
+ appraise "rails-6.1" do
2
+ gem "rails", "~> 6.1.0"
3
+ gem "sqlite3", "~> 1.5"
3
4
  end
4
5
 
5
- appraise "rails-5.2" do
6
- gem "rails", "~>5.2.0"
6
+ appraise "rails-7.0" do
7
+ gem "rails", "~> 7.0.0"
8
+ gem "sqlite3", "~> 1.7"
7
9
  end
8
10
 
9
- appraise "rails-6.0" do
10
- gem "rails", "~>6.0.0"
11
+ appraise "rails-7.1" do
12
+ gem "rails", "~> 7.1.0"
13
+ gem "sqlite3", "~> 1.7" # FIXME: remove after rails/rails#51592
11
14
  end
12
15
 
13
- appraise "rails-6.1" do
14
- gem "rails", "~>6.1.0"
16
+ appraise "rails-7.2" do
17
+ gem "rails", "~> 7.2.0.beta2"
18
+ end
19
+
20
+ appraise "rails-edge" do
21
+ gem "rails", github: "rails/rails"
15
22
  end
data/CHANGELOG.md ADDED
@@ -0,0 +1,38 @@
1
+ # Development (unreleased)
2
+
3
+ ## v2.2.2 2025-07-30
4
+
5
+ * Release database connections after cleaning: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/122
6
+ * Provide a 'Changelog' link on Rubygems: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/114
7
+ * Fix bundling and CONTRIBUTE.md instructions: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/123
8
+ * https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/121 by @etagwerker
9
+ * Fix order of arguments in `truncate_tables` expectation https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/124
10
+ * Add Docker to make it easier to run tests locally for maintainers and contributors https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/109
11
+ * Allow truncation option https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/121 by @etagwerker
12
+
13
+ ## v2.2.1 2025-05-13
14
+
15
+ * https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/111 by @tagliala
16
+ * https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/104 by @fatkodima
17
+ * https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/118 by @pat, @thegeorgeous, and @nnishimura
18
+
19
+ ## v2.2.0 2024-07-12
20
+
21
+ * Fix "ERROR: currval of sequence" in Postgres adapter: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/103
22
+ * Use lock synchronize on transaction callback: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/73
23
+ * Stop testing with EOLed Ruby & Rails versions: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/105
24
+ * Fix compatibility issue with Rails 7.2: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/107
25
+ * Fix typo in truncation methods: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/94/files
26
+ * Address deprecation of ActiveRecord::Base.connection in Rails 7.2: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/102
27
+ * Support Rails 7.2+: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/101
28
+ * Fix reset_ids test with Trilogy adapter: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/93
29
+ * Implement resetting ids for deletion strategy: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/71
30
+ * Avoid loading ActiveRecord::Base early: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/91
31
+ * Fix specs to account for trilogy: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/88
32
+ * Add basic support for trilogy: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/85
33
+
34
+ ## v2.1.0 2023-02-17
35
+
36
+ * Add Ruby 3.2 to CI matrix: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/79
37
+ * Add Rails 7.1 support: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/78
38
+ * Add WHERE clause to make `ruby-spanner-activerecord` happy: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/77
data/CONTRIBUTE.md ADDED
@@ -0,0 +1,45 @@
1
+ # Guidelines for contributing
2
+
3
+ ## 1. Fork & Clone
4
+
5
+ Since you probably don't have rights to the main repo, you should Fork it (big
6
+ button up top). After that, clone your fork locally and optionally add an
7
+ upstream:
8
+
9
+ git remote add upstream git@github.com:DatabaseCleaner/database_cleaner-active_record.git
10
+
11
+ ## 2. Make sure the tests run fine
12
+
13
+ The gem uses Appraisal to configure different Gemfiles to test different Rails versions.
14
+
15
+ ### Run tests without Docker (or using Docker only for the databases)
16
+
17
+ - You can run all the databases through docker if needed with `docker compose -f docker-compose.db.yml up` (you can also have them running on your system, just comment out the ones you don't need from the `docker-compose.db.yml` file)
18
+ - Copy `spec/support/sample.config.yml` to `spec/support/config.yml` and edit it as needed
19
+ - `BUNDLE_GEMFILE=gemfiles/rails_6.1.gemfile bundle install` (change `6.1` with any version from the `gemfiles` directory)
20
+ - `BUNDLE_GEMFILE=gemfiles/rails_6.1.gemfile bundle exec rake`
21
+
22
+ Note that if you don't have all the supported databases installed and running,
23
+ some tests will fail.
24
+
25
+ > Check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work
26
+
27
+ ### Run tests with Docker
28
+
29
+ - Open `docker-compose.yml` and configure the Ruby version and Gemfile file to use
30
+ - Copy `spec/support/sample.docker.config.yml` to `spec/support/config.yml` (not this config file is specific for the Docker setup)
31
+ - Run `docker compose up` to start the container, run the tests, and exit
32
+ - Run `docker compose run ruby bash` to open `bash` inside the container for more control, run `rake` to run the tests
33
+
34
+ > Note that the code is mounted inside the docker container, so changes in the container will reflect in the code. There's no need to re-build the container for code changes, but changing the Ruby version or Gemfile in the docker-compose.yml will require a container re-build with `docker compose build --no-cache`
35
+
36
+ > Check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work
37
+
38
+ ## 3. Prepare your contribution
39
+
40
+ This is all up to you but a few points should be kept in mind:
41
+
42
+ - Please write tests for your contribution
43
+ - Make sure that previous tests still pass
44
+ - Push it to a branch of your fork
45
+ - Submit a pull request
data/Dockerfile ADDED
@@ -0,0 +1,19 @@
1
+ ARG RUBY_VERSION=3.3
2
+ FROM ruby:${RUBY_VERSION}
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Copy the current directory contents into the container at /app
8
+ # This is copied so we can bundle the application, but it's replaced
9
+ # by a mounted volume with the current code when executed with docker compose
10
+ COPY . /app
11
+
12
+ ARG BUNDLE_GEMFILE=Gemfile
13
+ ENV BUNDLE_GEMFILE=${BUNDLE_GEMFILE}
14
+
15
+ # Install any needed packages specified in Gemfile
16
+ RUN ./bin/setup
17
+
18
+ # Command to run the application
19
+ CMD ["bash"]
data/Gemfile CHANGED
@@ -5,7 +5,6 @@ gemspec
5
5
  gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
6
 
7
7
  gem "rails", "~>5.2"
8
- gem "byebug"
9
8
 
10
9
  group :test do
11
10
  gem "simplecov", require: false
data/README.md CHANGED
@@ -1,6 +1,6 @@
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
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)
6
6
 
@@ -8,7 +8,7 @@ Clean your ActiveRecord databases with Database Cleaner.
8
8
 
9
9
  See https://github.com/DatabaseCleaner/database_cleaner for more information.
10
10
 
11
- For support or to discuss development please use the [Google Group](https://groups.google.com/group/database_cleaner).
11
+ For support or to discuss development please use GitHub Issues.
12
12
 
13
13
  ## Installation
14
14
 
@@ -51,16 +51,20 @@ The truncation and deletion strategies may accept the following options:
51
51
 
52
52
  ```ruby
53
53
  # Only truncate the "users" table.
54
- DatabaseCleaner[:active_record].strategy = :truncation, only: ["users"]
54
+ DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Truncation.new(only: ["users"])
55
55
 
56
56
  # Delete all tables except the "users" table.
57
- DatabaseCleaner[:active_record].strategy = :deletion, except: ["users"]
57
+ DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Deletion.new(except: ["users"])
58
58
  ```
59
59
 
60
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))
61
61
 
62
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`.
63
63
 
64
+ * `:reset_ids` - Only valid for deletion strategy, when set to `true` resets ids to 1 after each table is cleaned.
65
+
66
+ * `:truncate_option` - Only valid for PostgreSQL. Acceptable values are `:restrict` and `:cascade`. Default is `:restrict`
67
+
64
68
  ## Adapter configuration options
65
69
 
66
70
  `#db` defaults to the default ActiveRecord database, but can be specified manually in a few ways:
@@ -95,9 +99,13 @@ You can also add this parameter to your database.yml file:
95
99
  test:
96
100
  adapter: postgresql
97
101
  # ...
98
- min_messages: WARNING
102
+ min_messages: WARNING
99
103
  </pre>
100
104
 
105
+ ## Development
106
+
107
+ Check the CONTRIBUTE.md file for instructions running tests with and withour Docker.
108
+
101
109
  ## COPYRIGHT
102
110
 
103
111
  See [LICENSE](LICENSE) for details.
@@ -15,10 +15,10 @@ Gem::Specification.new do |spec|
15
15
  f.match(%r{^(test|spec|features)/})
16
16
  end
17
17
  spec.bindir = "exe"
18
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.executables = []
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "database_cleaner-core", "~>2.0.0"
21
+ spec.add_dependency "database_cleaner-core", "~>2.0"
22
22
  spec.add_dependency "activerecord", ">= 5.a"
23
23
 
24
24
  spec.add_development_dependency "bundler"
@@ -28,4 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency "mysql2"
29
29
  spec.add_development_dependency "pg"
30
30
  spec.add_development_dependency "sqlite3"
31
+ spec.add_development_dependency "trilogy"
32
+
33
+ spec.metadata["changelog_uri"] = spec.homepage + "/blob/main/CHANGELOG.md"
31
34
  end
@@ -0,0 +1,19 @@
1
+ services:
2
+ postgres:
3
+ image: postgres:16 # specify the version needed for a given app
4
+ environment:
5
+ - POSTGRES_PASSWORD=postgres # this is required
6
+ ports:
7
+ - "127.0.0.1:5432:5432" # so we can use `localhost` as the host
8
+ mysql:
9
+ image: mysql:5.7
10
+ environment:
11
+ - MYSQL_ROOT_PASSWORD=mysql
12
+ ports:
13
+ - "127.0.0.1:3306:3306"
14
+ redis:
15
+ image: redis:6.2-alpine
16
+ restart: always
17
+ ports:
18
+ - "127.0.0.1:6379:6379"
19
+ command: redis-server --save 20 1 --loglevel warning
@@ -0,0 +1,26 @@
1
+ services:
2
+ postgres:
3
+ image: postgres:16 # specify the version needed for a given app
4
+ environment:
5
+ - POSTGRES_PASSWORD=postgres # this is required
6
+ mysql:
7
+ image: mysql:5.7
8
+ environment:
9
+ - MYSQL_ROOT_PASSWORD=mysql
10
+ redis:
11
+ image: redis:6.2-alpine
12
+ restart: always
13
+ command: redis-server --save 20 1 --loglevel warning
14
+ ruby:
15
+ build:
16
+ context: .
17
+ args:
18
+ BUNDLE_GEMFILE: gemfiles/rails_7.2.gemfile # Manually change this based on the desired Rails version
19
+ RUBY_VERSION: 3.3 # Manually change this based on the desired Ruby version
20
+ volumes:
21
+ - ".:/app:delegated"
22
+ command: rake
23
+ depends_on:
24
+ - mysql
25
+ - postgres
26
+ - redis
@@ -3,8 +3,12 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
- gem "rails", "~>6.1.0"
7
- gem "byebug"
6
+ gem "rails", "~> 6.1.0"
7
+ gem "sqlite3", "~> 1.5"
8
+ gem "concurrent-ruby", "1.3.4"
9
+ gem "logger"
10
+ gem "mutex_m"
11
+ gem "bigdecimal"
8
12
 
9
13
  group :test do
10
14
  gem "simplecov", require: false
@@ -0,0 +1,18 @@
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
+ gem "sqlite3", "~> 1.7"
8
+ gem "concurrent-ruby", "1.3.4"
9
+ gem "logger"
10
+ gem "mutex_m"
11
+ gem "bigdecimal"
12
+
13
+ group :test do
14
+ gem "simplecov", require: false
15
+ gem "codecov", require: false
16
+ end
17
+
18
+ gemspec path: "../"
@@ -0,0 +1,17 @@
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.1.0"
7
+ gem "sqlite3", "~> 1.7"
8
+ gem "logger"
9
+ gem "mutex_m"
10
+ gem "bigdecimal"
11
+
12
+ group :test do
13
+ gem "simplecov", require: false
14
+ gem "codecov", require: false
15
+ end
16
+
17
+ gemspec path: "../"
@@ -3,8 +3,10 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
- gem "rails", "~>5.2.0"
7
- gem "byebug"
6
+ gem "rails", "~> 7.2.0"
7
+ gem "logger"
8
+ gem "mutex_m"
9
+ gem "bigdecimal"
8
10
 
9
11
  group :test do
10
12
  gem "simplecov", require: false
@@ -3,8 +3,10 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
- gem "rails", "~>5.1.0"
7
- gem "byebug"
6
+ gem "rails", "~> 8.0"
7
+ gem "logger"
8
+ gem "mutex_m"
9
+ gem "bigdecimal"
8
10
 
9
11
  group :test do
10
12
  gem "simplecov", require: false
@@ -3,8 +3,10 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
- gem "rails", "~>6.0.0"
7
- gem "byebug"
6
+ gem "rails", github: "rails/rails"
7
+ gem "logger"
8
+ gem "mutex_m"
9
+ gem "bigdecimal"
8
10
 
9
11
  group :test do
10
12
  gem "simplecov", require: false
@@ -1,4 +1,3 @@
1
- require 'active_record'
2
1
  require 'database_cleaner/strategy'
3
2
  require 'erb'
4
3
  require 'yaml'
@@ -15,7 +14,13 @@ module DatabaseCleaner
15
14
 
16
15
  class Base < DatabaseCleaner::Strategy
17
16
  def self.migration_table_name
18
- ::ActiveRecord::SchemaMigration.table_name
17
+ if ::ActiveRecord::Base.connection_pool.respond_to?(:schema_migration) # Rails >= 7.2
18
+ ::ActiveRecord::Base.connection_pool.schema_migration.table_name
19
+ elsif ::ActiveRecord::Base.connection.respond_to?(:schema_migration) # Rails >= 6.0
20
+ ::ActiveRecord::Base.connection.schema_migration.table_name
21
+ else
22
+ ::ActiveRecord::SchemaMigration.table_name
23
+ end
19
24
  end
20
25
 
21
26
  def self.exclusion_condition(column_name)
@@ -46,7 +51,12 @@ module DatabaseCleaner
46
51
 
47
52
  def load_config
48
53
  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)
54
+ connection_details =
55
+ if RUBY_VERSION.match?(/\A2\.5/)
56
+ YAML.safe_load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result, [], [], true)
57
+ else
58
+ YAML.safe_load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result, aliases: true)
59
+ end
50
60
  @connection_hash = valid_config(connection_details, db.to_s)
51
61
  end
52
62
  end
@@ -1,17 +1,16 @@
1
- require 'active_record'
2
- require 'database_cleaner/active_record/truncation'
3
-
4
1
  module DatabaseCleaner
5
2
  module ActiveRecord
6
3
  class Deletion < Truncation
7
4
  def clean
8
5
  connection.disable_referential_integrity do
9
6
  if pre_count? && connection.respond_to?(:pre_count_tables)
10
- delete_tables(connection, connection.pre_count_tables(tables_to_truncate(connection)))
7
+ delete_tables(connection, connection.pre_count_tables(tables_to_clean(connection)))
11
8
  else
12
- delete_tables(connection, tables_to_truncate(connection))
9
+ delete_tables(connection, tables_to_clean(connection))
13
10
  end
14
11
  end
12
+
13
+ connection_class.connection_pool.release_connection
15
14
  end
16
15
 
17
16
  private
@@ -19,14 +18,28 @@ module DatabaseCleaner
19
18
  def delete_tables(connection, table_names)
20
19
  table_names.each do |table_name|
21
20
  delete_table(connection, table_name)
21
+ reset_id_sequence(connection, table_name) if @reset_ids
22
22
  end
23
23
  end
24
24
 
25
25
  def delete_table connection, table_name
26
- connection.execute("DELETE FROM #{connection.quote_table_name(table_name)}")
26
+ connection.execute("DELETE FROM #{connection.quote_table_name(table_name)} WHERE 1=1")
27
+ end
28
+
29
+ def reset_id_sequence connection, table_name
30
+ case connection.adapter_name
31
+ when 'Mysql2', 'Trilogy'
32
+ connection.execute("ALTER TABLE #{table_name} AUTO_INCREMENT = 1;")
33
+ when 'SQLite'
34
+ connection.execute("delete from sqlite_sequence where name='#{table_name}';")
35
+ when 'PostgreSQL'
36
+ connection.reset_pk_sequence!(table_name)
37
+ else
38
+ raise "reset_id option not supported for #{connection.adapter_name}"
39
+ end
27
40
  end
28
41
 
29
- def tables_to_truncate(connection)
42
+ def tables_to_clean(connection)
30
43
  if information_schema_exists?(connection)
31
44
  @except += connection.database_cleaner_view_cache + migration_storage_names
32
45
  (@only.any? ? @only : tables_with_new_rows(connection)) - @except
@@ -64,7 +77,7 @@ module DatabaseCleaner
64
77
  end
65
78
 
66
79
  def information_schema_exists? connection
67
- connection.adapter_name == "Mysql2"
80
+ ["Mysql2", "Trilogy"].include?(connection.adapter_name)
68
81
  end
69
82
  end
70
83
  end
@@ -1,21 +1,29 @@
1
- require 'database_cleaner/active_record/base'
2
-
3
1
  module DatabaseCleaner
4
2
  module ActiveRecord
5
3
  class Transaction < Base
6
4
  def start
5
+ connection = if ::ActiveRecord.version >= Gem::Version.new("7.2")
6
+ connection_class.lease_connection
7
+ else
8
+ connection_class.connection
9
+ end
10
+
7
11
  # Hack to make sure that the connection is properly set up before cleaning
8
- connection_class.connection.transaction {}
12
+ connection.transaction {}
9
13
 
10
- connection_class.connection.begin_transaction joinable: false
14
+ connection.begin_transaction joinable: false
11
15
  end
12
16
 
13
17
 
14
18
  def clean
15
19
  connection_class.connection_pool.connections.each do |connection|
16
- next unless connection.open_transactions > 0
17
- connection.rollback_transaction
20
+ connection.lock.synchronize do
21
+ next unless connection.open_transactions > 0
22
+ connection.rollback_transaction
23
+ end
18
24
  end
25
+
26
+ connection_class.connection_pool.release_connection
19
27
  end
20
28
  end
21
29
  end
@@ -1,39 +1,48 @@
1
1
  require "delegate"
2
- require 'active_record/base'
3
2
  require 'database_cleaner/active_record/base'
4
3
 
5
4
  module DatabaseCleaner
6
5
  module ActiveRecord
7
6
  class Truncation < Base
8
7
  def initialize(opts={})
9
- if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :cache_tables]).empty?
10
- raise ArgumentError, "The only valid options are :only, :except, :pre_count, and :cache_tables. You specified #{opts.keys.join(',')}."
8
+ if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :cache_tables, :reset_ids, :truncate_option]).empty?
9
+ raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids, :cache_tables and :truncate_option. You specified #{opts.keys.join(',')}."
11
10
  end
12
11
 
13
12
  @only = Array(opts[:only]).dup
14
13
  @except = Array(opts[:except]).dup
15
14
 
15
+ @reset_ids = opts[:reset_ids]
16
16
  @pre_count = opts[:pre_count]
17
+ @truncate_option = opts[:truncate_option] || :restrict
17
18
  @cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
18
19
  end
19
20
 
20
21
  def clean
21
22
  connection.disable_referential_integrity do
22
23
  if pre_count? && connection.respond_to?(:pre_count_truncate_tables)
23
- connection.pre_count_truncate_tables(tables_to_truncate(connection))
24
+ connection.pre_count_truncate_tables(tables_to_clean(connection), { truncate_option: @truncate_option })
24
25
  else
25
- connection.truncate_tables(tables_to_truncate(connection))
26
+ connection.truncate_tables(tables_to_clean(connection), { truncate_option: @truncate_option })
26
27
  end
27
28
  end
29
+
30
+ connection_class.connection_pool.release_connection
28
31
  end
29
32
 
30
33
  private
31
34
 
32
35
  def connection
33
- @connection ||= ConnectionWrapper.new(connection_class.connection)
36
+ @connection ||= ConnectionWrapper.new(
37
+ if ::ActiveRecord.version >= Gem::Version.new("7.2")
38
+ connection_class.lease_connection
39
+ else
40
+ connection_class.connection
41
+ end
42
+ )
34
43
  end
35
44
 
36
- def tables_to_truncate(connection)
45
+ def tables_to_clean(connection)
37
46
  if @only.none?
38
47
  all_tables = cache_tables? ? connection.database_cleaner_table_cache : connection.database_tables
39
48
  @only = all_tables.map { |table| table.split(".").last }
@@ -62,7 +71,7 @@ module DatabaseCleaner
62
71
  def initialize(connection)
63
72
  extend AbstractAdapter
64
73
  case connection.adapter_name
65
- when "Mysql2"
74
+ when "Mysql2", "Trilogy"
66
75
  extend AbstractMysqlAdapter
67
76
  when "SQLite"
68
77
  extend AbstractMysqlAdapter
@@ -96,14 +105,14 @@ module DatabaseCleaner
96
105
  execute("DELETE FROM #{quote_table_name(table_name)}")
97
106
  end
98
107
 
99
- def truncate_tables(tables)
108
+ def truncate_tables(tables, opts)
100
109
  tables.each { |t| truncate_table(t) }
101
110
  end
102
111
  end
103
112
 
104
113
  module AbstractMysqlAdapter
105
- def pre_count_truncate_tables(tables)
106
- truncate_tables(pre_count_tables(tables))
114
+ def pre_count_truncate_tables(tables, opts = {})
115
+ truncate_tables(pre_count_tables(tables), opts)
107
116
  end
108
117
 
109
118
  def pre_count_tables(tables)
@@ -146,12 +155,12 @@ module DatabaseCleaner
146
155
  end
147
156
  end
148
157
 
149
- def truncate_tables(tables)
158
+ def truncate_tables(tables, opts)
150
159
  tables.each { |t| truncate_table(t) }
151
160
  end
152
161
 
153
- def pre_count_truncate_tables(tables)
154
- truncate_tables(pre_count_tables(tables))
162
+ def pre_count_truncate_tables(tables, opts = {})
163
+ truncate_tables(pre_count_tables(tables), opts)
155
164
  end
156
165
 
157
166
  def pre_count_tables(tables)
@@ -187,13 +196,14 @@ module DatabaseCleaner
187
196
  tables_with_schema
188
197
  end
189
198
 
190
- def truncate_tables(table_names)
199
+ def truncate_tables(table_names, opts)
191
200
  return if table_names.nil? || table_names.empty?
192
- execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} RESTART IDENTITY RESTRICT;")
201
+
202
+ execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} RESTART IDENTITY #{opts[:truncate_option]};")
193
203
  end
194
204
 
195
- def pre_count_truncate_tables(tables)
196
- truncate_tables(pre_count_tables(tables))
205
+ def pre_count_truncate_tables(tables, opts = {})
206
+ truncate_tables(pre_count_tables(tables), opts)
197
207
  end
198
208
 
199
209
  def pre_count_tables(tables)
@@ -218,7 +228,7 @@ module DatabaseCleaner
218
228
  def has_been_used?(table)
219
229
  return has_rows?(table) unless has_sequence?(table)
220
230
 
221
- cur_val = select_value("SELECT currval('#{table}_id_seq');").to_i rescue 0
231
+ cur_val = select_value("SELECT last_value from #{table}_id_seq;").to_i
222
232
  cur_val > 0
223
233
  end
224
234
 
@@ -246,4 +256,3 @@ module DatabaseCleaner
246
256
  private_constant :ConnectionWrapper
247
257
  end
248
258
  end
249
-
@@ -1,5 +1,5 @@
1
1
  module DatabaseCleaner
2
2
  module ActiveRecord
3
- VERSION = "2.0.1"
3
+ VERSION = "2.2.2"
4
4
  end
5
5
  end
@@ -1,7 +1,11 @@
1
- require 'database_cleaner/active_record/version'
1
+ require 'active_record'
2
2
  require 'database_cleaner/core'
3
- require 'database_cleaner/active_record/transaction'
4
- require 'database_cleaner/active_record/truncation'
5
- require 'database_cleaner/active_record/deletion'
6
3
 
7
- DatabaseCleaner[:active_record].strategy = :transaction
4
+ ActiveSupport.on_load(:active_record) do
5
+ require 'database_cleaner/active_record/base'
6
+ require 'database_cleaner/active_record/transaction'
7
+ require 'database_cleaner/active_record/truncation'
8
+ require 'database_cleaner/active_record/deletion'
9
+
10
+ DatabaseCleaner[:active_record].strategy = :transaction
11
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_cleaner-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernesto Tagwerker
8
8
  - Micah Geisel
9
- autorequire:
10
9
  bindir: exe
11
10
  cert_chain: []
12
- date: 2021-05-08 00:00:00.000000000 Z
11
+ date: 2025-07-30 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: database_cleaner-core
@@ -17,14 +16,14 @@ dependencies:
17
16
  requirements:
18
17
  - - "~>"
19
18
  - !ruby/object:Gem::Version
20
- version: 2.0.0
19
+ version: '2.0'
21
20
  type: :runtime
22
21
  prerelease: false
23
22
  version_requirements: !ruby/object:Gem::Requirement
24
23
  requirements:
25
24
  - - "~>"
26
25
  - !ruby/object:Gem::Version
27
- version: 2.0.0
26
+ version: '2.0'
28
27
  - !ruby/object:Gem::Dependency
29
28
  name: activerecord
30
29
  requirement: !ruby/object:Gem::Requirement
@@ -137,6 +136,20 @@ dependencies:
137
136
  - - ">="
138
137
  - !ruby/object:Gem::Version
139
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: trilogy
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
140
153
  description: Strategies for cleaning databases using ActiveRecord. Can be used to
141
154
  ensure a clean state for testing.
142
155
  email:
@@ -145,10 +158,13 @@ executables: []
145
158
  extensions: []
146
159
  extra_rdoc_files: []
147
160
  files:
161
+ - ".github/workflows/ci.yml"
148
162
  - ".gitignore"
149
163
  - ".rspec"
150
- - ".travis.yml"
151
164
  - Appraisals
165
+ - CHANGELOG.md
166
+ - CONTRIBUTE.md
167
+ - Dockerfile
152
168
  - Gemfile
153
169
  - LICENSE.txt
154
170
  - README.md
@@ -156,11 +172,15 @@ files:
156
172
  - bin/console
157
173
  - bin/setup
158
174
  - database_cleaner-active_record.gemspec
175
+ - docker-compose.db.yml
176
+ - docker-compose.yml
159
177
  - gemfiles/.bundle/config
160
- - gemfiles/rails_5.1.gemfile
161
- - gemfiles/rails_5.2.gemfile
162
- - gemfiles/rails_6.0.gemfile
163
178
  - gemfiles/rails_6.1.gemfile
179
+ - gemfiles/rails_7.0.gemfile
180
+ - gemfiles/rails_7.1.gemfile
181
+ - gemfiles/rails_7.2.gemfile
182
+ - gemfiles/rails_8.0.gemfile
183
+ - gemfiles/rails_edge.gemfile
164
184
  - lib/database_cleaner-active_record.rb
165
185
  - lib/database_cleaner/active_record.rb
166
186
  - lib/database_cleaner/active_record/base.rb
@@ -172,8 +192,8 @@ files:
172
192
  homepage: https://github.com/DatabaseCleaner/database_cleaner-active_record
173
193
  licenses:
174
194
  - MIT
175
- metadata: {}
176
- post_install_message:
195
+ metadata:
196
+ changelog_uri: https://github.com/DatabaseCleaner/database_cleaner-active_record/blob/main/CHANGELOG.md
177
197
  rdoc_options: []
178
198
  require_paths:
179
199
  - lib
@@ -188,8 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
188
208
  - !ruby/object:Gem::Version
189
209
  version: '0'
190
210
  requirements: []
191
- rubygems_version: 3.1.4
192
- signing_key:
211
+ rubygems_version: 3.6.2
193
212
  specification_version: 4
194
213
  summary: Strategies for cleaning databases using ActiveRecord. Can be used to ensure
195
214
  a clean state for testing.
data/.travis.yml DELETED
@@ -1,25 +0,0 @@
1
- language: ruby
2
- services:
3
- - mysql
4
- - postgresql
5
- rvm:
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