forked from fixin.me/fixin.me
Add multi-adapter test support: SQLite + MySQL via Gitea Actions and rake task
- .gitea/workflows/test.yml: two parallel CI jobs (SQLite and MySQL), each generates its own database.yml inline and runs the test suite - lib/tasks/test_multi_db.rake: `rails test:all_adapters` runs both adapters sequentially using DATABASE_URL to switch at runtime Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
95
.gitea/workflows/test.yml
Normal file
95
.gitea/workflows/test.yml
Normal file
@@ -0,0 +1,95 @@
|
||||
name: Tests
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
test-sqlite:
|
||||
name: Tests (SQLite)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: '3.3'
|
||||
bundler-cache: true
|
||||
env:
|
||||
BUNDLE_WITH: "sqlite:development:test"
|
||||
|
||||
- name: Generate database.yml
|
||||
run: |
|
||||
cat > config/database.yml <<'EOF'
|
||||
test:
|
||||
adapter: sqlite3
|
||||
database: db/test.sqlite3
|
||||
pool: 5
|
||||
EOF
|
||||
|
||||
- name: Set up test database
|
||||
run: bin/rails db:create db:schema:load
|
||||
env:
|
||||
RAILS_ENV: test
|
||||
|
||||
- name: Run tests
|
||||
run: bin/rails test
|
||||
env:
|
||||
RAILS_ENV: test
|
||||
CI: "true"
|
||||
|
||||
test-mysql:
|
||||
name: Tests (MySQL)
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
services:
|
||||
mysql:
|
||||
image: mysql:8.0
|
||||
env:
|
||||
MYSQL_ROOT_PASSWORD: ""
|
||||
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
|
||||
MYSQL_DATABASE: fixin_test
|
||||
ports:
|
||||
- 3306:3306
|
||||
options: >-
|
||||
--health-cmd="mysqladmin ping"
|
||||
--health-interval=10s
|
||||
--health-timeout=5s
|
||||
--health-retries=3
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: '3.3'
|
||||
bundler-cache: true
|
||||
env:
|
||||
BUNDLE_WITH: "mysql:development:test"
|
||||
|
||||
- name: Generate database.yml
|
||||
run: |
|
||||
cat > config/database.yml <<'EOF'
|
||||
test:
|
||||
adapter: mysql2
|
||||
encoding: utf8mb4
|
||||
collation: utf8mb4_0900_as_ci
|
||||
database: fixin_test
|
||||
host: 127.0.0.1
|
||||
username: root
|
||||
password: ""
|
||||
pool: 5
|
||||
EOF
|
||||
|
||||
- name: Set up test database
|
||||
run: bin/rails db:schema:load
|
||||
env:
|
||||
RAILS_ENV: test
|
||||
|
||||
- name: Run tests
|
||||
run: bin/rails test
|
||||
env:
|
||||
RAILS_ENV: test
|
||||
CI: "true"
|
||||
50
lib/tasks/test_multi_db.rake
Normal file
50
lib/tasks/test_multi_db.rake
Normal file
@@ -0,0 +1,50 @@
|
||||
namespace :test do
|
||||
desc "Run Rails tests against all supported database adapters (SQLite, MySQL)"
|
||||
task :all_adapters do
|
||||
# DATABASE_URL overrides the adapter from database.yml at runtime.
|
||||
# MySQL requires the mysql2 gem: bundle install --with mysql
|
||||
adapters = {
|
||||
"SQLite" => {
|
||||
"DATABASE_URL" => "sqlite3:db/test.sqlite3"
|
||||
},
|
||||
"MySQL" => {
|
||||
"DATABASE_URL" => format(
|
||||
"mysql2://%s:%s@%s/%s",
|
||||
ENV.fetch("DATABASE_USERNAME", "root"),
|
||||
ENV.fetch("DATABASE_PASSWORD", ""),
|
||||
ENV.fetch("DATABASE_HOST", "127.0.0.1"),
|
||||
ENV.fetch("DATABASE_NAME", "fixin_test")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
failed = []
|
||||
|
||||
adapters.each do |name, extra_env|
|
||||
puts "\n#{"=" * 60}"
|
||||
puts " Running tests with #{name}"
|
||||
puts "=" * 60
|
||||
|
||||
env = ENV.to_h.merge("RAILS_ENV" => "test").merge(extra_env)
|
||||
|
||||
# Reset test database; db:drop may fail on first run — that's fine
|
||||
system(env, "bin/rails db:drop")
|
||||
unless system(env, "bin/rails db:create db:schema:load")
|
||||
failed << "#{name} (database setup)"
|
||||
next
|
||||
end
|
||||
|
||||
failed << name unless system(env, "bin/rails test")
|
||||
end
|
||||
|
||||
puts "\n#{"=" * 60}"
|
||||
if failed.any?
|
||||
puts " FAILED: #{failed.join(", ")}"
|
||||
puts "=" * 60
|
||||
exit 1
|
||||
else
|
||||
puts " All adapters passed!"
|
||||
puts "=" * 60
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user