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>
This commit is contained in:
jtricerolph 2026-07-12 19:53:26 +00:00
parent e7e1fda9f6
commit bc874ac9ff
25 changed files with 302 additions and 302 deletions

View file

@ -1,22 +1,22 @@
"""Add portions_needed_unit to recipe_sub_recipes table."""
import logging
from sqlalchemy import text
from database import engine
logger = logging.getLogger(__name__)
async def migrate():
async with engine.begin() as conn:
result = await conn.execute(text(
"SELECT column_name FROM information_schema.columns "
"WHERE table_name = 'recipe_sub_recipes' AND column_name = 'portions_needed_unit'"
))
if result.scalar_one_or_none():
logger.info("recipe_sub_recipes.portions_needed_unit already exists, skipping")
return
await conn.execute(text(
"ALTER TABLE recipe_sub_recipes ADD COLUMN portions_needed_unit VARCHAR(10) DEFAULT NULL"
))
logger.info("Added portions_needed_unit column to recipe_sub_recipes")
"""Add portions_needed_unit to recipe_sub_recipes table."""
import logging
from sqlalchemy import text
from database import engine
logger = logging.getLogger(__name__)
async def migrate():
async with engine.begin() as conn:
result = await conn.execute(text(
"SELECT column_name FROM information_schema.columns "
"WHERE table_name = 'recipe_sub_recipes' AND column_name = 'portions_needed_unit'"
))
if result.scalar_one_or_none():
logger.info("recipe_sub_recipes.portions_needed_unit already exists, skipping")
return
await conn.execute(text(
"ALTER TABLE recipe_sub_recipes ADD COLUMN IF NOT EXISTS portions_needed_unit VARCHAR(10) DEFAULT NULL"
))
logger.info("Added portions_needed_unit column to recipe_sub_recipes")