Store Path._directions as a JSON array #18
3 changed files with 60 additions and 12 deletions
|
|
@ -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';
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
import itertools
|
import itertools
|
||||||
import json
|
|
||||||
import typing as t
|
import typing as t
|
||||||
|
|
||||||
import folium
|
import folium
|
||||||
|
|
@ -64,7 +63,7 @@ class Path(meta.Base):
|
||||||
nullable=True,
|
nullable=True,
|
||||||
)
|
)
|
||||||
# An array of latitude-longitude pairs approximating a courier's way.
|
# An array of latitude-longitude pairs approximating a courier's way.
|
||||||
_directions: orm.Mapped[str | None] = orm.mapped_column(
|
_directions: orm.Mapped[list[list[float]] | None] = orm.mapped_column(
|
||||||
"directions",
|
"directions",
|
||||||
psql.JSON,
|
psql.JSON,
|
||||||
nullable=True,
|
nullable=True,
|
||||||
|
|
@ -248,7 +247,7 @@ class Path(meta.Base):
|
||||||
steps.discard(self.first_address.location.lat_lng)
|
steps.discard(self.first_address.location.lat_lng)
|
||||||
steps.discard(self.second_address.location.lat_lng)
|
steps.discard(self.second_address.location.lat_lng)
|
||||||
|
|
||||||
self._directions = json.dumps(list(steps))
|
self._directions = [list(step) for step in steps]
|
||||||
|
|
||||||
db.session.add(self)
|
db.session.add(self)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
@ -271,7 +270,7 @@ class Path(meta.Base):
|
||||||
msg = "_directions is not set"
|
msg = "_directions is not set"
|
||||||
raise RuntimeError(msg)
|
raise RuntimeError(msg)
|
||||||
|
|
||||||
points = [utils.Location(*point) for point in json.loads(self._directions)]
|
points = [utils.Location(*point) for point in self._directions]
|
||||||
for point in points:
|
for point in points:
|
||||||
point.relate_to(self.first_address.city.southwest)
|
point.relate_to(self.first_address.city.southwest)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
"""Test the ORM's `Path` model."""
|
"""Test the ORM's `Path` model."""
|
||||||
|
|
||||||
import json
|
|
||||||
|
|
||||||
import googlemaps as gm
|
import googlemaps as gm
|
||||||
import pytest
|
import pytest
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
|
@ -28,12 +26,10 @@ def path(address, another_address, make_address):
|
||||||
|
|
||||||
# We put 5 latitude-longitude pairs as the "path" from
|
# We put 5 latitude-longitude pairs as the "path" from
|
||||||
# `.first_address` to `.second_address`.
|
# `.first_address` to `.second_address`.
|
||||||
directions = json.dumps(
|
directions = [
|
||||||
[
|
[float(add.latitude), float(add.longitude)]
|
||||||
(float(add.latitude), float(add.longitude))
|
for add in (make_address() for _ in range(5))
|
||||||
for add in (make_address() for _ in range(5))
|
]
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
return db.Path(
|
return db.Path(
|
||||||
first_address=address,
|
first_address=address,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue