From 7ae76654ce15cbf9fc2ed24722be38b97bbc44bc Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Thu, 15 May 2025 10:24:42 +0100 Subject: [PATCH 01/11] Fixes https://github.com/DatabaseCleaner/database_cleaner-active_record/issues/120 --- lib/database_cleaner/active_record/truncation.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/database_cleaner/active_record/truncation.rb b/lib/database_cleaner/active_record/truncation.rb index ce4c095..8e6be4c 100644 --- a/lib/database_cleaner/active_record/truncation.rb +++ b/lib/database_cleaner/active_record/truncation.rb @@ -21,7 +21,7 @@ def initialize(opts={}) def clean connection.disable_referential_integrity do if pre_count? && connection.respond_to?(:pre_count_truncate_tables) - connection.pre_count_truncate_tables(tables_to_clean(connection)) + connection.pre_count_truncate_tables(tables_to_clean(connection), { truncate_option: @truncate_option }) else connection.truncate_tables(tables_to_clean(connection), { truncate_option: @truncate_option }) end @@ -109,8 +109,8 @@ def truncate_tables(tables, opts) end module AbstractMysqlAdapter - def pre_count_truncate_tables(tables) - truncate_tables(pre_count_tables(tables)) + def pre_count_truncate_tables(tables, opts = {}) + truncate_tables(pre_count_tables(tables), opts) end def pre_count_tables(tables) @@ -157,8 +157,8 @@ def truncate_tables(tables, opts) tables.each { |t| truncate_table(t) } end - def pre_count_truncate_tables(tables) - truncate_tables(pre_count_tables(tables)) + def pre_count_truncate_tables(tables, opts = {}) + truncate_tables(pre_count_tables(tables), opts) end def pre_count_tables(tables) @@ -200,8 +200,8 @@ def truncate_tables(table_names, opts) execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} RESTART IDENTITY #{opts[:truncate_option]};") end - def pre_count_truncate_tables(tables) - truncate_tables(pre_count_tables(tables)) + def pre_count_truncate_tables(tables, opts = {}) + truncate_tables(pre_count_tables(tables), opts) end def pre_count_tables(tables) From 35e666b6c51831aca9ebbcc3d36bd99c7c1dff8f Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Thu, 11 Jul 2024 23:54:07 -0400 Subject: [PATCH 02/11] Add docker configuration for development This makes it easier to run the test suite in your local dev environment --- Dockerfile | 16 ++++++++++++ docker-compose.yaml | 36 +++++++++++++++++++++++++++ spec/support/sample.docker.config.yml | 33 ++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yaml create mode 100644 spec/support/sample.docker.config.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..aef81cb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM ruby:3.3 + +# Set the working directory in the container +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY . /app + +# Copy the sample config to the actual config (performed at build time) +RUN cp spec/support/sample.docker.config.yml spec/support/config.yml + +# Install any needed packages specified in Gemfile +RUN bundle install + +# Command to run the application +CMD ["bash"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..c054b33 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,36 @@ +services: + ruby: + image: ruby:3.3 # Manually change this based on the desired Ruby version + working_dir: /app + build: . + environment: + BUNDLE_GEMFILE: gemfiles/rails_7.2.gemfile # Manually change this based on the desired Rails version + depends_on: + - mysql + - postgres + + mysql: + image: mysql:8.4 + environment: + MYSQL_ALLOW_EMPTY_PASSWORD: yes + ports: + - "3306:3306" + healthcheck: + test: ["CMD", "mysqladmin", "ping"] + interval: 10s + timeout: 5s + retries: 3 + + postgres: + image: postgres:16 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + PGUSER: postgres + ports: + - "5432:5432" + healthcheck: + test: ["CMD-SHELL", "pg_isready"] + interval: 10s + timeout: 5s + retries: 5 \ No newline at end of file diff --git a/spec/support/sample.docker.config.yml b/spec/support/sample.docker.config.yml new file mode 100644 index 0000000..5faa76b --- /dev/null +++ b/spec/support/sample.docker.config.yml @@ -0,0 +1,33 @@ +mysql2: + adapter: mysql2 + database: database_cleaner_test + username: root + password: + host: mysql + port: 3306 + encoding: utf8 + +trilogy: + adapter: trilogy + database: database_cleaner_test + username: root + password: + host: mysql + port: 3306 + encoding: utf8 + +postgres: + adapter: postgresql + database: database_cleaner_test + username: postgres + password: postgres + host: postgres + encoding: unicode + template: template0 + +sqlite3: + adapter: sqlite3 + database: tmp/database_cleaner_test.sqlite3 + pool: 5 + timeout: 5000 + encoding: utf8 From 842b19ded6199bd0e97554b5c7628e2113880a8c Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Thu, 11 Jul 2024 23:57:32 -0400 Subject: [PATCH 03/11] Add a development section for contributors This should make it easier for people to set up the library and run the test suite within Docker --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index c05de91..7f6654b 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,18 @@ test: min_messages: WARNING +## Development + +If you want to test this library locally, you can do so like this: + +``` +docker compose run --build --rm ruby /bin/bash +bundle +bundle exec rake +``` + +You will need Docker installed. The configuration is set up for the latest Ruby, Rails, Postgres, and MySQL versions. + ## COPYRIGHT See [LICENSE](LICENSE) for details. From ab0fce4454955e035b15c842c94d17487378d0d4 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Mon, 7 Jul 2025 15:21:46 -0400 Subject: [PATCH 04/11] Added ruby service to make it easier to get started with your contribution --- docker-compose.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 41cf4c2..f8a7a0f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,3 +17,13 @@ services: ports: - "127.0.0.1:6379:6379" command: redis-server --save 20 1 --loglevel warning + ruby: + image: ruby:3.3 # Manually change this based on the desired Ruby version + working_dir: /app + build: . + environment: + BUNDLE_GEMFILE: gemfiles/rails_7.2.gemfile # Manually change this based on the desired Rails version + depends_on: + - mysql + - postgres + - redis \ No newline at end of file From c0fe3ecf9f35b1a74ded4e7d451755e80a71e384 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Mon, 7 Jul 2025 15:22:10 -0400 Subject: [PATCH 05/11] Update password to match Docker configuration --- spec/support/sample.docker.config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/support/sample.docker.config.yml b/spec/support/sample.docker.config.yml index 5faa76b..728002d 100644 --- a/spec/support/sample.docker.config.yml +++ b/spec/support/sample.docker.config.yml @@ -2,7 +2,7 @@ mysql2: adapter: mysql2 database: database_cleaner_test username: root - password: + password: mysql host: mysql port: 3306 encoding: utf8 @@ -11,7 +11,7 @@ trilogy: adapter: trilogy database: database_cleaner_test username: root - password: + password: mysql host: mysql port: 3306 encoding: utf8 From 240f3518c1df2b5d6b7ba5c08807bc6d7f5fa5ef Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Mon, 7 Jul 2025 15:22:23 -0400 Subject: [PATCH 06/11] Removed outdated docker-compose file --- docker-compose.yaml | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 docker-compose.yaml diff --git a/docker-compose.yaml b/docker-compose.yaml deleted file mode 100644 index c054b33..0000000 --- a/docker-compose.yaml +++ /dev/null @@ -1,36 +0,0 @@ -services: - ruby: - image: ruby:3.3 # Manually change this based on the desired Ruby version - working_dir: /app - build: . - environment: - BUNDLE_GEMFILE: gemfiles/rails_7.2.gemfile # Manually change this based on the desired Rails version - depends_on: - - mysql - - postgres - - mysql: - image: mysql:8.4 - environment: - MYSQL_ALLOW_EMPTY_PASSWORD: yes - ports: - - "3306:3306" - healthcheck: - test: ["CMD", "mysqladmin", "ping"] - interval: 10s - timeout: 5s - retries: 3 - - postgres: - image: postgres:16 - environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres - PGUSER: postgres - ports: - - "5432:5432" - healthcheck: - test: ["CMD-SHELL", "pg_isready"] - interval: 10s - timeout: 5s - retries: 5 \ No newline at end of file From 85896e46a044cc1196e228759c1cb94b8295ca82 Mon Sep 17 00:00:00 2001 From: Ariel Juodziukynas Date: Mon, 7 Jul 2025 17:31:05 -0400 Subject: [PATCH 07/11] Use older mysql to avoid trilogy error, fix docker compose and instructions, mount code for easier development --- CONTRIBUTE.md | 19 ++++++++++++++--- Dockerfile | 9 +++++--- docker-compose.db.yml | 19 +++++++++++++++++ docker-compose.yml | 21 ++++++++----------- .../active_record/truncation.rb | 14 ++++++------- 5 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 docker-compose.db.yml diff --git a/CONTRIBUTE.md b/CONTRIBUTE.md index 4c0940c..79e1886 100644 --- a/CONTRIBUTE.md +++ b/CONTRIBUTE.md @@ -12,15 +12,28 @@ upstream: The gem uses Appraisal to configure different Gemfiles to test different Rails versions. -- You can run all the databases through docker if needed with `docker compose up` (you can also have them running on your system, just comment out the ones you don't need from the `docker-compose.yml` file) -- Copy `spec/support/sample.config.yml` to `spec/support/config.yml` and edit it +### Run tests without Docker (or using Docker only for the databases) + +- 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) +- Copy `spec/support/sample.config.yml` to `spec/support/config.yml` and edit it as needed - `BUNDLE_GEMFILE=gemfiles/rails_6.1.gemfile bundle install` (change `6.1` with any version from the `gemfiles` directory) - `BUNDLE_GEMFILE=gemfiles/rails_6.1.gemfile bundle exec rake` Note that if you don't have all the supported databases installed and running, some tests will fail. -> Note that you can check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work +> Check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work + +### Run tests with Docker + +- Open `docker-compose.yml` and configure the Ruby version and Gemfile file to use +- Copy `spec/support/sample.docker.config.yml` to `spec/support/config.yml` (not this config file is specific for the Docker setup) +- Run `docker compose up` to start the container, run the tests, and exit +- Run `docker compose run ruby bash` to open `bash` inside the container for more control, run `rake` to run the tests + +> 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` + +> Check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work ## 3. Prepare your contribution diff --git a/Dockerfile b/Dockerfile index aef81cb..383ced0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,16 @@ -FROM ruby:3.3 +ARG RUBY_VERSION=3.3 +FROM ruby:${RUBY_VERSION} # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app +# This is copied so we can bundle the application, but it's replaced +# by a mounted volume with the current code when executed with docker compose COPY . /app -# Copy the sample config to the actual config (performed at build time) -RUN cp spec/support/sample.docker.config.yml spec/support/config.yml +ARG BUNDLE_GEMFILE=Gemfile +ENV BUNDLE_GEMFILE=${BUNDLE_GEMFILE} # Install any needed packages specified in Gemfile RUN bundle install diff --git a/docker-compose.db.yml b/docker-compose.db.yml new file mode 100644 index 0000000..300d6a7 --- /dev/null +++ b/docker-compose.db.yml @@ -0,0 +1,19 @@ +services: + postgres: + image: postgres:16 # specify the version needed for a given app + environment: + - POSTGRES_PASSWORD=postgres # this is required + ports: + - "127.0.0.1:5432:5432" # so we can use `localhost` as the host + mysql: + image: mysql:5.7 + environment: + - MYSQL_ROOT_PASSWORD=mysql + ports: + - "127.0.0.1:3306:3306" + redis: + image: redis:6.2-alpine + restart: always + ports: + - "127.0.0.1:6379:6379" + command: redis-server --save 20 1 --loglevel warning diff --git a/docker-compose.yml b/docker-compose.yml index f8a7a0f..eb13842 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,26 +3,23 @@ services: image: postgres:16 # specify the version needed for a given app environment: - POSTGRES_PASSWORD=postgres # this is required - ports: - - "127.0.0.1:5432:5432" # so we can use `localhost` as the host mysql: - image: mysql:9.3 + image: mysql:5.7 environment: - MYSQL_ROOT_PASSWORD=mysql - ports: - - "127.0.0.1:3306:3306" redis: image: redis:6.2-alpine restart: always - ports: - - "127.0.0.1:6379:6379" command: redis-server --save 20 1 --loglevel warning ruby: - image: ruby:3.3 # Manually change this based on the desired Ruby version - working_dir: /app - build: . - environment: - BUNDLE_GEMFILE: gemfiles/rails_7.2.gemfile # Manually change this based on the desired Rails version + build: + context: . + args: + BUNDLE_GEMFILE: gemfiles/rails_7.2.gemfile # Manually change this based on the desired Rails version + RUBY_VERSION: 3.3 # Manually change this based on the desired Ruby version + volumes: + - ".:/app:delegated" + command: rake depends_on: - mysql - postgres diff --git a/lib/database_cleaner/active_record/truncation.rb b/lib/database_cleaner/active_record/truncation.rb index 8e6be4c..ce4c095 100644 --- a/lib/database_cleaner/active_record/truncation.rb +++ b/lib/database_cleaner/active_record/truncation.rb @@ -21,7 +21,7 @@ def initialize(opts={}) def clean connection.disable_referential_integrity do if pre_count? && connection.respond_to?(:pre_count_truncate_tables) - connection.pre_count_truncate_tables(tables_to_clean(connection), { truncate_option: @truncate_option }) + connection.pre_count_truncate_tables(tables_to_clean(connection)) else connection.truncate_tables(tables_to_clean(connection), { truncate_option: @truncate_option }) end @@ -109,8 +109,8 @@ def truncate_tables(tables, opts) end module AbstractMysqlAdapter - def pre_count_truncate_tables(tables, opts = {}) - truncate_tables(pre_count_tables(tables), opts) + def pre_count_truncate_tables(tables) + truncate_tables(pre_count_tables(tables)) end def pre_count_tables(tables) @@ -157,8 +157,8 @@ def truncate_tables(tables, opts) tables.each { |t| truncate_table(t) } end - def pre_count_truncate_tables(tables, opts = {}) - truncate_tables(pre_count_tables(tables), opts) + def pre_count_truncate_tables(tables) + truncate_tables(pre_count_tables(tables)) end def pre_count_tables(tables) @@ -200,8 +200,8 @@ def truncate_tables(table_names, opts) execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} RESTART IDENTITY #{opts[:truncate_option]};") end - def pre_count_truncate_tables(tables, opts = {}) - truncate_tables(pre_count_tables(tables), opts) + def pre_count_truncate_tables(tables) + truncate_tables(pre_count_tables(tables)) end def pre_count_tables(tables) From 9b77d0c5163fe7e2a66c40837ef31f85d10ce3ac Mon Sep 17 00:00:00 2001 From: Ariel Juodziukynas Date: Mon, 7 Jul 2025 17:33:00 -0400 Subject: [PATCH 08/11] Update readme to point to CONTRIBUTE.md file --- README.md | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/README.md b/README.md index 7f6654b..3d7d49c 100644 --- a/README.md +++ b/README.md @@ -104,15 +104,7 @@ test: ## Development -If you want to test this library locally, you can do so like this: - -``` -docker compose run --build --rm ruby /bin/bash -bundle -bundle exec rake -``` - -You will need Docker installed. The configuration is set up for the latest Ruby, Rails, Postgres, and MySQL versions. +Check the CONTRIBUTE.md file for instructions running tests with and withour Docker. ## COPYRIGHT From 4f69ba7ce24ce6d31e7d35ac2fe49ad5fba7537d Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Wed, 30 Jul 2025 13:33:29 -0400 Subject: [PATCH 09/11] Run ./bin/setup so that we have the config file in place for tests --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 383ced0..691a8bb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ ARG BUNDLE_GEMFILE=Gemfile ENV BUNDLE_GEMFILE=${BUNDLE_GEMFILE} # Install any needed packages specified in Gemfile -RUN bundle install +RUN ./bin/setup # Command to run the application CMD ["bash"] \ No newline at end of file From b0f6c335789ef9b4c3691ccf1820c2a752117ca5 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Wed, 30 Jul 2025 13:33:57 -0400 Subject: [PATCH 10/11] Relax dependency --- database_cleaner-active_record.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database_cleaner-active_record.gemspec b/database_cleaner-active_record.gemspec index 7350835..147445a 100644 --- a/database_cleaner-active_record.gemspec +++ b/database_cleaner-active_record.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| spec.executables = [] spec.require_paths = ["lib"] - spec.add_dependency "database_cleaner-core", "~>2.1.0" + spec.add_dependency "database_cleaner-core", "~>2.0" spec.add_dependency "activerecord", ">= 5.a" spec.add_development_dependency "bundler" From 54dd580bba42db108f7a624a5a558592e42ef325 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Wed, 30 Jul 2025 13:35:14 -0400 Subject: [PATCH 11/11] Add a line about the Docker + DX change --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb2869c..3314fbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Provide a 'Changelog' link on Rubygems: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/114 * Fix bundling and CONTRIBUTE.md instructions: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/123 * Fix order of arguments in `truncate_tables` expectation https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/124 +* Add Docker to make it easier to run tests locally for maintainers and contributors https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/109 ## v2.2.1 2025-05-13