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

2026-12-01

Ignore the foreign keys into the original data
All checks were successful
CI / Checks (Python 3.13) (push) Successful in 2m47s
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.
2026-12-01 22:58:01 +01:00
Recreate checks stored by an old PostgreSQL version
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.
2026-12-01 22:56:12 +01:00
Rename constraints to follow the naming convention
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.
2026-12-01 22:49:36 +01:00
Avoid lazy loads in .__repr__()
`ReplaySimulation`, `ReplayedOrder`, and `Forecast` accessed
relationships in their `.__repr__()`, which emits queries
or fails for detached instances.

They now use the foreign key columns.
2026-12-01 22:34:38 +01:00
Cache City.map with functools.cached_property
This replaces the manual caching in the uninitialized
`._map` attribute with a `hasattr()` check.

`.clear_map()` now only discards the cached map.
2026-12-01 22:27:28 +01:00
Make grids.side_length not nullable
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.
2026-12-01 22:20:06 +01:00
Simplify the docstrings of the migrations
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.
2026-12-01 22:20:06 +01:00
Test grids with the same side length in different cities
Regression test for the removed `unique=True` on `Grid.side_length`.
2026-12-01 22:20:06 +01:00
Allow the same Grid.side_length across cities
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.
2026-12-01 21:58:29 +01:00
Add nox session "verify-db-objects"
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`
2026-12-01 21:53:31 +01:00