fix(database): keep migrations in alembic transactions (#6400)

This commit is contained in:
InfinityPacer
2026-08-23 00:27:55 +08:00
committed by GitHub
parent a59f1b928a
commit 176d9255e5
16 changed files with 659 additions and 154 deletions
+22 -5
View File
@@ -7,8 +7,8 @@ Create Date: 2025-05-13 19:49:51.271319
"""
import re
from app.db.oper.systemconfig import SystemConfigOper
from app.schemas.types import SystemConfigKey
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '486e56a62dcb'
@@ -20,16 +20,33 @@ depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
### 将消息模板中的 `season`(为单数字, 且重命名需要这个字段)替换为 `season_fmt`(Sxx格式字符串) ###
_systemconfig = SystemConfigOper()
templates = _systemconfig.get(SystemConfigKey.NotificationTemplates)
systemconfig = sa.table(
"systemconfig",
sa.column("key", sa.String()),
sa.column("value", sa.JSON()),
)
connection = op.get_bind()
key = "NotificationTemplates"
row = connection.execute(
sa.select(systemconfig.c.value).where(systemconfig.c.key == key)
).first()
templates = row[0] if row else None
if isinstance(templates, dict):
_re = r'(?<={{)(?![^}]*[%|])(\s*)season(\s*)(?=}})|(?<={%)if\s+(?![^%]*[%|])season\s*(?=%)'
changed = False
for k, v in templates.items():
# 替换season为season_fmt
result = re.sub(_re, r'\1season_fmt\2', v)
if result != v:
changed = True
templates[k] = result
# 将更新后的模板存回系统配置
_systemconfig.set(SystemConfigKey.NotificationTemplates, templates)
if changed:
connection.execute(
systemconfig.update().where(systemconfig.c.key == key).values(
value=templates
)
)
# ### end Alembic commands ###