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
Showing only changes of commit 248c1ae600 - Show all commits

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.
Alexander Hess 2026-12-01 22:57:47 +01:00
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -7,8 +7,11 @@ with it as their `search_path`; so, the original data in the
`ORIGINAL_SCHEMA` and the `alembic_version` table are not compared.
"""
import json
import os
import pathlib
import sys
import typing as t
import sqlalchemy as sa
from sqlalchemydiff import comparer
@ -17,6 +20,28 @@ import velofy
from velofy import db
def _without_sanity_checks(errors: dict[str, t.Any]) -> dict[str, t.Any]:
"""Drop the foreign keys from the `CLEAN_SCHEMA` into the `ORIGINAL_SCHEMA`.
The migrations create them to ensure the cleaned data still refer to
the original data. The ORM models do not know the original tables.
"""
foreign_keys = {}
for table, entry in errors.get("foreign_keys", {}).items():
live_only = [
fk
for fk in entry.get("live_only", [])
if fk["referred_schema"] != velofy.config.ORIGINAL_SCHEMA
]
rest = {key: value for key, value in entry.items() if key != "live_only"}
if live_only:
rest["live_only"] = live_only
if rest:
foreign_keys[table] = rest
return {**errors, "foreign_keys": foreign_keys}
def main() -> int:
"""Create the ORM objects in a temporary database and compare."""
try:
@ -46,13 +71,15 @@ def main() -> int:
two_alias="live",
)
if result.is_match:
errors = _without_sanity_checks(result.errors)
if not any(errors.values()):
print("Both databases consist of the exact same objects")
return 0
print('`diff` from "orm" to "live":')
print(result.errors)
result.dump_errors(".cache/sqlalchemy_diff.json")
print(json.dumps(errors, indent=2))
pathlib.Path(".cache/sqlalchemy_diff.json").write_text(json.dumps(errors, indent=2))
return 1