Five existing migrations used op.alter_column / op.create_unique_constraint / op.drop_constraint / op.create_foreign_key directly on the users + quotes + quotes_daily tables. SQLite has no native support for those operations and requires Alembic's batch_alter_table copy-and-rename workaround. This wasn't noticed until now because the test suite uses Base.metadata.create_all to materialise schema, not the migration chain itself; and prod is MariaDB. But running `alembic upgrade head` against a fresh SQLite database (developer onboarding, CI smoke tests, the test container's own bootstrap) would fail at 0005. Fixes: - alembic/env.py: set render_as_batch=True when the dialect is SQLite. This auto-wraps any future autogenerated migration but doesn't retroactively rewrite existing op.* calls. - 0005 (widen quotes.symbol), 0013 (referrals), 0018 (polar webhook), 0019 (stripe), 0023 (users.lang index + qd_symbol widen) explicitly wrap their problematic ops in `with op.batch_alter_table(...) as bop`. Now `alembic upgrade head` + `alembic downgrade base` round-trip cleanly on a fresh SQLite database. MariaDB prod behaviour unchanged. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
"""polar webhook: User.polar_customer_id/subscription_id, polar_events table.
|
|
|
|
Revision ID: 0018
|
|
Revises: 0017
|
|
Create Date: 2026-05-26
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
|
|
revision: str = "0018"
|
|
down_revision: Union[str, None] = "0017"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
with op.batch_alter_table("users") as bop:
|
|
bop.add_column(sa.Column("polar_customer_id", sa.String(length=64), nullable=True))
|
|
bop.add_column(sa.Column("polar_subscription_id", sa.String(length=64), nullable=True))
|
|
bop.create_unique_constraint(
|
|
"uq_users_polar_customer", ["polar_customer_id"],
|
|
)
|
|
|
|
op.create_table(
|
|
"polar_events",
|
|
sa.Column("id", sa.BigInteger(), autoincrement=True, primary_key=True),
|
|
sa.Column("event_id", sa.String(length=128), nullable=False),
|
|
sa.Column("event_type", sa.String(length=64), nullable=False),
|
|
sa.Column("received_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column("processed_at", sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column("error", sa.Text(), nullable=True),
|
|
sa.Column("payload", sa.Text(), nullable=False),
|
|
sa.UniqueConstraint("event_id", name="uq_polar_events_event_id"),
|
|
)
|
|
op.create_index(
|
|
"ix_polar_events_type_received",
|
|
"polar_events",
|
|
["event_type", "received_at"],
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index("ix_polar_events_type_received", table_name="polar_events")
|
|
op.drop_table("polar_events")
|
|
with op.batch_alter_table("users") as bop:
|
|
bop.drop_constraint("uq_users_polar_customer", type_="unique")
|
|
bop.drop_column("polar_subscription_id")
|
|
op.drop_column("users", "polar_customer_id")
|