69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
"""迁移 029:扩容 PPT 模板资产标识并增加版本指纹。"""
|
||
import logging
|
||
import os
|
||
|
||
from sqlalchemy import inspect, text
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
def migrate():
|
||
from insurance.db.compat import db
|
||
|
||
columns = {
|
||
item["name"]: item
|
||
for item in inspect(db.engine).get_columns("insurance_ppt_templates")
|
||
}
|
||
dialect = db.engine.dialect.name
|
||
|
||
if dialect == "postgresql":
|
||
db.session.execute(text(
|
||
"ALTER TABLE insurance_ppt_templates "
|
||
"ALTER COLUMN source_template_asset_id TYPE VARCHAR(255)"
|
||
))
|
||
elif dialect in {"mysql", "mariadb"}:
|
||
db.session.execute(text(
|
||
"ALTER TABLE insurance_ppt_templates "
|
||
"MODIFY source_template_asset_id VARCHAR(255)"
|
||
))
|
||
|
||
if "asset_sha256" not in columns:
|
||
db.session.execute(text(
|
||
"ALTER TABLE insurance_ppt_templates ADD COLUMN asset_sha256 VARCHAR(64)"
|
||
))
|
||
if "asset_version" not in columns:
|
||
db.session.execute(text(
|
||
"ALTER TABLE insurance_ppt_templates "
|
||
"ADD COLUMN asset_version INTEGER NOT NULL DEFAULT 1"
|
||
))
|
||
|
||
from insurance.ppt.template_asset_service import (
|
||
resolve_template_asset,
|
||
template_asset_sha256,
|
||
)
|
||
|
||
rows = db.session.execute(text(
|
||
"SELECT id, source_template_asset_id FROM insurance_ppt_templates "
|
||
"WHERE source_template_asset_id IS NOT NULL"
|
||
)).mappings().all()
|
||
for row in rows:
|
||
try:
|
||
path = resolve_template_asset(row["source_template_asset_id"])
|
||
digest = template_asset_sha256(path) if path and os.path.isfile(path) else None
|
||
except (OSError, ValueError):
|
||
digest = None
|
||
if digest:
|
||
db.session.execute(text(
|
||
"UPDATE insurance_ppt_templates "
|
||
"SET asset_sha256 = :digest, asset_version = COALESCE(asset_version, 1) "
|
||
"WHERE id = :id"
|
||
), {"digest": digest, "id": row["id"]})
|
||
|
||
db.session.execute(text(
|
||
"UPDATE insurance_ppt_templates SET clone_renderer = 'clone-edit-v2' "
|
||
"WHERE source_template_asset_id IS NOT NULL"
|
||
))
|
||
|
||
db.session.commit()
|
||
logger.info("[migrate_029] PPT 模板资产字段与指纹迁移完成")
|