read.markets/alembic/versions/0005_widen_quote_symbol.py
Giorgio Gilestro 78ce8c8b0d alembic: make migration chain SQLite-compatible (fresh upgrade)
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>
2026-05-28 00:16:09 +02:00

41 lines
1.2 KiB
Python

"""widen quotes.symbol to 128 chars to fit Eurostat / ONS path identifiers
Revision ID: 0005
Revises: 0004
Create Date: 2026-05-15
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
revision: str = "0005"
down_revision: Union[str, None] = "0004"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# batch_alter_table wraps the ALTER in a copy-and-rename dance for
# SQLite (which doesn't support ALTER COLUMN TYPE) while remaining a
# plain ALTER on MariaDB / Postgres. Required for `alembic upgrade
# head` to work against a fresh SQLite database during local tooling
# or test bootstrap.
with op.batch_alter_table("quotes") as bop:
bop.alter_column(
"symbol",
existing_type=sa.String(64),
type_=sa.String(128),
existing_nullable=False,
)
def downgrade() -> None:
with op.batch_alter_table("quotes") as bop:
bop.alter_column(
"symbol",
existing_type=sa.String(128),
type_=sa.String(64),
existing_nullable=False,
)