Ignore the foreign keys into the original data
All checks were successful
CI / Checks (Python 3.13) (push) Successful in 2m47s
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.
This commit is contained in:
parent
25ed8c744b
commit
248c1ae600
1 changed files with 30 additions and 3 deletions
|
|
@ -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.
|
`ORIGINAL_SCHEMA` and the `alembic_version` table are not compared.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
import sys
|
import sys
|
||||||
|
import typing as t
|
||||||
|
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemydiff import comparer
|
from sqlalchemydiff import comparer
|
||||||
|
|
@ -17,6 +20,28 @@ import velofy
|
||||||
from velofy import db
|
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:
|
def main() -> int:
|
||||||
"""Create the ORM objects in a temporary database and compare."""
|
"""Create the ORM objects in a temporary database and compare."""
|
||||||
try:
|
try:
|
||||||
|
|
@ -46,13 +71,15 @@ def main() -> int:
|
||||||
two_alias="live",
|
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")
|
print("Both databases consist of the exact same objects")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
print('`diff` from "orm" to "live":')
|
print('`diff` from "orm" to "live":')
|
||||||
print(result.errors)
|
print(json.dumps(errors, indent=2))
|
||||||
result.dump_errors(".cache/sqlalchemy_diff.json")
|
pathlib.Path(".cache/sqlalchemy_diff.json").write_text(json.dumps(errors, indent=2))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue