Skip to content

Commit 935e7e5

Browse files
seurosCopilot
andauthored
feat: support of multidb (#116)
* feat!: refactor to adapter injection pattern with multi-db support Release-As: 6.0.0.alpha1 BREAKING CHANGE: Remove private APIs (Base, DatabaseAdapterSupport). Add full mixed adapter support for PostgreSQL/MySQL in same app. Add JRuby compatibility. Co-authored-by: Copilot <[email protected]>
1 parent 5d65109 commit 935e7e5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1218
-925
lines changed

.github/workflows/ci-mysql8.yml

Lines changed: 0 additions & 59 deletions
This file was deleted.

.github/workflows/ci-postgresql.yml

Lines changed: 0 additions & 61 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
8+
concurrency:
9+
group: ci-${{ github.head_ref }}
10+
cancel-in-progress: true
11+
12+
jobs:
13+
minitest:
14+
runs-on: ubuntu-latest
15+
name: CI Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
16+
services:
17+
postgres:
18+
image: 'postgres:17-alpine'
19+
ports:
20+
- '5432'
21+
env:
22+
POSTGRES_USER: with_advisory
23+
POSTGRES_PASSWORD: with_advisory_pass
24+
POSTGRES_DB: with_advisory_lock_test
25+
options: >-
26+
--health-cmd pg_isready
27+
--health-interval 10s
28+
--health-timeout 5s
29+
--health-retries 5
30+
mysql:
31+
image: mysql/mysql-server
32+
ports:
33+
- 3306
34+
env:
35+
MYSQL_USER: with_advisory
36+
MYSQL_PASSWORD: with_advisory_pass
37+
MYSQL_DATABASE: with_advisory_lock_test
38+
MYSQL_ROOT_HOST: '%'
39+
strategy:
40+
fail-fast: false
41+
matrix:
42+
ruby:
43+
- '3.3'
44+
- '3.4'
45+
- 'truffleruby'
46+
rails:
47+
- 7.1
48+
- 7.2
49+
- "8.0"
50+
include:
51+
- ruby: jruby
52+
rails: 7.1
53+
env:
54+
ACTIVERECORD_VERSION: ${{ matrix.rails }}
55+
RAILS_ENV: test
56+
steps:
57+
- name: Checkout
58+
uses: actions/checkout@v4
59+
60+
- name: Setup Ruby
61+
uses: ruby/setup-ruby@v1
62+
with:
63+
ruby-version: ${{ matrix.ruby }}
64+
bundler-cache: true
65+
rubygems: latest
66+
67+
- name: Setup test databases
68+
env:
69+
DATABASE_URL_PG: postgres://with_advisory:with_advisory_pass@localhost:${{ job.services.postgres.ports[5432] }}/with_advisory_lock_test
70+
DATABASE_URL_MYSQL: mysql2://with_advisory:[email protected]:${{ job.services.mysql.ports[3306] }}/with_advisory_lock_test
71+
run: |
72+
cd test/dummy
73+
bundle exec rake db:test:prepare
74+
75+
- name: Test
76+
env:
77+
DATABASE_URL_PG: postgres://with_advisory:with_advisory_pass@localhost:${{ job.services.postgres.ports[5432] }}/with_advisory_lock_test
78+
DATABASE_URL_MYSQL: mysql2://with_advisory:[email protected]:${{ job.services.mysql.ports[3306] }}/with_advisory_lock_test
79+
WITH_ADVISORY_LOCK_PREFIX: ${{ github.run_id }}
80+
run: bin/rails test

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ test/tmp
1818
test/version_tmp
1919
tmp
2020
*.iml
21-
.env
21+
.env
22+
test/dummy/log/

Appraisals

Lines changed: 0 additions & 33 deletions
This file was deleted.

Gemfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
gemspec
6+
7+
gem 'rake'
8+
9+
# Gems that will be removed from default gems in Ruby 3.5.0
10+
gem 'benchmark'
11+
gem 'logger'
12+
gem 'ostruct'
13+
14+
activerecord_version = ENV.fetch('ACTIVERECORD_VERSION', '7.1')
15+
16+
gem 'activerecord', "~> #{activerecord_version}.0"
17+
18+
gem 'dotenv'
19+
gem 'railties'
20+
21+
platforms :ruby do
22+
gem 'mysql2'
23+
gem 'pg'
24+
gem 'trilogy'
25+
end
26+
27+
platforms :jruby do
28+
# JRuby JDBC adapters only support Rails 7.1 currently
29+
if activerecord_version == '7.1'
30+
gem 'activerecord-jdbcmysql-adapter', '~> 71.0'
31+
gem 'activerecord-jdbcpostgresql-adapter', '~> 71.0'
32+
end
33+
end

Makefile

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
.PHONY: test-pg test-mysql
1+
.PHONY: test
22

3-
test-pg:
4-
docker compose up -d pg
5-
sleep 10 # give some time for the service to start
6-
DATABASE_URL_PG=postgres://with_advisory:with_advisory_pass@localhost/with_advisory_lock_test appraisal rake test
3+
test: setup-db
4+
bin/rails test
75

8-
test-mysql:
9-
docker compose up -d mysql
10-
sleep 10 # give some time for the service to start
11-
DATABASE_URL_MYSQL=mysql2://with_advisory:[email protected]:3306/with_advisory_lock_test appraisal rake test
12-
13-
14-
test: test-pg test-mysql
6+
setup-db:
7+
docker compose up -d
8+
sleep 2
9+
bundle
10+
bin/setup_test_db

Rakefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
require "bundler/gem_tasks"
1+
# frozen_string_literal: true
2+
3+
require 'bundler/gem_tasks'
24

35
require 'yard'
46
YARD::Rake::YardocTask.new do |t|
@@ -14,4 +16,8 @@ Rake::TestTask.new do |t|
1416
t.verbose = true
1517
end
1618

17-
task :default => :test
19+
# Load Rails tasks from dummy app to get db:test:prepare
20+
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
21+
load 'rails/tasks/engine.rake' if File.exist?(APP_RAKEFILE)
22+
23+
task default: :test

bin/rails

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This command will automatically be run when you run "rails" with Rails gems
55
# installed from the root of your application.
66

7-
ENGINE_ROOT = File.expand_path('../test/dummy', __dir__)
7+
ENGINE_ROOT = File.expand_path('..', __dir__)
88
APP_PATH = File.expand_path('../test/dummy/config/application', __dir__)
99

1010
# Set up gems listed in the Gemfile.

bin/sanity

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
check_port() {
5+
local port="$1"
6+
local name="$2"
7+
if nc -z localhost "$port" >/dev/null 2>&1; then
8+
echo "$name running on port $port"
9+
else
10+
echo "ERROR: $name is not running on port $port" >&2
11+
return 1
12+
fi
13+
}
14+
15+
main() {
16+
check_port 5433 "Postgresql"
17+
check_port 3366 "Mysql"
18+
}
19+
20+
main "$@"

0 commit comments

Comments
 (0)