Skip to content

Migration 0.36x

Use when upgrading from a version older than these releases: 0.366.0 and 0.361.0.

Part of the Migration guide.

0.366.0 — RoutingBackend requires route_through and matrix

The RoutingBackend protocol gained two methods: route_through (a route with stops, #785) and matrix (which the live-location ETA uses, #786). OSRMBackend and FakeRoutingBackend already have all three. A backend of your own that only implemented route no longer satisfies the protocol: mypy refuses to pass it where RoutingBackend is asked for, and, since the protocol is @runtime_checkable, isinstance(backend, RoutingBackend) now answers False.

Both methods can be written on top of the route you already have:

import asyncio
from collections.abc import Sequence

from tempest_fastapi_sdk.geo import (
    Coordinate,
    DistanceMatrix,
    RouteThroughResult,
    RoutingBackend,
    TravelEstimate,
    TravelModeEnum,
    estimate_travel,
)


class MyRoutingBackend:
    """A backend written before 0.366.0, now with the two new methods."""

    async def route(
        self,
        origin: Coordinate,
        destination: Coordinate,
        *,
        mode: TravelModeEnum = TravelModeEnum.CAR,
        with_geometry: bool = False,
    ) -> TravelEstimate:
        """The method the backend always had."""
        return estimate_travel(origin, destination, mode)

    async def route_through(
        self,
        points: Sequence[Coordinate],
        *,
        mode: TravelModeEnum = TravelModeEnum.CAR,
        with_geometry: bool = False,
    ) -> RouteThroughResult:
        """One leg per consecutive pair, through the existing route."""
        legs = [
            await self.route(origin, target, mode=mode)
            for origin, target in zip(points, points[1:], strict=False)
        ]
        return RouteThroughResult(
            mode=mode,
            distance_km=sum(leg.distance_km for leg in legs),
            duration_minutes=sum(leg.duration_minutes for leg in legs),
            source=legs[0].source,
            geometry=list(points) if with_geometry else [],
            legs=legs,
        )

    async def matrix(
        self,
        sources: list[Coordinate],
        destinations: list[Coordinate] | None = None,
        *,
        mode: TravelModeEnum = TravelModeEnum.CAR,
    ) -> DistanceMatrix:
        """Every (source, destination) pair, through the existing route."""
        targets = destinations if destinations is not None else sources
        rows = [[await self.route(a, b, mode=mode) for b in targets] for a in sources]
        return DistanceMatrix(
            sources=sources,
            destinations=targets,
            distances_km=[[cell.distance_km for cell in row] for row in rows],
            durations_minutes=[[cell.duration_minutes for cell in row] for row in rows],
            mode=mode,
            source_label=rows[0][0].source,
        )


async def main() -> None:
    """Check the backend against the protocol and route through a stop."""
    backend = MyRoutingBackend()
    a = Coordinate(latitude=-23.5505, longitude=-46.6333)
    b = Coordinate(latitude=-23.5629, longitude=-46.6544)
    c = Coordinate(latitude=-23.5874, longitude=-46.6576)
    result = await backend.route_through([a, b, c])
    print(isinstance(backend, RoutingBackend), len(result.legs), result.source)


asyncio.run(main())

Measured output: True 2 heuristic — the backend passes isinstance (and mypy) again, and the route through one stop has two legs.

0.361.0 — manual payout gains four columns

BaseManualPayoutModel (and make_manual_payout_model) gained four nullable columns, which ManualPayoutService uses to record the payment and the refusal. A table created on 0.360.0 lacks them, and any select of the new model fails — on SQLite with OperationalError: no such column: manual_payouts.end_to_end_id.

Column Type What for
end_to_end_id String(64), nullable Pix end-to-end id, written by mark_paid(..., end_to_end_id=...)
rejection_reason String(512), nullable why it was refused
rejected_at UtcDateTime, nullable when it was refused
rejected_by String(128), nullable who refused it

The REJECTED status ("rejected") fits the existing String(16), so it needs no migration.

Generate the revision with alembic revision --autogenerate or write it by hand:

import sqlalchemy as sa
from alembic import op

from tempest_fastapi_sdk.db import UtcDateTime


def upgrade() -> None:
    """Add the 0.361.0 manual payout columns."""
    op.add_column("manual_payouts", sa.Column("end_to_end_id", sa.String(64), nullable=True))
    op.add_column("manual_payouts", sa.Column("rejection_reason", sa.String(512), nullable=True))
    op.add_column("manual_payouts", sa.Column("rejected_at", UtcDateTime(), nullable=True))
    op.add_column("manual_payouts", sa.Column("rejected_by", sa.String(128), nullable=True))


def downgrade() -> None:
    """Drop the 0.361.0 manual payout columns."""
    for column in ("rejected_by", "rejected_at", "rejection_reason", "end_to_end_id"):
        op.drop_column("manual_payouts", column)

Replace "manual_payouts" if you passed another tablename= to make_manual_payout_model.