From 78fbf12f8f59a93ac3a4f18bf0c9191a5893a535 Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 7 Jun 2026 22:13:08 -0400 Subject: [PATCH] Make share index migration online safe --- .../AcceptedShareIndex1780862400000.ts | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/ORM/_migrations/AcceptedShareIndex1780862400000.ts b/src/ORM/_migrations/AcceptedShareIndex1780862400000.ts index fbb6a0d..04986bd 100644 --- a/src/ORM/_migrations/AcceptedShareIndex1780862400000.ts +++ b/src/ORM/_migrations/AcceptedShareIndex1780862400000.ts @@ -10,7 +10,11 @@ export class AcceptedShareIndex1780862400000 implements MigrationInterface { await queryRunner.query(` SELECT setval( 'accepted_share_index_seq', - GREATEST((SELECT COUNT(*) FROM "accepted_share_entity") + 1, 1), + GREATEST( + (SELECT COUNT(*) FROM "accepted_share_entity") + 1, + (SELECT COALESCE(MAX("shareIndex"), 0) + 1 FROM "accepted_share_entity"), + 1 + ), false ) `); @@ -18,28 +22,10 @@ export class AcceptedShareIndex1780862400000 implements MigrationInterface { ALTER TABLE "accepted_share_entity" ALTER COLUMN "shareIndex" SET DEFAULT nextval('accepted_share_index_seq') `); - await queryRunner.query(` - WITH ordered AS ( - SELECT - "id", - "acceptedAt", - row_number() OVER (ORDER BY "acceptedAt", "id") AS "shareIndex" - FROM "accepted_share_entity" - WHERE "shareIndex" IS NULL - ) - UPDATE "accepted_share_entity" AS share - SET "shareIndex" = ordered."shareIndex" - FROM ordered - WHERE share."id" = ordered."id" - AND share."acceptedAt" = ordered."acceptedAt" - `); - await queryRunner.query(` - ALTER TABLE "accepted_share_entity" - ALTER COLUMN "shareIndex" SET NOT NULL - `); await queryRunner.query(` CREATE INDEX IF NOT EXISTS "IDX_accepted_share_order" ON "accepted_share_entity" ("shareIndex" DESC) + WHERE "shareIndex" IS NOT NULL `); }