A lightweight, self-hosted multi-tenant web publishing platform

feat(db): single v0.2 migration, UTC datetimes, naming convention and cascades #15

Merged pre1ude.dev
targeting dev from refactor/models-and-migrations
Merged opened by pre1ude.dev
targeting dev from refactor/models-and-migrations

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;
  • access allowed 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 id v0.2, creates the complete schema; downgrade drops the tables in dependency order. Generated from the models against an empty MariaDB, then cleaned up (plain sa.DateTime(), no app imports).
  • The dead join_requests table and email_verification_tokens.join_request_id column are simply absent (see the earlier removal commit on dev).

Models

  • db/base.py: deterministic naming convention for indexes, unique/check/foreign/primary keys.
  • db/types.py: UTCDateTime, a TypeDecorator that stores naive UTC (MariaDB has no timezone-aware type) and always returns timezone-aware UTC values. All datetime columns use it. The manual tzinfo patches in routes/auth.py are removed.
  • access: unique (user_id, page_id) (uq_access_user_page), ON DELETE CASCADE on both foreign keys, index on page_id.
  • access_keys.created_by: ON DELETE SET NULL.
  • pages check constraint renamed to fit the convention (ck_pages_name_or_url).

CI / tooling

  • The migration now runs on SQLite, so backend.yml gets a step (and make migrations-check locally) that runs alembic upgrade head, alembic check (fails if models and migration drift) and alembic 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 access row 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 access rows 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 and access uniqueness 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 only alembic_version) → upgrade head again. Same cycle on SQLite via make 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 duplicate access insert is rejected with Duplicate 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.2 is the migration id only; pyproject.toml/package.json are still 0.1.0 (make version minor when releasing).

Merge notes #

Independent of the other two branches. Merge this one last, because everyone with a local database has to recreate it.

Commits 1

feat(db): single v0.2 migration, UTC datetimes, naming convention, cascades
Sign up or Sign in to add to the discussion.