🚨 The Problem
Everything worked fine locally—but CI kept failing.
I was working on a Rails application that used two databases:
- A primary database (fully controlled by the app)
- A secondary database (owned by a different application)
Here’s the catch: 👉 The secondary database was read-only from my app’s perspective.
Locally, this wasn’t an issue because the database already existed and had the expected schema.
But in CI:
- The secondary database didn’t exist
- Rails tried to interact with it like a normal DB
- Tests failed with missing tables and connection errors
🔍 Root Cause
The issue came down to this:
👉 Rails CI setup assumes it can create and migrate all databases.
That assumption breaks when:
- A database is managed externally
- Your app does not have write/migration access
- Schema must be imported, not generated
So while this worked:
rails db:create:all
rails db:migrate:all
It failed for my setup because:
- The secondary DB could not be migrated
- Required tables were never created
- Tests relying on that DB failed immediately
🛠️ The Solution
The fix required treating each database based on its ownership and access level.
✅ Step 1: Handle Primary DB Normally
The primary database is fully controlled by the app, so standard setup works:
RAILS_ENV=test bundle exec rails db:create
RAILS_ENV=test bundle exec rails db:migrate
✅ Step 2: Restore Secondary DB from Dump
Since the secondary DB is:
- Managed by another app
- Read-only in this context
👉 The only reliable solution was to restore it from a dump file I stored a dump inside the project:
db/backups/secondary_test_dump.sql
Then restored it during CI setup:
psql -U postgres -d secondary_test_db < db/backups/secondary_test_dump.sql
✅ Step 3: Wire It into CI
Here’s what the CI setup looked like:
- name: Setup Primary DB
run: |
bundle exec rails db:create
bundle exec rails db:migrate
- name: Restore Secondary DB
run: |
psql -U postgres -d secondary_test_db < db/backups/secondary_test_dump.sql
- name: Run Tests
run: bundle exec rspec
📈 Results
After applying this approach:
✅ CI became stable.
✅ No more missing table errors.
✅ Tests behaved exactly like local.
✅ Clear separation of responsibilities between DBs.
🧩 Key Takeaways
- Not all databases in a Rails app are equal
- If a DB is externally managed, don’t try to migrate it
- Use dumps for read-only or shared databases
- CI setup should reflect real-world constraints, not assumptions
🚀 Final Thoughts
This issue wasn’t really about Rails—it was about understanding system boundaries.
Once I stopped treating both databases the same way and respected their roles:
- One writable
- One external and read-only
…the solution became straightforward.
If your CI is failing in a multi-database setup, don’t just look at Rails config.
👉 Look at who owns the database and how it’s supposed to be used.