From 51e3afb7e7e23598e3963f1fef14c2f749d83448 Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 7 Jun 2026 22:46:46 -0400 Subject: [PATCH] Fix client presence cleanup index --- ...ixClientSessionActiveIndex1780863600000.ts | 40 +++++++++++++++++++ .../InitialTimescaleSchema1780859300000.ts | 2 +- src/ORM/_migrations/UniqueNonceIndex.ts | 2 +- src/ORM/client/client.service.ts | 4 +- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/ORM/_migrations/FixClientSessionActiveIndex1780863600000.ts diff --git a/src/ORM/_migrations/FixClientSessionActiveIndex1780863600000.ts b/src/ORM/_migrations/FixClientSessionActiveIndex1780863600000.ts new file mode 100644 index 0000000..eb2865e --- /dev/null +++ b/src/ORM/_migrations/FixClientSessionActiveIndex1780863600000.ts @@ -0,0 +1,40 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class FixClientSessionActiveIndex1780863600000 implements MigrationInterface { + public name = 'FixClientSessionActiveIndex1780863600000'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX IF EXISTS "IDX_unique_nonce"`); + await queryRunner.query(` + DELETE FROM "client_entity" + WHERE ctid IN ( + SELECT ctid + FROM ( + SELECT + ctid, + row_number() OVER ( + PARTITION BY "sessionId" + ORDER BY "createdAt" DESC, "id" DESC + ) AS rn + FROM "client_entity" + WHERE "deletedAt" IS NULL + ) duplicates + WHERE rn > 1 + ) + `); + await queryRunner.query(` + CREATE UNIQUE INDEX IF NOT EXISTS "IDX_unique_nonce" + ON "client_entity" ("sessionId") + WHERE "deletedAt" IS NULL + `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX IF EXISTS "IDX_unique_nonce"`); + await queryRunner.query(` + CREATE UNIQUE INDEX IF NOT EXISTS "IDX_unique_nonce" + ON "client_entity" ("sessionId") + WHERE "deletedAt" IS NOT NULL + `); + } +} diff --git a/src/ORM/_migrations/InitialTimescaleSchema1780859300000.ts b/src/ORM/_migrations/InitialTimescaleSchema1780859300000.ts index e0f4a77..82d6b61 100644 --- a/src/ORM/_migrations/InitialTimescaleSchema1780859300000.ts +++ b/src/ORM/_migrations/InitialTimescaleSchema1780859300000.ts @@ -38,7 +38,7 @@ export class InitialTimescaleSchema1780859300000 implements MigrationInterface { `); await queryRunner.query(`CREATE INDEX IF NOT EXISTS "IDX_client_entity_address" ON "client_entity" ("address")`); await queryRunner.query(`CREATE INDEX IF NOT EXISTS "idx_client_cleanup" ON "client_entity" ("id") WHERE "deletedAt" IS NULL`); - await queryRunner.query(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_unique_nonce" ON "client_entity" ("sessionId") WHERE "deletedAt" IS NOT NULL`); + await queryRunner.query(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_unique_nonce" ON "client_entity" ("sessionId") WHERE "deletedAt" IS NULL`); await queryRunner.query(` CREATE TABLE IF NOT EXISTS "accepted_share_entity" ( diff --git a/src/ORM/_migrations/UniqueNonceIndex.ts b/src/ORM/_migrations/UniqueNonceIndex.ts index ae78979..914cff3 100644 --- a/src/ORM/_migrations/UniqueNonceIndex.ts +++ b/src/ORM/_migrations/UniqueNonceIndex.ts @@ -4,7 +4,7 @@ export class UniqueNonceIndex implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { await queryRunner.query( - `CREATE UNIQUE INDEX IF NOT EXISTS "IDX_unique_nonce" ON "client_entity" ("sessionId") WHERE "deletedAt" IS NOT NULL` + `CREATE UNIQUE INDEX IF NOT EXISTS "IDX_unique_nonce" ON "client_entity" ("sessionId") WHERE "deletedAt" IS NULL` ); } diff --git a/src/ORM/client/client.service.ts b/src/ORM/client/client.service.ts index 40fab6c..e3cc3a0 100644 --- a/src/ORM/client/client.service.ts +++ b/src/ORM/client/client.service.ts @@ -93,7 +93,9 @@ export class ClientService { public async deleteAll() { return await this.clientRepository .createQueryBuilder() - .softDelete() + .delete() + .from(ClientEntity) + .where('"deletedAt" IS NULL') .execute(); }