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 25ed8c744b - Show all commits

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

View file

@ -0,0 +1,139 @@
"""Recreate checks stored by an old PostgreSQL version.
PostgreSQL stores a check constraint as an expression tree and prints it
in the style of the running version. Before version 14, `EXTRACT(...)`
was stored as `date_part(...)` returning a `double precision`; newer
versions keep `EXTRACT(...)` returning a `numeric`. The rendering of the
`IN (...)` check in "couriers" changed as well.
The checks below were created in 2021 and still carry the old form.
They express the same rules as the ORM models, but a comparison with a
new database created from the ORM cannot tell. So, they are recreated.
"""
import os
from alembic import op
from velofy import configuration
revision = "09c70662dda4"
down_revision = "87412e235878"
branch_labels = None
depends_on = None
config = configuration.make_config("testing" if os.getenv("TESTING") else "production")
# (table, constraint, new expression, old expression)
CHECKS = (
(
"couriers",
"ck_couriers_on_available_vehicle_types",
"vehicle IN ('bicycle', 'motorcycle')",
(
"vehicle::text = ANY (ARRAY['bicycle'::character varying::text,"
" 'motorcycle'::character varying::text])"
),
),
(
"forecasts",
"ck_forecasts_on_start_at_allows_no_microseconds",
"(EXTRACT(microseconds FROM start_at)::integer % 1000000) = 0",
"(date_part('microseconds'::text, start_at)::integer % 1000000) = 0",
),
(
"forecasts",
"ck_forecasts_on_start_at_allows_no_seconds",
"EXTRACT(seconds FROM start_at) = 0::numeric",
"date_part('seconds'::text, start_at) = 0::double precision",
),
(
"forecasts",
"ck_forecasts_on_start_at_minutes_must_be_quarters_of_the_hour",
"(EXTRACT(minutes FROM start_at)::integer % 15) = 0",
"(date_part('minutes'::text, start_at)::integer % 15) = 0",
),
(
"forecasts",
"ck_forecasts_on_start_at_must_be_within_operating_hours",
(
"NOT (EXTRACT(hour FROM start_at) < 11::numeric"
" OR EXTRACT(hour FROM start_at) > 22::numeric)"
),
(
"NOT (date_part('hour'::text, start_at) < 11::double precision"
" OR date_part('hour'::text, start_at) > 22::double precision)"
),
),
(
"orders",
"ck_orders_on_ad_hoc_orders_within_business_hours",
(
"NOT (ad_hoc IS TRUE AND (EXTRACT(hour FROM placed_at) < 11::numeric"
" OR EXTRACT(hour FROM placed_at) > 22::numeric))"
),
"""
NOT (ad_hoc IS TRUE AND (
date_part('hour'::text, placed_at) < 11::double precision
OR date_part('hour'::text, placed_at) > 22::double precision
))
""",
),
(
"orders",
"ck_orders_on_scheduled_orders_not_within_30_minutes",
"NOT EXTRACT(epoch FROM scheduled_delivery_at - placed_at) < 1800::numeric",
(
"NOT date_part('epoch'::text, scheduled_delivery_at - placed_at)"
" < 1800::double precision"
),
),
(
"orders",
"ck_orders_on_scheduled_orders_within_business_hours",
"""
NOT (ad_hoc IS FALSE AND (
EXTRACT(hour FROM scheduled_delivery_at) <= 11::numeric
AND NOT (
EXTRACT(hour FROM scheduled_delivery_at) = 11::numeric
AND EXTRACT(minute FROM scheduled_delivery_at) = 45::numeric
)
OR EXTRACT(hour FROM scheduled_delivery_at) > 22::numeric
))
""",
"""
NOT (ad_hoc IS FALSE AND (
date_part('hour'::text, scheduled_delivery_at) <= 11::double precision
AND NOT (
date_part('hour'::text, scheduled_delivery_at) = 11::double precision
AND date_part('minute'::text, scheduled_delivery_at) = 45::double precision
)
OR date_part('hour'::text, scheduled_delivery_at) > 22::double precision
))
""",
),
)
def _recreate(table: str, name: str, sqltext: str) -> None:
"""Replace the check `name` on `table` with one for `sqltext`."""
op.execute(
f"ALTER TABLE {config.CLEAN_SCHEMA}.{table}"
f" DROP CONSTRAINT {name},"
f" ADD CONSTRAINT {name} CHECK ({sqltext});",
)
def upgrade():
"""Upgrade to the next revision."""
for table, name, new_sqltext, _old_sqltext in CHECKS:
_recreate(table, name, new_sqltext)
def downgrade():
"""Downgrade to the previous revision."""
for table, name, _new_sqltext, old_sqltext in CHECKS:
_recreate(table, name, old_sqltext)