From 86b115fcbec1b8e6aa359b935f89889aaf5e0d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Esteves?= Date: Wed, 1 Oct 2025 20:39:22 +0100 Subject: [PATCH 1/2] Add test instructions --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 913962a..d35e570 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,16 @@ concurrent access to **any instance of a model**. Their coarseness means they aren't going to be commonly applicable, and they can be a source of [deadlocks](http://en.wikipedia.org/wiki/Deadlock). +## Running Tests + +To setup the project and run the whole test suite: + +1. Have Docker running +2. `echo -e "DATABASE_URL_PG=postgres://with_advisory:with_advisory_pass@localhost:5433/with_advisory_lock_test\nDATABASE_URL_MYSQL=mysql2://with_advisory:with_advisory_pass@127.0.0.1:3366/with_advisory_lock_test" > .env` +3. `make` + +Alternatively to `make`, run `bin/rails test` to skip database and dependency setup. + ## FAQ ### Transactions and Advisory Locks From 92132bb58ed6197307cb36b3ce468eefd041f306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Esteves?= Date: Wed, 1 Oct 2025 21:23:51 +0100 Subject: [PATCH 2/2] Use env var in more places --- README.md | 2 +- bin/setup_test_db | 14 ++++++++------ docker-compose.yml | 12 ++++++------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d35e570..606abb7 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ aren't going to be commonly applicable, and they can be a source of To setup the project and run the whole test suite: 1. Have Docker running -2. `echo -e "DATABASE_URL_PG=postgres://with_advisory:with_advisory_pass@localhost:5433/with_advisory_lock_test\nDATABASE_URL_MYSQL=mysql2://with_advisory:with_advisory_pass@127.0.0.1:3366/with_advisory_lock_test" > .env` +2. `echo -e "DB_USER=with_advisory\nDB_PASSWORD=with_advisory_pass\nDB_NAME=with_advisory_lock_test\nDATABASE_URL_PG=postgres://\$DB_USER:\$DB_PASSWORD@localhost:5433/\$DB_NAME\nDATABASE_URL_MYSQL=mysql2://\$DB_USER:\$DB_PASSWORD@127.0.0.1:3366/\$DB_NAME" > .env` 3. `make` Alternatively to `make`, run `bin/rails test` to skip database and dependency setup. diff --git a/bin/setup_test_db b/bin/setup_test_db index 8a0cc26..81daa55 100755 --- a/bin/setup_test_db +++ b/bin/setup_test_db @@ -3,6 +3,8 @@ require 'bundler/setup' require 'active_record' +require 'dotenv' +Dotenv.load # Setup PostgreSQL database puts 'Setting up PostgreSQL test database...' @@ -10,9 +12,9 @@ ActiveRecord::Base.establish_connection( adapter: 'postgresql', host: 'localhost', port: 5433, - database: 'with_advisory_lock_test', - username: 'with_advisory', - password: 'with_advisory_pass' + database: ENV['DB_NAME'], + username: ENV['DB_USER'], + password: ENV['DB_PASSWORD'] ) ActiveRecord::Schema.define(version: 1) do @@ -36,9 +38,9 @@ ActiveRecord::Base.establish_connection( adapter: 'mysql2', host: '127.0.0.1', port: 3366, - database: 'with_advisory_lock_test', - username: 'with_advisory', - password: 'with_advisory_pass' + database: ENV['DB_NAME'], + username: ENV['DB_USER'], + password: ENV['DB_PASSWORD'] ) ActiveRecord::Schema.define(version: 1) do diff --git a/docker-compose.yml b/docker-compose.yml index a40d61c..26fca3e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,17 +2,17 @@ services: pg: image: postgres:17-alpine environment: - POSTGRES_USER: with_advisory - POSTGRES_PASSWORD: with_advisory_pass - POSTGRES_DB: with_advisory_lock_test + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} + POSTGRES_DB: ${DB_NAME} ports: - "5433:5432" mysql: image: mysql:8 environment: - MYSQL_USER: with_advisory - MYSQL_PASSWORD: with_advisory_pass - MYSQL_DATABASE: with_advisory_lock_test + MYSQL_USER: ${DB_USER} + MYSQL_PASSWORD: ${DB_PASSWORD} + MYSQL_DATABASE: ${DB_NAME} MYSQL_RANDOM_ROOT_PASSWORD: "yes" MYSQL_ROOT_HOST: '%' ports: