From c45687ef183983efc738e52842cc1fb53afd8ccd Mon Sep 17 00:00:00 2001 From: "tao.chen" <93983997+taochen-ct@users.noreply.github.com> Date: Fri, 14 Aug 2026 13:44:57 +0800 Subject: [PATCH] fix: update unique key --- backend/src/backend/resources.py | 1 + backend/src/backend/scripts.py | 26 ++++++++++++++++++++++--- backend/src/backend/services/storage.py | 1 + common/src/common/db/models/scripts.py | 8 -------- common/src/common/db/models/storage.py | 1 - 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/backend/src/backend/resources.py b/backend/src/backend/resources.py index c26bd76..d163d4f 100644 --- a/backend/src/backend/resources.py +++ b/backend/src/backend/resources.py @@ -413,6 +413,7 @@ async def delete_resource( await soft_delete_object(resource.storage_object_id, request, session) resource.status = "deleted" resource.deleted_at = datetime.now(UTC).replace(tzinfo=None) + resource.is_deleted = 1 return { "request_id": context.request_id, "data": {"resource_id": resource_id, "status": "deleted"}, diff --git a/backend/src/backend/scripts.py b/backend/src/backend/scripts.py index 94dcc89..e499c58 100644 --- a/backend/src/backend/scripts.py +++ b/backend/src/backend/scripts.py @@ -504,10 +504,7 @@ async def create_script_record( ) try: session.add(storage_object) - session.add(script) await session.flush() - await session.refresh(storage_object) - await session.refresh(script) except Exception: # Best-effort compensating cleanup: remove the Jupyter file we # just created so a failed flush does not leave an orphan on disk. @@ -522,6 +519,27 @@ async def create_script_record( f"after DB flush error: {cleanup_exc.status_code} {cleanup_exc.detail}" ) raise + await session.refresh(storage_object) + + try: + session.add(script) + await session.flush() + except Exception: + # If the Scripts row fails, the active transaction rolls back the + # StorageObjects insert as well because both share the same session. + # Still attempt to remove the Jupyter file we just pushed. + try: + await runtime_client.delete_file(workspace_id, name=jupyter_path) + except RuntimeClientError as cleanup_exc: + if cleanup_exc.status_code == 404: + pass + else: + logger.warning( + f"failed to clean up Jupyter file {jupyter_path} " + f"after DB flush error: {cleanup_exc.status_code} {cleanup_exc.detail}" + ) + raise + await session.refresh(script) return script, storage_object @@ -975,6 +993,7 @@ async def delete_workspace_directory( ) if script is not None: script.status = "deleted" + script.is_deleted = 1 script.deleted_at = datetime.now(UTC).replace(tzinfo=None) deleted_scripts += 1 descendant.object_status = "deleted" @@ -1287,6 +1306,7 @@ async def delete_script( script.status = "deleted" script.deleted_at = datetime.now(UTC).replace(tzinfo=None) + script.is_deleted = 1 return { "request_id": context.request_id, "data": { diff --git a/backend/src/backend/services/storage.py b/backend/src/backend/services/storage.py index 0c9d0fd..33890cc 100644 --- a/backend/src/backend/services/storage.py +++ b/backend/src/backend/services/storage.py @@ -603,6 +603,7 @@ async def soft_delete_object( item.trash_key = trash_key item.object_status = "deleted" item.deleted_at = _utcnow_naive() + item.is_deleted = 1 return { "data": { "storage_object_id": storage_object_id, diff --git a/common/src/common/db/models/scripts.py b/common/src/common/db/models/scripts.py index 535b0de..e956747 100644 --- a/common/src/common/db/models/scripts.py +++ b/common/src/common/db/models/scripts.py @@ -19,14 +19,6 @@ class Scripts(Base): "visibility", "status", ), - Index("uk_scripts_current_object", "current_object_id", unique=True), - Index( - "uk_scripts_workspace_name", - "workspace_id", - "script_name", - "script_type", - unique=True, - ), {"comment": "可执行 Python/Notebook 脚本"}, ) diff --git a/common/src/common/db/models/storage.py b/common/src/common/db/models/storage.py index 0cd7daf..2a04b55 100644 --- a/common/src/common/db/models/storage.py +++ b/common/src/common/db/models/storage.py @@ -131,7 +131,6 @@ class DataResources(Base): "visibility", "status", ), - Index("uk_data_resources_object", "storage_object_id", unique=True), {"comment": "数据资源"}, )