From 52954f37028aca2b2258cc6a5c4963f94adc4a20 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Wed, 5 Feb 2025 16:17:37 +0000 Subject: [PATCH] feat: add migration for ServiceProviderUserAuthToken and ServiceProviderProjectAuthToken tables --- Common/Server/API/SlackAPI.ts | 5 +-- .../1738772194675-MigrationName.ts | 40 +++++++++++++++++++ .../Postgres/SchemaMigrations/Index.ts | 2 + 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 Common/Server/Infrastructure/Postgres/SchemaMigrations/1738772194675-MigrationName.ts diff --git a/Common/Server/API/SlackAPI.ts b/Common/Server/API/SlackAPI.ts index ad0b158e7e..e1f2fdc2e3 100644 --- a/Common/Server/API/SlackAPI.ts +++ b/Common/Server/API/SlackAPI.ts @@ -22,9 +22,8 @@ import HTTPResponse from "../../Types/API/HTTPResponse"; import API from "../../Utils/API"; import ServiceProviderProjectAuthTokenService from "../Services/ServiceProviderProjectAuthTokenService"; import ObjectID from "../../Types/ObjectID"; -import { ServiceProviderType } from "../../Models/DatabaseModels/ServiceProviderProjectAuthToken"; import ServiceProviderUserAuthTokenService from "../Services/ServiceProviderUserAuthTokenService"; -import { ServiceProviderUserAuthTokenServiceProviderType } from "../../Models/DatabaseModels/ServiceProviderUserAuthToken"; +import ServiceProviderType from "../../Types/ServiceProvider/ServiceProviderType"; export default class SlackAPI { public getRouter(): ExpressRouter { @@ -236,7 +235,7 @@ export default class SlackAPI { await ServiceProviderUserAuthTokenService.refreshAuthToken({ projectId: new ObjectID(projectId), userId: new ObjectID(userId), - serviceProviderType: ServiceProviderUserAuthTokenServiceProviderType.Slack, + serviceProviderType: ServiceProviderType.Slack, authToken: slackUserAccessToken || "", serviceProviderUserId: slackUserId || "", miscData: { diff --git a/Common/Server/Infrastructure/Postgres/SchemaMigrations/1738772194675-MigrationName.ts b/Common/Server/Infrastructure/Postgres/SchemaMigrations/1738772194675-MigrationName.ts new file mode 100644 index 0000000000..a2a6c7b3e8 --- /dev/null +++ b/Common/Server/Infrastructure/Postgres/SchemaMigrations/1738772194675-MigrationName.ts @@ -0,0 +1,40 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class MigrationName1738772194675 implements MigrationInterface { + public name = 'MigrationName1738772194675' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE "ServiceProviderUserAuthToken" ("_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deletedAt" TIMESTAMP WITH TIME ZONE, "version" integer NOT NULL, "projectId" uuid NOT NULL, "authToken" text NOT NULL, "serviceProviderUserId" character varying(500) NOT NULL, "serviceProviderType" character varying(500) NOT NULL, "miscData" jsonb NOT NULL, "userId" uuid, "createdByUserId" uuid, "deletedByUserId" uuid, CONSTRAINT "PK_d16320dcfcb0ebb0c340e888507" PRIMARY KEY ("_id"))`); + await queryRunner.query(`CREATE INDEX "IDX_ee9d305ca2fe8d488d2a62427e" ON "ServiceProviderUserAuthToken" ("projectId") `); + await queryRunner.query(`CREATE INDEX "IDX_d822e61467a94b8f369df4faed" ON "ServiceProviderUserAuthToken" ("userId") `); + await queryRunner.query(`CREATE TABLE "ServiceProviderProjectAuthToken" ("_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deletedAt" TIMESTAMP WITH TIME ZONE, "version" integer NOT NULL, "projectId" uuid NOT NULL, "authToken" text NOT NULL, "serviceProviderType" character varying(500) NOT NULL, "serviceProviderProjectId" character varying(500) NOT NULL, "miscData" jsonb NOT NULL, "createdByUserId" uuid, "deletedByUserId" uuid, CONSTRAINT "PK_4f1e7a83253e520ab31d81596f0" PRIMARY KEY ("_id"))`); + await queryRunner.query(`CREATE INDEX "IDX_41ed33a7df1d96aa9a29b4c568" ON "ServiceProviderProjectAuthToken" ("projectId") `); + await queryRunner.query(`ALTER TABLE "OnCallDutyPolicyScheduleLayer" ALTER COLUMN "rotation" SET DEFAULT '{"_type":"Recurring","value":{"intervalType":"Day","intervalCount":{"_type":"PositiveNumber","value":1}}}'`); + await queryRunner.query(`ALTER TABLE "OnCallDutyPolicyScheduleLayer" ALTER COLUMN "restrictionTimes" SET DEFAULT '{"_type":"RestrictionTimes","value":{"restictionType":"None","dayRestrictionTimes":null,"weeklyRestrictionTimes":[]}}'`); + await queryRunner.query(`ALTER TABLE "ServiceProviderUserAuthToken" ADD CONSTRAINT "FK_ee9d305ca2fe8d488d2a62427e5" FOREIGN KEY ("projectId") REFERENCES "Project"("_id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "ServiceProviderUserAuthToken" ADD CONSTRAINT "FK_d822e61467a94b8f369df4faed0" FOREIGN KEY ("userId") REFERENCES "User"("_id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "ServiceProviderUserAuthToken" ADD CONSTRAINT "FK_539084afd2895cf223b4d434827" FOREIGN KEY ("createdByUserId") REFERENCES "User"("_id") ON DELETE SET NULL ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "ServiceProviderUserAuthToken" ADD CONSTRAINT "FK_32d31d8a370a31cb02619b47c9b" FOREIGN KEY ("deletedByUserId") REFERENCES "User"("_id") ON DELETE SET NULL ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "ServiceProviderProjectAuthToken" ADD CONSTRAINT "FK_41ed33a7df1d96aa9a29b4c568f" FOREIGN KEY ("projectId") REFERENCES "Project"("_id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "ServiceProviderProjectAuthToken" ADD CONSTRAINT "FK_90d33ae3ffafc76bfaf5308292c" FOREIGN KEY ("createdByUserId") REFERENCES "User"("_id") ON DELETE SET NULL ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "ServiceProviderProjectAuthToken" ADD CONSTRAINT "FK_efdf4b9a3b0dc87a9bc9ed99746" FOREIGN KEY ("deletedByUserId") REFERENCES "User"("_id") ON DELETE SET NULL ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "ServiceProviderProjectAuthToken" DROP CONSTRAINT "FK_efdf4b9a3b0dc87a9bc9ed99746"`); + await queryRunner.query(`ALTER TABLE "ServiceProviderProjectAuthToken" DROP CONSTRAINT "FK_90d33ae3ffafc76bfaf5308292c"`); + await queryRunner.query(`ALTER TABLE "ServiceProviderProjectAuthToken" DROP CONSTRAINT "FK_41ed33a7df1d96aa9a29b4c568f"`); + await queryRunner.query(`ALTER TABLE "ServiceProviderUserAuthToken" DROP CONSTRAINT "FK_32d31d8a370a31cb02619b47c9b"`); + await queryRunner.query(`ALTER TABLE "ServiceProviderUserAuthToken" DROP CONSTRAINT "FK_539084afd2895cf223b4d434827"`); + await queryRunner.query(`ALTER TABLE "ServiceProviderUserAuthToken" DROP CONSTRAINT "FK_d822e61467a94b8f369df4faed0"`); + await queryRunner.query(`ALTER TABLE "ServiceProviderUserAuthToken" DROP CONSTRAINT "FK_ee9d305ca2fe8d488d2a62427e5"`); + await queryRunner.query(`ALTER TABLE "OnCallDutyPolicyScheduleLayer" ALTER COLUMN "restrictionTimes" SET DEFAULT '{"_type": "RestrictionTimes", "value": {"restictionType": "None", "dayRestrictionTimes": null, "weeklyRestrictionTimes": []}}'`); + await queryRunner.query(`ALTER TABLE "OnCallDutyPolicyScheduleLayer" ALTER COLUMN "rotation" SET DEFAULT '{"_type": "Recurring", "value": {"intervalType": "Day", "intervalCount": {"_type": "PositiveNumber", "value": 1}}}'`); + await queryRunner.query(`DROP INDEX "public"."IDX_41ed33a7df1d96aa9a29b4c568"`); + await queryRunner.query(`DROP TABLE "ServiceProviderProjectAuthToken"`); + await queryRunner.query(`DROP INDEX "public"."IDX_d822e61467a94b8f369df4faed"`); + await queryRunner.query(`DROP INDEX "public"."IDX_ee9d305ca2fe8d488d2a62427e"`); + await queryRunner.query(`DROP TABLE "ServiceProviderUserAuthToken"`); + } + +} diff --git a/Common/Server/Infrastructure/Postgres/SchemaMigrations/Index.ts b/Common/Server/Infrastructure/Postgres/SchemaMigrations/Index.ts index 3c439bb495..8a33cbc55d 100644 --- a/Common/Server/Infrastructure/Postgres/SchemaMigrations/Index.ts +++ b/Common/Server/Infrastructure/Postgres/SchemaMigrations/Index.ts @@ -102,6 +102,7 @@ import { MigrationName1737997557974 } from "./1737997557974-MigrationName"; import { MigrationName1738675385487 } from "./1738675385487-MigrationName"; import { MigrationName1738676335575 } from "./1738676335575-MigrationName"; import { MigrationName1738756463613 } from "./1738756463613-MigrationName"; +import { MigrationName1738772194675 } from "./1738772194675-MigrationName"; export default [ InitialMigration, @@ -208,4 +209,5 @@ export default [ MigrationName1738675385487, MigrationName1738676335575, MigrationName1738756463613, + MigrationName1738772194675 ];