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>
40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
"""
|
|
Migration: Add order_email and account_number columns to suppliers table.
|
|
Used for Purchase Order email sending and supplier identification.
|
|
"""
|
|
import asyncio
|
|
from sqlalchemy import text
|
|
from database import engine
|
|
|
|
|
|
async def migrate():
|
|
async with engine.begin() as conn:
|
|
# Add order_email column
|
|
try:
|
|
await conn.execute(text(
|
|
"ALTER TABLE suppliers ADD COLUMN IF NOT EXISTS order_email VARCHAR(255)"
|
|
))
|
|
print("+ Added order_email column to suppliers")
|
|
except Exception as e:
|
|
if "already exists" in str(e).lower() or "duplicate column" in str(e).lower():
|
|
print("- order_email column already exists, skipping")
|
|
else:
|
|
raise
|
|
|
|
# Add account_number column
|
|
try:
|
|
await conn.execute(text(
|
|
"ALTER TABLE suppliers ADD COLUMN IF NOT EXISTS account_number VARCHAR(100)"
|
|
))
|
|
print("+ Added account_number column to suppliers")
|
|
except Exception as e:
|
|
if "already exists" in str(e).lower() or "duplicate column" in str(e).lower():
|
|
print("- account_number column already exists, skipping")
|
|
else:
|
|
raise
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("Running migration: add_supplier_po_fields")
|
|
asyncio.run(migrate())
|
|
print("Migration complete!")
|