Skip to content

Commit 3841a1a

Browse files
committed
Add MySQL and PostgreSQL to CI database matrix
The existing CI matrix runs exclusively against SQLite, which hides any adapter-specific behavior (upsert semantics, conflict targets, column types, etc.). This adds two new CI jobs — one for MySQL 8.0 and one for PostgreSQL 16 — that run the full spec suite against a live database service, using the latest supported Ruby/Rails/ActiveAdmin combination. The adapter is selected by the `DB` env var (sqlite | mysql | postgres). The test Rails app is cached per-adapter (`spec/rails/rails-<ver>-<db>`) and the Gemfile loads the matching driver conditionally. Host, port, and credentials are all driven from env vars so the same setup works locally and in CI.
1 parent 064ef88 commit 3841a1a

File tree

5 files changed

+122
-5
lines changed

5 files changed

+122
-5
lines changed

.github/workflows/test.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,71 @@ jobs:
2727
bundler-cache: true
2828
- name: Run tests
2929
run: bundle exec rspec spec
30+
test-mysql:
31+
name: DB mysql
32+
runs-on: ubuntu-latest
33+
env:
34+
RAILS: '8.0.0'
35+
AA: '3.5.1'
36+
DB: mysql
37+
DB_HOST: 127.0.0.1
38+
DB_PORT: 3306
39+
DB_USERNAME: root
40+
DB_PASSWORD: root
41+
services:
42+
mysql:
43+
image: mysql:8.0
44+
env:
45+
MYSQL_ROOT_PASSWORD: root
46+
MYSQL_DATABASE: active_admin_import_test
47+
ports:
48+
- 3306:3306
49+
options: >-
50+
--health-cmd="mysqladmin ping -h localhost -uroot -proot"
51+
--health-interval=10s
52+
--health-timeout=5s
53+
--health-retries=10
54+
steps:
55+
- uses: actions/checkout@v4
56+
- uses: ruby/setup-ruby@v1
57+
with:
58+
ruby-version: '3.4'
59+
bundler-cache: true
60+
- name: Run tests
61+
run: bundle exec rspec spec
62+
test-postgres:
63+
name: DB postgres
64+
runs-on: ubuntu-latest
65+
env:
66+
RAILS: '8.0.0'
67+
AA: '3.5.1'
68+
DB: postgres
69+
DB_HOST: 127.0.0.1
70+
DB_PORT: 5432
71+
DB_USERNAME: postgres
72+
DB_PASSWORD: postgres
73+
services:
74+
postgres:
75+
image: postgres:16
76+
env:
77+
POSTGRES_USER: postgres
78+
POSTGRES_PASSWORD: postgres
79+
POSTGRES_DB: active_admin_import_test
80+
ports:
81+
- 5432:5432
82+
options: >-
83+
--health-cmd="pg_isready -U postgres"
84+
--health-interval=10s
85+
--health-timeout=5s
86+
--health-retries=10
87+
steps:
88+
- uses: actions/checkout@v4
89+
- uses: ruby/setup-ruby@v1
90+
with:
91+
ruby-version: '3.4'
92+
bundler-cache: true
93+
- name: Run tests
94+
run: bundle exec rspec spec
3095
coverage:
3196
name: Coverage
3297
runs-on: ubuntu-latest

Gemfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ gem 'sass-rails'
1212
group :test do
1313
gem 'simplecov', require: false
1414
gem 'rspec-rails'
15-
gem 'sqlite3', '~> 2.0'
15+
case ENV['DB']
16+
when 'mysql'
17+
gem 'mysql2'
18+
when 'postgres', 'postgresql'
19+
gem 'pg'
20+
else
21+
gem 'sqlite3', '~> 2.0'
22+
end
1623
gem 'database_cleaner'
1724
gem 'capybara'
1825
gem 'cuprite'

spec/spec_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
ENV['RAILS_ENV'] = 'test'
1414
require 'rails'
1515
ENV['RAILS'] = Rails.version
16-
ENV['RAILS_ROOT'] = File.expand_path("../rails/rails-#{ENV['RAILS']}", __FILE__)
16+
ENV['DB'] ||= 'sqlite'
17+
ENV['RAILS_ROOT'] = File.expand_path("../rails/rails-#{ENV['RAILS']}-#{ENV['DB']}", __FILE__)
1718
system 'rake setup' unless File.exist?(ENV['RAILS_ROOT'])
1819

1920
require 'active_model'

spec/support/rails_template.rb

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
create_file "app/assets/config/manifest.js", skip: true
22

3+
db = ENV['DB'] || 'sqlite'
4+
case db
5+
when 'mysql'
6+
remove_file 'config/database.yml'
7+
create_file 'config/database.yml', <<~YAML
8+
default: &default
9+
adapter: mysql2
10+
encoding: utf8mb4
11+
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
12+
host: <%= ENV.fetch("DB_HOST", "127.0.0.1") %>
13+
port: <%= ENV.fetch("DB_PORT", 3306) %>
14+
username: <%= ENV.fetch("DB_USERNAME", "root") %>
15+
password: <%= ENV.fetch("DB_PASSWORD", "root") %>
16+
17+
test:
18+
<<: *default
19+
database: active_admin_import_test
20+
YAML
21+
when 'postgres', 'postgresql'
22+
remove_file 'config/database.yml'
23+
create_file 'config/database.yml', <<~YAML
24+
default: &default
25+
adapter: postgresql
26+
encoding: unicode
27+
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
28+
host: <%= ENV.fetch("DB_HOST", "127.0.0.1") %>
29+
port: <%= ENV.fetch("DB_PORT", 5432) %>
30+
username: <%= ENV.fetch("DB_USERNAME", "postgres") %>
31+
password: <%= ENV.fetch("DB_PASSWORD", "postgres") %>
32+
33+
test:
34+
<<: *default
35+
database: active_admin_import_test
36+
YAML
37+
end
38+
339
generate :model, 'author name:string{10}:uniq last_name:string birthday:date --force'
440
generate :model, 'post title:string:uniq body:text request_ip:string author:references --force'
541
generate :model, 'post_comment body:text post:references --force'
@@ -19,6 +55,6 @@
1955

2056
run 'rm -rf test'
2157
route "root :to => 'admin/dashboard#index'"
22-
rake 'db:migrate'
58+
rake 'db:create db:migrate'
2359

2460
run 'rm -f Gemfile Gemfile.lock'

tasks/test.rake

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ desc "Creates a test rails app for the specs to run against"
22
task :setup do
33
require 'rails/version'
44

5-
rails_new_opts = %w(
5+
db = ENV['DB'] || 'sqlite'
6+
rails_db = case db
7+
when 'mysql' then 'mysql'
8+
when 'postgres', 'postgresql' then 'postgresql'
9+
else 'sqlite3'
10+
end
11+
12+
rails_new_opts = %W(
613
--skip-turbolinks
714
--skip-spring
815
--skip-bootsnap
16+
-d #{rails_db}
917
-m
1018
spec/support/rails_template.rb
1119
)
12-
system "bundle exec rails new spec/rails/rails-#{Rails::VERSION::STRING} #{rails_new_opts.join(' ')}"
20+
system "bundle exec rails new spec/rails/rails-#{Rails::VERSION::STRING}-#{db} #{rails_new_opts.join(' ')}"
1321
end

0 commit comments

Comments
 (0)