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

@ -26,7 +26,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE invoices ADD COLUMN notes TEXT"
"ALTER TABLE invoices ADD COLUMN IF NOT EXISTS notes TEXT"
))
logger.info("Added notes column to invoices")
except Exception as e:
@ -39,7 +39,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE invoices ADD COLUMN dext_sent_at TIMESTAMP"
"ALTER TABLE invoices ADD COLUMN IF NOT EXISTS dext_sent_at TIMESTAMP"
))
logger.info("Added dext_sent_at column to invoices")
except Exception as e:
@ -52,7 +52,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE invoices ADD COLUMN dext_sent_by_user_id INTEGER REFERENCES users(id)"
"ALTER TABLE invoices ADD COLUMN IF NOT EXISTS dext_sent_by_user_id INTEGER REFERENCES users(id)"
))
logger.info("Added dext_sent_by_user_id column to invoices")
except Exception as e:
@ -67,7 +67,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN smtp_host VARCHAR(255)"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS smtp_host VARCHAR(255)"
))
logger.info("Added smtp_host column to kitchen_settings")
except Exception as e:
@ -80,7 +80,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN smtp_port INTEGER DEFAULT 587"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS smtp_port INTEGER DEFAULT 587"
))
logger.info("Added smtp_port column to kitchen_settings")
except Exception as e:
@ -93,7 +93,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN smtp_username VARCHAR(255)"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS smtp_username VARCHAR(255)"
))
logger.info("Added smtp_username column to kitchen_settings")
except Exception as e:
@ -106,7 +106,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN smtp_password VARCHAR(500)"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS smtp_password VARCHAR(500)"
))
logger.info("Added smtp_password column to kitchen_settings")
except Exception as e:
@ -119,7 +119,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN smtp_use_tls BOOLEAN DEFAULT TRUE"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS smtp_use_tls BOOLEAN DEFAULT TRUE"
))
logger.info("Added smtp_use_tls column to kitchen_settings")
except Exception as e:
@ -132,7 +132,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN smtp_from_email VARCHAR(255)"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS smtp_from_email VARCHAR(255)"
))
logger.info("Added smtp_from_email column to kitchen_settings")
except Exception as e:
@ -145,7 +145,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN smtp_from_name VARCHAR(255) DEFAULT 'Kitchen Invoice System'"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS smtp_from_name VARCHAR(255) DEFAULT 'Kitchen Invoice System'"
))
logger.info("Added smtp_from_name column to kitchen_settings")
except Exception as e:
@ -160,7 +160,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN dext_email VARCHAR(255)"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS dext_email VARCHAR(255)"
))
logger.info("Added dext_email column to kitchen_settings")
except Exception as e:
@ -173,7 +173,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN dext_include_notes BOOLEAN DEFAULT TRUE"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS dext_include_notes BOOLEAN DEFAULT TRUE"
))
logger.info("Added dext_include_notes column to kitchen_settings")
except Exception as e:
@ -186,7 +186,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN dext_include_non_stock BOOLEAN DEFAULT TRUE"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS dext_include_non_stock BOOLEAN DEFAULT TRUE"
))
logger.info("Added dext_include_non_stock column to kitchen_settings")
except Exception as e:
@ -199,7 +199,7 @@ async def run_migration():
try:
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE kitchen_settings ADD COLUMN dext_auto_send_enabled BOOLEAN DEFAULT FALSE"
"ALTER TABLE kitchen_settings ADD COLUMN IF NOT EXISTS dext_auto_send_enabled BOOLEAN DEFAULT FALSE"
))
logger.info("Added dext_auto_send_enabled column to kitchen_settings")
except Exception as e: