fix(sql): backfill whatsapp chat_id in compat init
This commit is contained in:
@@ -12,7 +12,7 @@ from services.shared.sql_models import Base
|
||||
|
||||
|
||||
_SCHEMA_INIT_LOCK = threading.Lock()
|
||||
_SCHEMA_INITIALIZED = False
|
||||
_BASE_SCHEMA_INITIALIZED = False
|
||||
|
||||
|
||||
def _table_columns(inspector, table_name: str) -> set[str]:
|
||||
@@ -194,6 +194,7 @@ def _apply_runtime_schema_compatibility() -> None:
|
||||
|
||||
if "whatsapp_messages" in table_names:
|
||||
columns = _table_columns(inspector, "whatsapp_messages")
|
||||
_add_column_if_missing(conn, columns, "whatsapp_messages", "chat_id", "VARCHAR(128)")
|
||||
_add_column_if_missing(conn, columns, "whatsapp_messages", "thread_id", "VARCHAR(64)")
|
||||
_add_column_if_missing(conn, columns, "whatsapp_messages", "interaction_id", "VARCHAR(64)")
|
||||
_add_column_if_missing(conn, columns, "whatsapp_messages", "customer_id", "VARCHAR(64)")
|
||||
@@ -593,21 +594,16 @@ def _apply_runtime_schema_compatibility() -> None:
|
||||
|
||||
|
||||
def init_sql_schema() -> None:
|
||||
global _SCHEMA_INITIALIZED
|
||||
if _SCHEMA_INITIALIZED:
|
||||
return
|
||||
global _BASE_SCHEMA_INITIALIZED
|
||||
with _SCHEMA_INIT_LOCK:
|
||||
if _SCHEMA_INITIALIZED:
|
||||
return
|
||||
if schema_management_mode() == "migrations":
|
||||
validate_schema_migrations_applied()
|
||||
_SCHEMA_INITIALIZED = True
|
||||
return
|
||||
Base.metadata.create_all(bind=engine)
|
||||
elif not _BASE_SCHEMA_INITIALIZED:
|
||||
Base.metadata.create_all(bind=engine)
|
||||
_BASE_SCHEMA_INITIALIZED = True
|
||||
for attempt in range(5):
|
||||
try:
|
||||
_apply_runtime_schema_compatibility()
|
||||
_SCHEMA_INITIALIZED = True
|
||||
return
|
||||
except OperationalError as exc:
|
||||
if "database is locked" not in str(exc).lower() or attempt >= 4:
|
||||
|
||||
Reference in New Issue
Block a user