Requires a fresh database!! #
The eight 0.1 migrations (001_initial … 008_user_token_version) are replaced by one revision, v0.2. Existing databases are stamped with revision ids that no longer exist, so alembic upgrade head fails on them, and the new schema also differs (constraint names, cascades, a unique constraint on access). Recreate the database, or export what you need first. Local Docker: make clean and make dev.
This is a one-off reset of the history; later releases add normal incremental migrations on top of v0.2.
Why #
The audit found:
- eight incremental migrations from early development, including constraints that SQLite can't run, so CI couldn't check migrations at all;
- models and migrations had drifted (server defaults, duplicated unique indexes, an index present in the DB but not the model);
- no constraint naming convention, so names were a mix of explicit, auto-generated and
op.f(); - naive datetimes patched with
.replace(tzinfo=UTC)at several call sites, and API timestamps with inconsistent offsets; accessallowed duplicate(user, page)rows (transfer code deduplicated by hand) and had no cascade rules.
What changed #
One migration, named by release
backend/alembic/versions/v0_2.py, revision idv0.2, creates the complete schema;downgradedrops the tables in dependency order. Generated from the models against an empty MariaDB, then cleaned up (plainsa.DateTime(), no app imports).- The dead
join_requeststable andemail_verification_tokens.join_request_idcolumn are simply absent (see the earlier removal commit ondev).
Models
db/base.py: deterministic naming convention for indexes, unique/check/foreign/primary keys.db/types.py:UTCDateTime, aTypeDecoratorthat stores naive UTC (MariaDB has no timezone-aware type) and always returns timezone-aware UTC values. All datetime columns use it. The manualtzinfopatches inroutes/auth.pyare removed.access: unique(user_id, page_id)(uq_access_user_page),ON DELETE CASCADEon both foreign keys, index onpage_id.access_keys.created_by:ON DELETE SET NULL.pagescheck constraint renamed to fit the convention (ck_pages_name_or_url).
CI / tooling
- The migration now runs on SQLite, so
backend.ymlgets a step (andmake migrations-checklocally) that runsalembic upgrade head,alembic check(fails if models and migration drift) andalembic downgrade base.
Behaviour changes to be aware of #
- API timestamps now always carry an offset (
…+00:00). Previously values read back from the database had none, and a browser parses an offset-less ISO string as local time, so displayed times could be shifted. - Inserting a second
accessrow for the same user and page now raises an integrity error instead of silently creating a duplicate. No current code path does this (the full suite passes). - Deleting a user or page now cascades to its
accessrows at the database level; the existing cleanup code still runs and is unaffected.
Testing #
ruff format --check,ruff check,basedpyright,pytest(new: UTC round-trip andaccessuniqueness tests) — clean.- On a scratch database in MariaDB (not the dev data):
alembic upgrade head→alembic check("No new upgrade operations detected") →downgrade base(leaves onlyalembic_version) →upgrade headagain. Same cycle on SQLite viamake migrations-check. - Ran the app against the scratch MariaDB: owner bootstrapped on the empty database, logged in, created a page, listed users and notifications (timestamps with
+00:00), generated access keys, and confirmed a duplicateaccessinsert is rejected withDuplicate entry … for key 'uq_access_user_page'. The scratch database was dropped afterwards. - Not run: the new CI step on Tangled; an upgrade of an existing 0.1 database (intentionally unsupported, see above).
Not in this PR #
- ORM
relationship()s (nothing uses them yet). - Roles and notification kinds as real enums in the database.
- Version bump:
v0.2is the migration id only;pyproject.toml/package.jsonare still0.1.0(make version minorwhen releasing).
Merge notes #
Independent of the other two branches. Merge this one last, because everyone with a local database has to recreate it.