From 754cfdba115d95036dd46c5540c89d92439bd044 Mon Sep 17 00:00:00 2001 From: barbie-bot Date: Sun, 1 Mar 2026 04:19:06 +0000 Subject: [PATCH] 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 --- .gitea/workflows/test.yml | 95 ++++++++++++++++++++++++++++++++++++ lib/tasks/test_multi_db.rake | 50 +++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 .gitea/workflows/test.yml create mode 100644 lib/tasks/test_multi_db.rake diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml new file mode 100644 index 0000000..512f387 --- /dev/null +++ b/.gitea/workflows/test.yml @@ -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" diff --git a/lib/tasks/test_multi_db.rake b/lib/tasks/test_multi_db.rake new file mode 100644 index 0000000..d0b8a84 --- /dev/null +++ b/lib/tasks/test_multi_db.rake @@ -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