"""time model: business_days.business_date, sync_events.created_at as UTCDateTime (D-11, step 1.1) Revision ID: 0002_time_model Revises: 0001_v2_baseline Create Date: 2026-11-12 Every other datetime column moved from DateTime to UTCDateTime in the same step. That needs no DDL: UTCDateTime's storage type is the same DATETIME text; it only adds the "aware UTC in, aware UTC out" rules in Python. Hand-edited after autogenerate: - business_date is added nullable, backfilled from opened_at in the venue zone with the rollover hour (same rule as core.timeutil.business_date_of, re-implemented here so the revision doesn't depend on app code that may change), then made NOT NULL. - sync_events rows are deleted: they're a 24 h WebSocket replay buffer whose created_at was ISO text with "T" and an offset, which doesn't compare with the new storage text. sqlite_autoincrement keeps ids increasing, so client cursors stay valid. The batch rebuild must restate it (table_kwargs), or SQLite drops AUTOINCREMENT silently. """ from datetime import datetime, timedelta, timezone from zoneinfo import ZoneInfo from alembic import op import sqlalchemy as sa from core.timeutil import UTCDateTime revision = '0002_time_model' down_revision = '0001_v2_baseline' branch_labels = None depends_on = None def _setting(conn, key, default): row = conn.execute(sa.text("select value from pos_settings where key = :k"), {"k": key}).fetchone() return row[0] if row and row[0] else default def upgrade() -> None: conn = op.get_bind() with op.batch_alter_table('business_days', schema=None) as batch_op: batch_op.add_column(sa.Column('business_date', sa.Date(), nullable=True)) tz = ZoneInfo(_setting(conn, "venue.timezone", "Europe/Athens")) rollover = int(_setting(conn, "business_day.rollover_hour", "6")) for bid, opened_at in conn.execute(sa.text("select id, opened_at from business_days")).fetchall(): dt = datetime.fromisoformat(str(opened_at).replace("Z", "+00:00")) if dt.tzinfo is None: dt = dt.replace(tzinfo=timezone.utc) local = dt.astimezone(tz) bdate = local.date() - timedelta(days=1) if local.hour < rollover else local.date() conn.execute(sa.text("update business_days set business_date = :d where id = :i"), {"d": bdate.isoformat(), "i": bid}) with op.batch_alter_table('business_days', schema=None) as batch_op: batch_op.alter_column('business_date', existing_type=sa.Date(), nullable=False) batch_op.create_index(batch_op.f('ix_business_days_business_date'), ['business_date'], unique=False) conn.execute(sa.text("delete from sync_events")) # Batch mode rebuilds the table from reflection, which loses AUTOINCREMENT unless restated. with op.batch_alter_table('sync_events', schema=None, table_kwargs={"sqlite_autoincrement": True}) as batch_op: batch_op.alter_column('created_at', existing_type=sa.VARCHAR(), type_=UTCDateTime(), existing_nullable=False) def downgrade() -> None: with op.batch_alter_table('sync_events', schema=None, table_kwargs={"sqlite_autoincrement": True}) as batch_op: batch_op.alter_column('created_at', existing_type=UTCDateTime(), type_=sa.VARCHAR(), existing_nullable=False) with op.batch_alter_table('business_days', schema=None) as batch_op: batch_op.drop_index(batch_op.f('ix_business_days_business_date')) batch_op.drop_column('business_date')