From 4949b92be8941af1e7adf59b7ec05ddf1bca8cfb Mon Sep 17 00:00:00 2001 From: yiling Date: Mon, 25 May 2026 10:04:32 +0800 Subject: [PATCH] chore(prisma): add migration for cleanup job refactor and revert test-global-setup.ts --- .../migration.sql | 20 +++++++++++++++++++ src/services/asset/asset.ts | 5 ++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 prisma/migrations/20260525020408_refactor_cleanup_status/migration.sql diff --git a/prisma/migrations/20260525020408_refactor_cleanup_status/migration.sql b/prisma/migrations/20260525020408_refactor_cleanup_status/migration.sql new file mode 100644 index 00000000..2b14542e --- /dev/null +++ b/prisma/migrations/20260525020408_refactor_cleanup_status/migration.sql @@ -0,0 +1,20 @@ +/* + Warnings: + + - The values [purging] on the enum `AssetStatus` will be removed. If these variants are still used in the database, this will fail. + +*/ +-- CreateEnum +CREATE TYPE "StorageKeyStatus" AS ENUM ('active', 'purging'); + +-- AlterEnum +BEGIN; +CREATE TYPE "AssetStatus_new" AS ENUM ('uploading', 'uploaded', 'processing', 'processed', 'trashed', 'pending_purge'); +ALTER TABLE "assets" ALTER COLUMN "status" TYPE "AssetStatus_new" USING ("status"::text::"AssetStatus_new"); +ALTER TYPE "AssetStatus" RENAME TO "AssetStatus_old"; +ALTER TYPE "AssetStatus_new" RENAME TO "AssetStatus"; +DROP TYPE "public"."AssetStatus_old"; +COMMIT; + +-- AlterTable +ALTER TABLE "storage_keys" ADD COLUMN "status" "StorageKeyStatus" NOT NULL DEFAULT 'active'; diff --git a/src/services/asset/asset.ts b/src/services/asset/asset.ts index 67944b72..b490ff7b 100644 --- a/src/services/asset/asset.ts +++ b/src/services/asset/asset.ts @@ -1071,9 +1071,8 @@ export class AssetService { // using FOR UPDATE SKIP LOCKED to ensure multiple servers don't pick up the same keys. const lockedKeys = await this.prismaClient.$queryRaw<{ id: string; key: string }[]>` WITH to_purge AS ( - SELECT sk.id FROM storage_keys sk - LEFT JOIN assets a ON a.storage_key_id = sk.id - WHERE a.id IS NULL + SELECT id FROM storage_keys sk + WHERE NOT EXISTS (SELECT 1 FROM assets a WHERE a.storage_key_id = sk.id) AND sk.created_at < NOW() - INTERVAL '24 hours' AND (sk.status = 'active' OR (sk.status = 'purging' AND sk.updated_at < NOW() - INTERVAL '1 hour')) LIMIT 100