Store the directions of paths as JSON arrays
All checks were successful
CI / Checks (Python 3.13) (push) Successful in 2m46s

The data migration converts the 42 existing rows
from a JSON-encoded string into a JSON array.
This commit is contained in:
Alexander Hess 2026-12-02 10:29:35 +01:00
commit 664dccee02
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -0,0 +1,53 @@
"""Store the directions of paths as JSON arrays.
The "directions" column in "addresses_addresses" is of type `JSON`,
but `Path.sync_with_google_maps()` stored a JSON-encoded string in it.
So, the data was encoded twice: PostgreSQL saw one JSON string
instead of an array of latitude-longitude pairs.
The `WHERE` clauses make both directions safe to run more than once.
"""
import os
from alembic import op
from velofy import configuration
revision = "e5b4b207f490"
down_revision = "09c70662dda4"
branch_labels = None
depends_on = None
config = configuration.make_config("testing" if os.getenv("TESTING") else "production")
def upgrade():
"""Upgrade to the next revision."""
op.execute(
f"""
UPDATE
{config.CLEAN_SCHEMA}.addresses_addresses
SET
-- `#>> '{{}}'` extracts the text of a JSON string; `::json` parses it
directions = (directions #>> '{{}}')::json
WHERE
json_typeof(directions) = 'string';
""",
)
def downgrade():
"""Downgrade to the previous revision."""
op.execute(
f"""
UPDATE
{config.CLEAN_SCHEMA}.addresses_addresses
SET
directions = to_json(directions::text)
WHERE
json_typeof(directions) = 'array';
""",
)