refactor(db)!: finalize pooled WNDB v2 migration

This commit is contained in:
2026-08-27 17:26:22 +08:00
parent fa188af0b1
commit b74799a39d
105 changed files with 4988 additions and 5565 deletions
+16 -4
View File
@@ -12,6 +12,7 @@ from fastapi import (
UploadFile,
status,
)
from starlette.concurrency import run_in_threadpool
from app.auth.metadata_dependencies import (
get_current_metadata_admin,
@@ -24,6 +25,7 @@ from app.auth.project_dependencies import (
from app.core.audit import AuditAction, log_audit_event
from app.infra.db.metadb.repositories.metadata_repository import MetadataRepository
from app.infra.db.project_routing import activate_project_routing
from app.native.wndb.core.database import MaterializedViewRefreshAfterCommitError
from app.services.network_import import network_update
from app.services.tjnetwork import run_inp
@@ -87,8 +89,8 @@ def _validate_inp_bytes(content: bytes, filename: str) -> str:
async def _read_upload(file: UploadFile) -> tuple[bytes, str]:
filename = Path(file.filename or "").name
content = await file.read(MAX_INP_FILE_BYTES + 1)
_validate_inp_bytes(content, filename)
return content, filename
normalized = _validate_inp_bytes(content, filename).encode("utf-8")
return normalized, filename
async def _audit_model_change(
@@ -114,7 +116,7 @@ async def _audit_model_change(
)
async def _run_uploaded_inp(content: bytes) -> str:
def _run_uploaded_inp_sync(content: bytes) -> str:
target_dir = Path("inp")
target_dir.mkdir(parents=True, exist_ok=True)
model_name = f"admin_model_{uuid4().hex}"
@@ -123,7 +125,11 @@ async def _run_uploaded_inp(content: bytes) -> str:
return run_inp(model_name)
async def _update_from_inp(content: bytes, project_code: str) -> None:
async def _run_uploaded_inp(content: bytes) -> str:
return await run_in_threadpool(_run_uploaded_inp_sync, content)
def _update_from_inp_sync(content: bytes, project_code: str) -> None:
temp_path: Path | None = None
try:
with NamedTemporaryFile(suffix=".inp", delete=False) as temp_file:
@@ -135,9 +141,15 @@ async def _update_from_inp(content: bytes, project_code: str) -> None:
temp_path.unlink(missing_ok=True)
async def _update_from_inp(content: bytes, project_code: str) -> None:
await run_in_threadpool(_update_from_inp_sync, content, project_code)
async def _apply_model_update(content: bytes, project_code: str) -> None:
try:
await _update_from_inp(content, project_code)
except MaterializedViewRefreshAfterCommitError:
raise
except Exception as exc:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,