Sharkey/packages/backend/migration/1750591589187-registry-unique-constraints.js
dakkar 3aa5ec713c unique constraint for registry_item - fixes #1121
we remove all duplicate items from the registry, keeping the
most-recently updated one (which presumably is the one that the users
expect to be used)

then we use `upsert` instead of the more complicated previous logic,
which was subject to races
2025-06-22 13:10:22 +01:00

19 lines
709 B
JavaScript

/*
* SPDX-FileCopyrightText: dakkar and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
export class RegistryUniqueConstraints1750591589187 {
async up(queryRunner) {
await queryRunner.query(`DELETE FROM "registry_item" WHERE "id" IN (
SELECT t."id" FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY "userId","key","scope","domain" ORDER BY "updatedAt" DESC) rn
FROM "registry_item"
) t WHERE t.rn>1)`);
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_d9c48d580287308f8c1f674946" ON "registry_item" ("userId", "key", "scope", "domain") NULLS NOT DISTINCT`);
}
async down(queryRunner) {
await queryRunner.query(`DROP INDEX "public"."IDX_d9c48d580287308f8c1f674946"`);
}
}