更新
This commit is contained in:
@@ -33,6 +33,26 @@ def add_column_if_missing(conn, table: str, column: str, ddl_by_dialect: dict[st
|
||||
conn.execute(text(ddl))
|
||||
|
||||
|
||||
def add_index_if_missing(
|
||||
conn,
|
||||
table: str,
|
||||
index_name: str,
|
||||
columns: tuple[str, ...],
|
||||
) -> None:
|
||||
"""Create one portable index without relying on dialect-specific IF NOT EXISTS."""
|
||||
try:
|
||||
insp = inspect(conn)
|
||||
if not insp.has_table(table):
|
||||
return
|
||||
existing = {item.get("name") for item in insp.get_indexes(table)}
|
||||
except Exception:
|
||||
return
|
||||
if index_name in existing:
|
||||
return
|
||||
safe_columns = ", ".join(columns)
|
||||
conn.execute(text(f"CREATE INDEX {index_name} ON {table} ({safe_columns})"))
|
||||
|
||||
|
||||
def migrate_accounts_table(conn) -> None:
|
||||
add_column_if_missing(
|
||||
conn,
|
||||
@@ -124,6 +144,36 @@ def migrate_message_logs_table(conn) -> None:
|
||||
"sender_avatar",
|
||||
{"default": "ALTER TABLE message_logs ADD COLUMN sender_avatar TEXT"},
|
||||
)
|
||||
add_index_if_missing(
|
||||
conn,
|
||||
"message_logs",
|
||||
"ix_message_logs_account_created_at",
|
||||
("account_id", "created_at"),
|
||||
)
|
||||
add_index_if_missing(
|
||||
conn,
|
||||
"message_logs",
|
||||
"ix_message_logs_created_at",
|
||||
("created_at",),
|
||||
)
|
||||
add_index_if_missing(
|
||||
conn,
|
||||
"message_logs",
|
||||
"ix_message_logs_status_account_id",
|
||||
("status", "account_id"),
|
||||
)
|
||||
add_index_if_missing(
|
||||
conn,
|
||||
"received_message_logs",
|
||||
"ix_received_message_logs_account_created_at",
|
||||
("account_id", "created_at"),
|
||||
)
|
||||
add_index_if_missing(
|
||||
conn,
|
||||
"system_logs",
|
||||
"ix_system_logs_account_created_at",
|
||||
("account_id", "created_at"),
|
||||
)
|
||||
|
||||
|
||||
def migrate_rules_table(conn) -> None:
|
||||
|
||||
Reference in New Issue
Block a user