Keep one classification migration, and make it safe to run again
What this is
Two migrations define the same class. db/migrate/20260909000000_add_classification_to_securities.rb is on main; the same schema went upstream as we-promise/sure#3608 (mirrored here as #170), which timestamped it 20260916120000. Both declare class AddClassificationToSecurities, so the moment #3608 merges and this fork next syncs, Rails refuses db:migrate with ActiveRecord::DuplicateMigrationNameError — the same collision the A1 sync already hit once with add_variable_rate_tracking_to_loans.
This keeps upstream's file and drops the fork's, so there is one copy and it is the one upstream will carry.
Why upstream's is the one to keep
fork 20260909000000 | upstream 20260916120000 | |
|---|---|---|
| Migration version | [7.2] | [8.1] — what #175's rule asks for |
| Comment | cites loans.day_count_convention | no fork-only reference |
| Rationale for repeating the constants | absent | present |
Column-for-column and constraint-for-constraint the two produce the same schema: seven columns, three check constraints over the same value lists.
The part that is not just a file swap
Adopting it as written would break every database that has already run the fork's copy — including this fork's own.
The constraint loop leans on if_not_exists: true. That is not sufficient. It resolves through CheckConstraintDefinition#defined_for? (activerecord-8.1.3.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:197):
self.name == name.to_s &&
(validate.nil? || validate == self.options.fetch(:validate, validate)) &&
The call passes validate: false, so the guard only matches a constraint that is also NOT VALID. Against one that is present and validated it reports "absent", the ADD CONSTRAINT is issued a second time, and PostgreSQL raises PG::DuplicateObject.
Reproduced on a database in exactly that state — classification columns and validated constraints present, 20260916120000 pending:
PG::DuplicateObject: ERROR: constraint "chk_securities_asset_class" for relation "securities" already exists
The migration aborts and the schema_migrations row is never written, so it fails the same way on every subsequent run.
The loop now skips on the name alone. That still covers the state disable_ddl_transaction! actually creates — killed after the ADD but before the VALIDATE — which the original did handle, so nothing is traded away.
Evidence
test/migrations/add_classification_to_securities_migration_test.rb, following the shape of the two tests already in test/migrations/:
- re-running over validated constraints does not raise — the state every existing installation is in.
- re-running over an unvalidated constraint validates it rather than duplicating it — the other side of the boundary, asserting one constraint by that name and that it ends up validated.
Against the migration as upstream wrote it, both error with PG::DuplicateObject. With the guard, both pass (2 runs, 7 assertions).
Also run:
test/models/security_test.rb— 29 runs, 91 assertions, 0 failures. Includes the drift test that reads the rendered constraint back out of the catalog and compares it toSecurity::ASSET_CLASSESand friends, so the swap provably did not change the taxonomy.- Fresh install:
db:drop db:create db:migrate→ exit 0, 0 pending migrations,20260916120000recorded. - Already-migrated database:
db:migratere-run → exit 0. - Rubocop clean on both touched Ruby files.
- Full suite at this head: 10,007 runs, 42,478 assertions, 0 failures, 0 errors, 33 skips.
On db/schema.rb
The file is no longer in this PR. At 5b7f00b9 I restored it to be byte-identical to main — git diff origin/main -- db/schema.rb is empty both staged and unstaged — and the PR now touches four files, none of them the schema.
It does not need to change. main's schema version is already 2026_09_20_090000, later than either migration's timestamp, so renaming one moves nothing the dump records; and the two migrations render the same table definitions, so there is nothing else for it to reflect. The two corrections below are why this section exists at all, and they are kept as the record.
Correction 1 — an earlier head shipped churn this section denied. Head c14c3144e carried ~30 extra constraint rewrites while this text claimed it did not. I cleaned the file, verified the diff, and then ran db:drop db:create db:migrate to check the fresh-install path. dump_schema_after_migration is true in development, so that run silently re-dumped the file, and git add -A swept it in. Fixed; the diff is now re-verified after the last command that can touch it.
Correction 2 — my explanation for that churn was wrong. I said this machine's PostgreSQL rendered pg_get_constraintdef differently from whatever produced the committed file, so the committed file was stale and any legitimate db:schema:dump would flip those lines. That is not what is happening, and I have since tested it directly:
| created from | renders back as |
|---|---|
v IN ('x','y') — what a migration writes | ARRAY['x'::character varying, ...]::text[] |
that text fed back in — what schema:load does | ARRAY[('x'::character varying)::text, ...] |
| the second form fed back in | unchanged |
PostgreSQL re-renders a CHECK from its parse tree, and the two inputs do not round-trip to the same text. So the difference tracks how the database was built, not which PostgreSQL built it — same server, both forms, depending on input.
That inverts the conclusion: the committed form is the canonical one, because it is what running the migrations produces. The rewritten form only appears after a load-then-dump round trip, which is exactly what my verification procedure did. There is no stale schema and no refresh to schedule. Apologies for sending you after a non-existent problem.
What this leaves open
The byte-identity gap is closed, and it reopened once. An earlier revision of this section said this file and the copy on up/securities-classification were not byte-identical until we-promise/sure#3608 took the fix. It took it — and then this branch broke identity again by strengthening the migration test here and not there, which the gate caught.
Rather than narrow the claim to cover it, the stronger assertions were carried to up/securities-classification (and so to we-promise/sure#3608, which shares the branch). That copy is the one that ships upstream, so it is the one that most needs an assertion saying WHICH constraint an interrupted re-run left behind; scoping this sentence to the migration file instead would have left upstream with the weaker test deliberately.
Verified by object hash at both heads rather than by diff of a working tree: db/migrate/20260916120000_add_classification_to_securities.rb is b0139536 and test/migrations/add_classification_to_securities_migration_test.rb is 95cd346d on both branches. The single-copy rule is satisfied.
What remains is not a divergence but a sequencing gate: #3608 has to merge carrying the guard, which it does at its current head. If it merged without it, the next sync would reintroduce a migration that crashes db:migrate on every existing fork database — the precise failure this PR exists to prevent, arriving by the back door.
This does not include the ai → inferred rename of CLASSIFICATION_SOURCES. That was briefly pushed to #3608 and then withdrawn: it belongs to the slice that actually writes the column (#122 slice 3.6), not to a schema-only change where nothing reads or writes it.
One thing a self-hoster will notice. Every existing installation keeps an orphaned 20260909000000 row in schema_migrations, because this PR deletes the file but nothing removes the row. db:migrate:status then lists it as up 20260909000000 ********** NO FILE ********** beside up 20260916120000. It is harmless — Rails ignores a recorded version with no file, and a schema dump takes the higher one — but it is visible, and worth knowing before someone reports it as a fault.
Refs #170.
Summary by CodeRabbit
- Bug Fixes
- Improved database handling for security classification data, including safer schema updates and better resilience if a migration is interrupted or re-run.
- Tightened validation coverage so classification values stay consistent across the application and database.
- Tests
- Added migration coverage for re-run and partially applied schema scenarios.
- Expanded model/database comparison checks for classification values.