Skip to content

Only connect SecondaryRecord to secondary DB when MULTI_DB_TEST is set#318

Open
OdenTakashi wants to merge 1 commit intomainfrom
fix-secondary-record-adapter-not-specified
Open

Only connect SecondaryRecord to secondary DB when MULTI_DB_TEST is set#318
OdenTakashi wants to merge 1 commit intomainfrom
fix-secondary-record-adapter-not-specified

Conversation

@OdenTakashi
Copy link
Collaborator

Problem

database.yml defines the secondary database configuration only when MULTI_DB_TEST=true.
However, SecondaryRecord always declares:

connects_to database: { writing: :secondary }

As a result, running annotaterb models without MULTI_DB_TEST set raised the following error:

Unable to process app/models/secondary/test_default.rb: The secondary database is not
configured for the development environment.

Solution

Make SecondaryRecord connect to the secondary database only when MULTI_DB_TEST=true is set.

When MULTI_DB_TEST is not set, SecondaryRecord falls back to the primary connection. Since it is an abstract class and its child class Secondary::TestDefault does not have a corresponding table in the primary database, both are safely skipped by annotaterb.

This change only affects the test dummy app (spec/dummyapp/) and does not impact the gem’s runtime behavior.

## Problem
`database.yml` defines the `secondary` database configuration only when `MULTI_DB_TEST=true`.
However, `SecondaryRecord` always declares:

```rb
connects_to database: { writing: :secondary }
```

As a result, running `annotaterb models` without `MULTI_DB_TEST` set raised the following error:

```sh
Unable to process app/models/secondary/test_default.rb: The secondary database is not
configured for the development environment.
```

## Solution
Make `SecondaryRecord` connect to the `secondary` database only when `MULTI_DB_TEST=true` is set.

When `MULTI_DB_TEST` is not set, `SecondaryRecord` falls back to the primary connection. Since it is an abstract class and its child class `Secondary::TestDefault` does not have a corresponding table in the primary database, both are safely skipped by annotaterb.
@OdenTakashi OdenTakashi force-pushed the fix-secondary-record-adapter-not-specified branch from b41557b to b2b1458 Compare February 28, 2026 13:46
@drwl
Copy link
Owner

drwl commented Mar 5, 2026

This makes sense to me, but are there tests that test this change? I would think that this would break some tests -- but it could be we don't have them to test the secondary database.

@OdenTakashi
Copy link
Collaborator Author

Thank you for reviewing.

are there tests that test this change? I would think that this would break some tests

The existing multi-database tests all run with MULTI_DB_TEST=true, so they continue to pass as before. The single-database integration tests already cover the case without it.

it could be we don't have them to test the secondary database

We do have them in annotate_models_in_multi_db_spec.rb — they just always set MULTI_DB_TEST=true, so this change doesn't affect them.

Let me know if I'm misunderstanding anything!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants