diff --git a/apps/api/app/services/edit_template_service.py b/apps/api/app/services/edit_template_service.py index ef8c72652..43cf7af99 100755 --- a/apps/api/app/services/edit_template_service.py +++ b/apps/api/app/services/edit_template_service.py @@ -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] = [] diff --git a/packages/adapters/sqlalchemy_impl/template_clip_config_repository.py b/packages/adapters/sqlalchemy_impl/template_clip_config_repository.py old mode 100644 new mode 100755 index 4e06c01a8..17ca3b73a --- a/packages/adapters/sqlalchemy_impl/template_clip_config_repository.py +++ b/packages/adapters/sqlalchemy_impl/template_clip_config_repository.py @@ -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: diff --git a/tests/unit/test_edit_template_service.py b/tests/unit/test_edit_template_service.py index 5a1d30f2d..4846b6b59 100755 --- a/tests/unit/test_edit_template_service.py +++ b/tests/unit/test_edit_template_service.py @@ -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]