104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
"""rename glm provider code to dashscope
|
|
|
|
Revision ID: 20260429_rename_glm_provider_to_dashscope
|
|
Revises: 20260429_add_ai_call_trace_fields
|
|
Create Date: 2026-04-29
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
try:
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
|
except Exception: # pragma: no cover
|
|
JSONB = sa.JSON
|
|
|
|
|
|
revision = "20260429_rename_glm_provider_to_dashscope"
|
|
down_revision = "20260429_add_ai_call_trace_fields"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _json_type(bind):
|
|
return JSONB() if bind.dialect.name == "postgresql" else sa.JSON()
|
|
|
|
|
|
def _table_exists(bind, table_name: str) -> bool:
|
|
return sa.inspect(bind).has_table(table_name)
|
|
|
|
|
|
def _column_exists(bind, table_name: str, column_name: str) -> bool:
|
|
inspector = sa.inspect(bind)
|
|
if not inspector.has_table(table_name):
|
|
return False
|
|
return any(str(item.get("name") or "") == column_name for item in inspector.get_columns(table_name))
|
|
|
|
|
|
def _load_json(value: Any) -> Any:
|
|
if isinstance(value, str):
|
|
try:
|
|
return json.loads(value)
|
|
except Exception:
|
|
return value
|
|
return value
|
|
|
|
|
|
def _replace_provider_code(value: Any) -> tuple[Any, bool]:
|
|
changed = False
|
|
if isinstance(value, dict):
|
|
result: dict[Any, Any] = {}
|
|
for raw_key, raw_value in value.items():
|
|
key = "dashscope" if raw_key == "glm" else raw_key
|
|
next_value, child_changed = _replace_provider_code(raw_value)
|
|
result[key] = next_value
|
|
changed = changed or child_changed or key != raw_key
|
|
return result, changed
|
|
if isinstance(value, list):
|
|
result = []
|
|
for item in value:
|
|
next_value, child_changed = _replace_provider_code(item)
|
|
result.append(next_value)
|
|
changed = changed or child_changed
|
|
return result, changed
|
|
if value == "glm":
|
|
return "dashscope", True
|
|
return value, False
|
|
|
|
|
|
def _migrate_json_table(bind, table_name: str, column_name: str) -> None:
|
|
if not _table_exists(bind, table_name) or not _column_exists(bind, table_name, column_name):
|
|
return
|
|
table = sa.table(
|
|
table_name,
|
|
sa.column("id", sa.Integer()),
|
|
sa.column(column_name, _json_type(bind)),
|
|
)
|
|
rows = bind.execute(sa.select(table.c.id, table.c[column_name])).all()
|
|
for row_id, raw_value in rows:
|
|
payload = _load_json(raw_value)
|
|
next_payload, changed = _replace_provider_code(payload)
|
|
if changed:
|
|
bind.execute(
|
|
table.update().where(table.c.id == row_id).values({column_name: next_payload})
|
|
)
|
|
|
|
|
|
def upgrade() -> None:
|
|
bind = op.get_bind()
|
|
_migrate_json_table(bind, "integrations", "config")
|
|
_migrate_json_table(bind, "ai_call_logs", "routing_meta")
|
|
|
|
if _table_exists(bind, "ai_call_logs") and _column_exists(bind, "ai_call_logs", "provider"):
|
|
op.execute("UPDATE ai_call_logs SET provider = 'dashscope' WHERE provider = 'glm'")
|
|
|
|
|
|
def downgrade() -> None:
|
|
# Intentionally irreversible: glm was a misleading historical provider code.
|
|
pass
|