fix: a rollback never stops the database from starting; schema changes are migration files

schema.sql dropped and re-added three CHECK constraints on every start, so
an older image narrowed the lists and failed on rows written by a newer one
(an Expedição user, for example). They are now created only when missing,
and app/migrate.py applies timestamped files from app/migrations/ once each,
recording them in dtf_local.migrations; an older image skips names it does
not know. The first migration sets today's full lists. A CI step rehearses
the rollback on the real database inside a rolled-back transaction.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-09 16:11:30 -03:00
parent 63af984e83
commit 6b7ef1c4c8
11 changed files with 278 additions and 12 deletions

View File

@@ -238,6 +238,24 @@ storage permits; otherwise reselect them. Saved quote IDs also survive reloads.
Clearing cookies loses a guest session, while registered customers can sign in
again. The operator can still inspect order records.
## Schema changes
`app/schema.sql` is the frozen baseline: every start runs it, and nothing new
is added to it. A schema change is a new file in `app/migrations/`, named with
a timestamp and a short slug, `YYYYMMDDHHMM_description.sql` (for example
`202611091607_checks-current-lists.sql`). `app/migrate.py` runs every file not
yet recorded in `dtf_local.migrations`, in name order, each recorded in the
same transaction and under an advisory lock; `app/bootstrap.py` and
`app/core/db.py` call it right after the schema and before the grants, so a
table a migration creates is granted to the runtime role. Never edit an applied
migration: add a new one instead. Any file in that folder whose name does not
match `^\d{12}_[a-z0-9-]+\.sql$` stops the start, so a typo is never skipped.
Widening a CHECK constraint is a DROP + ADD in a migration, with the full list;
`app/schema.sql` only creates the three initial ones when missing. A rollback
to an image at or after this change is safe: an older image ignores migration
names it does not know.
## Print files
Every paid order queues one print-file job per item. The worker renders a PDF

View File

@@ -173,7 +173,10 @@ affect stored data or retention.
In the Portainer stack variables, change `IMAGE_TAG` from `latest` to the full
SHA of the last known-good Gitea commit and redeploy. This rolls back Site, API,
Kanban, and worker code; it does not undo database migrations or restore data.
After recovery and a corrected release, return `IMAGE_TAG` to `latest`.
Rolling back to an image from before the timestamped migrations can still fail
if the data uses newer values; images from that change on ignore migration
names they do not know, so a rollback is safe. After recovery and a corrected
release, return `IMAGE_TAG` to `latest`.
## 6. Before opening to customers