0a6f753584
- 此前 MySQL 适配时模型编辑误删 description,导致仓储序列化 AttributeError - 0037_system_config_description:幂等补列(SQLite 已有则跳过)并回填存量说明 - 与并行的 0037_market 分叉建 merge 版本,统一迁移线(当前 head=02d10eccb1f9)
45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
"""补回 system_configs.description(此前模型编辑误删,导致 /admin/config 500)。
|
|
|
|
Revision ID: 0037_system_config_description
|
|
Revises: 0036_service_orders
|
|
Create Date: 2026-08-31
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision = "0037_system_config_description"
|
|
down_revision = "0036_service_orders"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
"""SQLite 已有该列 → 仅 MySQL/缺失时补;幂等(查列存在性)。"""
|
|
bind = op.get_bind()
|
|
insp = sa.inspect(bind)
|
|
cols = [c["name"] for c in insp.get_columns("system_configs")]
|
|
if "description" not in cols:
|
|
op.add_column("system_configs", sa.Column("description", sa.String(), nullable=True, server_default=""))
|
|
# 从 SQLite 备份源回填说明(如存在)
|
|
try:
|
|
import sqlite3
|
|
from pathlib import Path
|
|
db_path = Path("serverdata/data/app.db")
|
|
if db_path.exists():
|
|
sconn = sqlite3.connect(str(db_path))
|
|
rows = sconn.execute("SELECT key, description FROM system_configs").fetchall()
|
|
sconn.close()
|
|
from sqlalchemy import text
|
|
for k, d in rows:
|
|
if d:
|
|
bind.execute(text("UPDATE system_configs SET description=:d WHERE `key`=:k"),
|
|
{"d": d, "k": k})
|
|
except Exception: # noqa: BLE001 回填失败不阻塞
|
|
pass
|
|
|
|
|
|
def downgrade() -> None:
|
|
pass # 保留列(回滚会造成数据丢失)
|