diff --git a/scripts/verify_database_objects.py b/scripts/verify_database_objects.py index 9fb40c6..87b3a2d 100644 --- a/scripts/verify_database_objects.py +++ b/scripts/verify_database_objects.py @@ -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