Platform v2 step 1.1 (APPS_PLATFORM D-11, §7, D-24). - core/timeutil.py: utcnow, venue_tz (venue.timezone, cached, invalidated on change - no restart), to_venue/venue_date/venue_strftime, parse_api_datetime (offset -> UTC; naive = venue-local; date = venue midnight), business_date_of /business_date_window (rollover hour, default 06:00), LocalClock + is_within, UTCDateTime column type (naive writes raise, reads are aware UTC), iso_z - every model column is UTCDateTime; Pydantic UTCDatetime emits "...Z" - business_days.business_date set at open; revision 0002 adds it (backfilled) and makes sync_events.created_at a UTCDateTime (AUTOINCREMENT restated) - tz.py deleted; system.timezone setting replaced by venue.timezone (validated) + business_day.rollover_hour; manager timezone picker writes venue.timezone - fixes: price-group schedules/time_range used the container's zone (happy hour bug); traffic report bucketed by UTC hour; reports' "date" meant the UTC calendar day, now the business date; reservations stored venue-local naive times (no-show check 2-3 h off) and listed by func.date; schedule week used UTC midnights; fiscal file names used container-local time - seed.py/demo_seed.py create store_manager with role permissions (regression from step 1.0: v1's startup ALTER list used to rename/grant role 'manager') - tzdata dependency; guard tests/guards/test_no_naive_time.py (AST scan); tests/test_timeutil.py covers the §7.3 DST cases and the API checks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
3.5 KiB
Python
78 lines
3.5 KiB
Python
"""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')
|