Review the ORM models and align the live database with them #16

Merged
alexander merged 10 commits from orm-review into develop 2026-12-01 23:06:04 +01:00
Owner
  • Add the nox session "verify-db-objects" that compares a live database with the ORM models
  • Allow the same Grid.side_length in different cities
  • Make grids.side_length not nullable
  • Simplify the docstrings of the migrations and the template
  • Cache City.map with functools.cached_property
  • Avoid lazy loads in .__repr__()
  • Rename constraints to follow the naming convention
  • Recreate checks stored by an old PostgreSQL version

After this branch, uv run nox -s verify-db-objects reports no differences for the live database.

- Add the `nox` session "verify-db-objects" that compares a live database with the ORM models - Allow the same `Grid.side_length` in different cities - Make `grids.side_length` not nullable - Simplify the docstrings of the migrations and the template - Cache `City.map` with `functools.cached_property` - Avoid lazy loads in `.__repr__()` - Rename constraints to follow the naming convention - Recreate checks stored by an old PostgreSQL version After this branch, `uv run nox -s verify-db-objects` reports no differences for the live database.
The new "verify-db-objects" session creates the ORM objects
with `.create_all()` in a temporary database and compares
them with the `CLEAN_SCHEMA` of a "live" database using `sqlalchemy-diff`
The `unique=True` on `Grid.side_length`
made the side length unique across all cities.

The intended rule is the `UniqueConstraint("city_id", "side_length")`,
which the migrations already implement; so, no migration is needed.
Regression test for the removed `unique=True` on `Grid.side_length`.
The "Revision" and "Revises" lines and the revision hashes in `upgrade()`
and `downgrade()` repeated the `revision` and `down_revision` variables;
the creation date is in the file name.
The ORM always defined `Grid.side_length` with `nullable=False`,
but the migration that created the "grids" table did not; the
live database contains no `NULL`s.
This replaces the manual caching in the uninitialized
`._map` attribute with a `hasattr()` check.

`.clear_map()` now only discards the cached map.
`ReplaySimulation`, `ReplayedOrder`, and `Forecast` accessed
relationships in their `.__repr__()`, which emits queries
or fails for detached instances.

They now use the foreign key columns.
Earlier migrations named some constraints without `op.f()`,
a column rename did not rename the unique constraint containing the column,
and the "ordered_timestamps_N" checks in "orders" were shifted by one
compared to the ORM.

See the docstring of the migration for details.
Before version 14, PostgreSQL stored `EXTRACT(...)` as `date_part(...)`,
and the rendering of `IN (...)` changed as well.

The checks created in 2021 express the same rules as the ORM models
but render differently. See the docstring of the migration for details.
Ignore the foreign keys into the original data
All checks were successful
CI / Checks (Python 3.13) (push) Successful in 2m47s
248c1ae600
The migrations create foreign keys from the `CLEAN_SCHEMA`
into the `ORIGINAL_SCHEMA` to ensure the cleaned data still
refer to the original data.

The ORM models do not know the original tables.
Also, print the `diff` as indented JSON.
alexander merged commit 455af6dfdb into develop 2026-12-01 23:06:04 +01:00
alexander deleted branch orm-review 2026-12-01 23:06:04 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
alexander/velofy!16
No description provided.