41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
"""迁移 031:增加海报模板布局与格式兼容字段。"""
|
||
import logging
|
||
|
||
from sqlalchemy import inspect, text
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
def migrate():
|
||
from insurance.db.compat import db
|
||
|
||
columns = {
|
||
item["name"]
|
||
for item in inspect(db.engine).get_columns("poster_templates")
|
||
}
|
||
additions = {
|
||
"layout_key": "VARCHAR(50)",
|
||
"supported_modes": "TEXT",
|
||
"supported_formats": "TEXT",
|
||
}
|
||
for name, column_type in additions.items():
|
||
if name not in columns:
|
||
db.session.execute(text(
|
||
f"ALTER TABLE poster_templates ADD COLUMN {name} {column_type}"
|
||
))
|
||
|
||
db.session.execute(text(
|
||
"UPDATE poster_templates SET layout_key = :layout "
|
||
"WHERE layout_key IS NULL OR layout_key = ''"
|
||
), {"layout": "legacy"})
|
||
db.session.execute(text(
|
||
"UPDATE poster_templates SET supported_modes = :modes "
|
||
"WHERE supported_modes IS NULL OR supported_modes = ''"
|
||
), {"modes": '["single","long"]'})
|
||
db.session.execute(text(
|
||
"UPDATE poster_templates SET supported_formats = :formats "
|
||
"WHERE supported_formats IS NULL OR supported_formats = ''"
|
||
), {"formats": '["single_2_3","single_9_16","long_1242_auto"]'})
|
||
db.session.commit()
|
||
logger.info("[migrate_031] 海报模板布局与格式兼容字段迁移完成")
|