|
|
@@ -1,5 +1,6 @@
|
|
|
import os
|
|
|
from sqlalchemy.orm import Session
|
|
|
+from sqlalchemy.exc import IntegrityError
|
|
|
from app.models import Project, DataVersion, DataFile
|
|
|
from app.config import settings
|
|
|
from app.services.gogs_client import GogsClient
|
|
|
@@ -23,7 +24,8 @@ class StorageService:
|
|
|
self.db.refresh(project)
|
|
|
return project
|
|
|
|
|
|
- def create_version(self, project_id: str, stage: str, commit_id: str, author: str, manifest: str) -> DataVersion:
|
|
|
+ def create_version(self, project_id: str, stage: str, commit_id: str, author: str, manifest: str) -> DataVersion | None:
|
|
|
+ """Create a new data version. Returns None if a duplicate exists (IntegrityError)."""
|
|
|
version = DataVersion(
|
|
|
project_id=project_id,
|
|
|
stage=stage,
|
|
|
@@ -31,10 +33,15 @@ class StorageService:
|
|
|
author=author,
|
|
|
manifest_snapshot=manifest
|
|
|
)
|
|
|
- self.db.add(version)
|
|
|
- self.db.commit()
|
|
|
- self.db.refresh(version)
|
|
|
- return version
|
|
|
+ try:
|
|
|
+ self.db.add(version)
|
|
|
+ self.db.commit()
|
|
|
+ self.db.refresh(version)
|
|
|
+ return version
|
|
|
+ except IntegrityError:
|
|
|
+ self.db.rollback()
|
|
|
+ logger.info(f"Version already exists for project {project_id}, stage {stage}, commit {commit_id[:8]}.")
|
|
|
+ return None
|
|
|
|
|
|
def rollback_version(self, version: DataVersion):
|
|
|
"""Remove a version and all its associated file records.
|