kitchen/backend/migrations/add_sambapos_excluded_items.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

36 lines
1.1 KiB
Python

"""
Migration script to add SambaPOS excluded items column.
Run this script once after deploying the new code.
"""
import asyncio
import logging
from sqlalchemy import text
from database import engine
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def run_migration():
"""Add sambapos_excluded_items column to kitchen_settings table."""
logger.info("Running SambaPOS excluded items migration...")
# Add column for excluded items (TEXT to allow for many items with long names)
sql = "ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS sambapos_excluded_items TEXT"
try:
async with engine.begin() as conn:
await conn.execute(text(sql))
logger.info("Added column: sambapos_excluded_items")
except Exception as e:
if "already exists" in str(e).lower() or "duplicate column" in str(e).lower():
logger.info("Column already exists, skipping")
else:
logger.warning(f"Column migration warning: {e}")
logger.info("SambaPOS excluded items migration completed!")
if __name__ == "__main__":
asyncio.run(run_migration())