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:
parent
e7e1fda9f6
commit
bc874ac9ff
25 changed files with 302 additions and 302 deletions
|
|
@ -1,33 +1,33 @@
|
|||
"""Move yield_percent from ingredients to recipe_ingredients (per-use yield)."""
|
||||
import logging
|
||||
from sqlalchemy import text
|
||||
from database import engine
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def migrate():
|
||||
async with engine.begin() as conn:
|
||||
# Check if column already exists
|
||||
result = await conn.execute(text(
|
||||
"SELECT column_name FROM information_schema.columns "
|
||||
"WHERE table_name = 'recipe_ingredients' AND column_name = 'yield_percent'"
|
||||
))
|
||||
if result.scalar_one_or_none():
|
||||
logger.info("recipe_ingredients.yield_percent already exists, skipping")
|
||||
return
|
||||
|
||||
# Add yield_percent to recipe_ingredients with default 100
|
||||
await conn.execute(text(
|
||||
"ALTER TABLE recipe_ingredients ADD COLUMN yield_percent NUMERIC(5,2) DEFAULT 100.00 NOT NULL"
|
||||
))
|
||||
logger.info("Added recipe_ingredients.yield_percent column")
|
||||
|
||||
# Populate from existing ingredient yields
|
||||
await conn.execute(text(
|
||||
"UPDATE recipe_ingredients ri "
|
||||
"SET yield_percent = i.yield_percent "
|
||||
"FROM ingredients i "
|
||||
"WHERE ri.ingredient_id = i.id AND i.yield_percent != 100.00"
|
||||
))
|
||||
logger.info("Populated recipe_ingredients.yield_percent from existing ingredient values")
|
||||
"""Move yield_percent from ingredients to recipe_ingredients (per-use yield)."""
|
||||
import logging
|
||||
from sqlalchemy import text
|
||||
from database import engine
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def migrate():
|
||||
async with engine.begin() as conn:
|
||||
# Check if column already exists
|
||||
result = await conn.execute(text(
|
||||
"SELECT column_name FROM information_schema.columns "
|
||||
"WHERE table_name = 'recipe_ingredients' AND column_name = 'yield_percent'"
|
||||
))
|
||||
if result.scalar_one_or_none():
|
||||
logger.info("recipe_ingredients.yield_percent already exists, skipping")
|
||||
return
|
||||
|
||||
# Add yield_percent to recipe_ingredients with default 100
|
||||
await conn.execute(text(
|
||||
"ALTER TABLE recipe_ingredients ADD COLUMN IF NOT EXISTS yield_percent NUMERIC(5,2) DEFAULT 100.00 NOT NULL"
|
||||
))
|
||||
logger.info("Added recipe_ingredients.yield_percent column")
|
||||
|
||||
# Populate from existing ingredient yields
|
||||
await conn.execute(text(
|
||||
"UPDATE recipe_ingredients ri "
|
||||
"SET yield_percent = i.yield_percent "
|
||||
"FROM ingredients i "
|
||||
"WHERE ri.ingredient_id = i.id AND i.yield_percent != 100.00"
|
||||
))
|
||||
logger.info("Populated recipe_ingredients.yield_percent from existing ingredient values")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue