kitchen/backend/migrations/add_kitchen_details.py
jtricerolph bc874ac9ff Fix migration transaction poisoning: ADD COLUMN IF NOT EXISTS
Migrations used try/except around ADD COLUMN inside a single engine.begin()
block. When a 'column already exists' error was caught, asyncpg left the
transaction in aborted state, causing all subsequent DDL in the block to fail
with InFailedSQLTransactionError. Replace with IF NOT EXISTS to prevent the
error entirely.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-12 19:53:26 +00:00

39 lines
1.3 KiB
Python

"""
Migration: Add kitchen detail columns to kitchen_settings table.
Used for Purchase Order letterhead (preview and email).
"""
import asyncio
from sqlalchemy import text
from database import engine
COLUMNS = [
("kitchen_display_name", "VARCHAR(255)"),
("kitchen_address_line1", "VARCHAR(255)"),
("kitchen_address_line2", "VARCHAR(255)"),
("kitchen_city", "VARCHAR(100)"),
("kitchen_postcode", "VARCHAR(20)"),
("kitchen_phone", "VARCHAR(50)"),
("kitchen_email", "VARCHAR(255)"),
]
async def migrate():
async with engine.begin() as conn:
for col_name, col_type in COLUMNS:
try:
await conn.execute(text(
f"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS {col_name} {col_type}"
))
print(f"+ Added {col_name} column to kitchen_settings")
except Exception as e:
if "already exists" in str(e).lower() or "duplicate column" in str(e).lower():
print(f"- {col_name} column already exists, skipping")
else:
raise
if __name__ == "__main__":
print("Running migration: add_kitchen_details")
asyncio.run(migrate())
print("Migration complete!")