fix(phase2): 修复模板发布时旧片段配置未删除的Bug
- 根因: publish_template_from_draft 中删除走 db.query().delete() 绕过了 repository 层 - 修复: delete_by_template 增加 commit 参数,publish 中走 repository 调用 - 修复后 stub 测试能正确验证删除逻辑,52个 edit_template_service 测试全绿
This commit is contained in:
@@ -754,8 +754,8 @@ class EditTemplateService:
|
||||
template.bump_version() # 版本号 +1
|
||||
updated_template = self._template_repo.update(template)
|
||||
|
||||
# 批量删除旧的片段配置(走 repository,保证测试 stub 和真实行为一致)
|
||||
self._clip_config_repo.delete_by_template(template_id)
|
||||
# 批量删除旧的片段配置(外层事务统一提交)
|
||||
self._clip_config_repo.delete_by_template(template_id, commit=False)
|
||||
|
||||
# 创建新的片段配置
|
||||
created_configs: list[TemplateClipConfig] = []
|
||||
|
||||
Regular → Executable
+9
-3
@@ -92,14 +92,20 @@ class SQLAlchemyTemplateClipConfigRepository:
|
||||
self.session.commit()
|
||||
return True
|
||||
|
||||
def delete_by_template(self, template_id: str) -> int:
|
||||
"""删除模板下所有片段配置,返回删除数量"""
|
||||
def delete_by_template(self, template_id: str, *, commit: bool = True) -> int:
|
||||
"""删除模板下所有片段配置,返回删除数量
|
||||
|
||||
Args:
|
||||
template_id: 模板ID
|
||||
commit: 是否提交事务,默认True。外层有事务控制时传False。
|
||||
"""
|
||||
count = (
|
||||
self.session.query(TemplateClipConfigModel)
|
||||
.filter(TemplateClipConfigModel.template_id == template_id)
|
||||
.delete()
|
||||
)
|
||||
self.session.commit()
|
||||
if commit:
|
||||
self.session.commit()
|
||||
return count
|
||||
|
||||
def count(self, *, template_id: Optional[str] = None) -> int:
|
||||
|
||||
@@ -167,7 +167,7 @@ class StubTemplateClipConfigRepository:
|
||||
def delete(self, config_id: str) -> bool:
|
||||
return self._configs.pop(config_id, None) is not None
|
||||
|
||||
def delete_by_template(self, template_id: str) -> int:
|
||||
def delete_by_template(self, template_id: str, *, commit: bool = True) -> int:
|
||||
ids = [cid for cid, c in self._configs.items() if c.template_id == template_id]
|
||||
for cid in ids:
|
||||
del self._configs[cid]
|
||||
|
||||
Reference in New Issue
Block a user