From b8039917d0f0dda9f27965a336505ed525a269b8 Mon Sep 17 00:00:00 2001 From: murdle Date: Sun, 21 Dec 2025 15:24:19 +0200 Subject: [PATCH] im trying.. --- .../1766283926503-makeKeysNullable.ts | 103 +++++++++++------- 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/src/util/migration/postgres/1766283926503-makeKeysNullable.ts b/src/util/migration/postgres/1766283926503-makeKeysNullable.ts index 23cf10ef..fd9318b5 100644 --- a/src/util/migration/postgres/1766283926503-makeKeysNullable.ts +++ b/src/util/migration/postgres/1766283926503-makeKeysNullable.ts @@ -1,44 +1,69 @@ import { MigrationInterface, QueryRunner } from "typeorm"; export class MakeKeysNullable1766283926503 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE TABLE "temporary_push_subscriptions" ( + "id" varchar PRIMARY KEY NOT NULL, + "user_id" varchar, + "endpoint" varchar NOT NULL, + "expiration_time" bigint, + "auth" varchar, + "p256dh" varchar, + "type" text NOT NULL DEFAULT 'webpush', + CONSTRAINT "UQ_push_subscriptions_endpoint" UNIQUE ("endpoint"), + CONSTRAINT "FK_push_subscriptions_user" + FOREIGN KEY ("user_id") REFERENCES "users"("id") + ON DELETE SET NULL ON UPDATE NO ACTION + ) + `); + + await queryRunner.query(` + INSERT INTO "temporary_push_subscriptions"("id", "user_id", "endpoint", "expiration_time", "auth", "p256dh", "type") + SELECT "id", "user_id", "endpoint", "expiration_time", "auth", "p256dh", 'webpush' + FROM "push_subscriptions" + `); + + await queryRunner.query(`DROP TABLE "push_subscriptions"`); + + await queryRunner.query(`ALTER TABLE "temporary_push_subscriptions" RENAME TO "push_subscriptions"`); + + await queryRunner.query(` + CREATE UNIQUE INDEX IF NOT EXISTS "idx_push_subscriptions_endpoint" + ON "push_subscriptions" ("endpoint") + `); + } - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(` - ALTER TABLE "push_subscriptions" - ALTER COLUMN "auth" DROP NOT NULL; - `); - - await queryRunner.query(` - ALTER TABLE "push_subscriptions" - ALTER COLUMN "p256dh" DROP NOT NULL; - `); - - await queryRunner.query(` - ALTER TABLE "push_subscriptions" - ADD COLUMN "type" text; - `); - - await queryRunner.query(` - UPDATE "push_subscriptions" - SET "type" = 'webpush' - WHERE "type" IS NULL; - `); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(` - ALTER TABLE "push_subscriptions" - DROP COLUMN "type"; - `); - - await queryRunner.query(` - ALTER TABLE "push_subscriptions" - ALTER COLUMN "auth" SET NOT NULL; - `); - - await queryRunner.query(` - ALTER TABLE "push_subscriptions" - ALTER COLUMN "p256dh" SET NOT NULL; - `); - } + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE TABLE "temporary_push_subscriptions" ( + "id" varchar PRIMARY KEY NOT NULL, + "user_id" varchar, + "endpoint" varchar NOT NULL, + "expiration_time" bigint, + "auth" varchar NOT NULL, + "p256dh" varchar NOT NULL, + CONSTRAINT "UQ_push_subscriptions_endpoint" UNIQUE ("endpoint"), + CONSTRAINT "FK_push_subscriptions_user" + FOREIGN KEY ("user_id") REFERENCES "users"("id") + ON DELETE SET NULL ON UPDATE NO ACTION + ) + `); + + await queryRunner.query(` + INSERT INTO "temporary_push_subscriptions"("id", "user_id", "endpoint", "expiration_time", "auth", "p256dh") + SELECT "id", "user_id", "endpoint", "expiration_time", "auth", "p256dh" + FROM "push_subscriptions" + WHERE "auth" IS NOT NULL AND "p256dh" IS NOT NULL + `); + + await queryRunner.query(`DROP TABLE "push_subscriptions"`); + + await queryRunner.query(`ALTER TABLE "temporary_push_subscriptions" RENAME TO "push_subscriptions"`); + + await queryRunner.query(` + CREATE UNIQUE INDEX IF NOT EXISTS "idx_push_subscriptions_endpoint" + ON "push_subscriptions" ("endpoint") + `); + } } \ No newline at end of file