fix(db): 補齊 action_plans schema drift
All checks were successful
CD Pipeline / deploy (push) Successful in 5m10s
All checks were successful
CD Pipeline / deploy (push) Successful in 5m10s
This commit is contained in:
@@ -37,6 +37,18 @@ _metadata_initialized = False
|
||||
_POSTGRES_METADATA_LOCK_ID = 170017
|
||||
|
||||
|
||||
def _repair_postgres_schema_drift(conn):
|
||||
"""補齊 create_all 不會處理的既有 PostgreSQL schema drift。"""
|
||||
conn.execute(text("""
|
||||
ALTER TABLE IF EXISTS action_plans
|
||||
ADD COLUMN IF NOT EXISTS action_type VARCHAR(100),
|
||||
ADD COLUMN IF NOT EXISTS description TEXT,
|
||||
ADD COLUMN IF NOT EXISTS priority INTEGER DEFAULT 3,
|
||||
ADD COLUMN IF NOT EXISTS metadata_json TEXT
|
||||
"""))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_action_plans_type ON action_plans(action_type)"))
|
||||
|
||||
|
||||
def ensure_metadata_initialized(engine, use_postgres_lock=False):
|
||||
"""冪等初始化 SQLAlchemy metadata,避免一般流程重複碰 DDL。"""
|
||||
global _metadata_initialized
|
||||
@@ -52,6 +64,7 @@ def ensure_metadata_initialized(engine, use_postgres_lock=False):
|
||||
conn.execute(text("SELECT pg_advisory_lock(:lock_id)"), {"lock_id": _POSTGRES_METADATA_LOCK_ID})
|
||||
try:
|
||||
Base.metadata.create_all(conn)
|
||||
_repair_postgres_schema_drift(conn)
|
||||
finally:
|
||||
conn.execute(text("SELECT pg_advisory_unlock(:lock_id)"), {"lock_id": _POSTGRES_METADATA_LOCK_ID})
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user