From 7fb2e4d1a95b8f2d4c84c4e73bb5be7e09cc9c62 Mon Sep 17 00:00:00 2001 From: Yassine R Date: Tue, 10 Oct 2023 15:57:48 +0200 Subject: [PATCH] fix(api): fix migrations --- .../1696369714284-auto-migration.ts | 45 ++++++----- .../1696882900837-manual-migration.ts | 81 ++++++++++--------- .../1696941100775-manual-migration.ts | 51 +++++++----- 3 files changed, 99 insertions(+), 78 deletions(-) diff --git a/packages/backend/src/_migrations/1696369714284-auto-migration.ts b/packages/backend/src/_migrations/1696369714284-auto-migration.ts index e97c7ac607..c9d2c9e813 100644 --- a/packages/backend/src/_migrations/1696369714284-auto-migration.ts +++ b/packages/backend/src/_migrations/1696369714284-auto-migration.ts @@ -1,28 +1,35 @@ import { MigrationInterface, QueryRunner } from "typeorm"; +import { domifaConfig } from "../config"; export class AutoMigration1696369714284 implements MigrationInterface { name = "AutoMigration1696369714284"; public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE EXTENSION Postgis;`); - await queryRunner.query( - `CREATE TABLE "open_data_places" ("uuid" 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(), "version" integer NOT NULL, "nom" text NOT NULL, "adresse" text NOT NULL, "complementAdresse" text, "ville" text, "codePostal" text, "departement" text NOT NULL, "region" text NOT NULL, "latitude" numeric(10,7) NOT NULL, "longitude" numeric(10,7) NOT NULL, "source" text NOT NULL, "uniqueId" text NOT NULL, "software" text, "structureId" integer, CONSTRAINT "PK_f80b64cfb42753deacd8bf6d78d" PRIMARY KEY ("uuid"))` - ); - await queryRunner.query( - `CREATE INDEX "IDX_d85d3252e11effca2f6b652fde" ON "open_data_places" ("codePostal") ` - ); - await queryRunner.query( - `CREATE INDEX "IDX_0408f9f2c0defbdc5e44f467a3" ON "open_data_places" ("departement") ` - ); - await queryRunner.query( - `CREATE INDEX "IDX_6e030c1cdb3fa54d0d735cdc6b" ON "open_data_places" ("region") ` - ); - await queryRunner.query( - `CREATE INDEX "IDX_d10ac71fca9180b787ef468659" ON "open_data_places" ("structureId") ` - ); - await queryRunner.query( - `ALTER TABLE "open_data_places" ADD CONSTRAINT "FK_d10ac71fca9180b787ef468659e" FOREIGN KEY ("structureId") REFERENCES "structure"("id") ON DELETE CASCADE ON UPDATE NO ACTION` - ); + if ( + domifaConfig().envId === "prod" || + domifaConfig().envId === "preprod" || + domifaConfig().envId === "local" + ) { + await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "postgis"`); + await queryRunner.query( + `CREATE TABLE "open_data_places" ("uuid" 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(), "version" integer NOT NULL, "nom" text NOT NULL, "adresse" text NOT NULL, "complementAdresse" text, "ville" text, "codePostal" text, "departement" text NOT NULL, "region" text NOT NULL, "latitude" numeric(10,7) NOT NULL, "longitude" numeric(10,7) NOT NULL, "source" text NOT NULL, "uniqueId" text NOT NULL, "software" text, "structureId" integer, CONSTRAINT "PK_f80b64cfb42753deacd8bf6d78d" PRIMARY KEY ("uuid"))` + ); + await queryRunner.query( + `CREATE INDEX "IDX_d85d3252e11effca2f6b652fde" ON "open_data_places" ("codePostal") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_0408f9f2c0defbdc5e44f467a3" ON "open_data_places" ("departement") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_6e030c1cdb3fa54d0d735cdc6b" ON "open_data_places" ("region") ` + ); + await queryRunner.query( + `CREATE INDEX "IDX_d10ac71fca9180b787ef468659" ON "open_data_places" ("structureId") ` + ); + await queryRunner.query( + `ALTER TABLE "open_data_places" ADD CONSTRAINT "FK_d10ac71fca9180b787ef468659e" FOREIGN KEY ("structureId") REFERENCES "structure"("id") ON DELETE CASCADE ON UPDATE NO ACTION` + ); + } } public async down(queryRunner: QueryRunner): Promise { diff --git a/packages/backend/src/_migrations/1696882900837-manual-migration.ts b/packages/backend/src/_migrations/1696882900837-manual-migration.ts index 6c3943c21b..fbf912afa6 100644 --- a/packages/backend/src/_migrations/1696882900837-manual-migration.ts +++ b/packages/backend/src/_migrations/1696882900837-manual-migration.ts @@ -2,50 +2,57 @@ import { MigrationInterface, QueryRunner } from "typeorm"; import { usagerRepository } from "../database"; import { Usager } from "../_common/model"; +import { domifaConfig } from "../config"; export class ManualMigration1696882900837 implements MigrationInterface { public async up(_queryRunner: QueryRunner): Promise { - const usagers: Usager[] = await usagerRepository - .createQueryBuilder("usager") - .select("*") - .where( - `(decision->>'dateFin' is null OR decision->>'dateDebut' is null) and decision->>'statut' not in ('INSTRUCTION', 'ATTENTE_DECISION')` - ) - .getRawMany(); + if ( + domifaConfig().envId === "prod" || + domifaConfig().envId === "preprod" || + domifaConfig().envId === "local" + ) { + const usagers: Usager[] = await usagerRepository + .createQueryBuilder("usager") + .select("*") + .where( + `(decision->>'dateFin' is null OR decision->>'dateDebut' is null) and decision->>'statut' not in ('INSTRUCTION', 'ATTENTE_DECISION')` + ) + .getRawMany(); - for (const usager of usagers) { - if (usager.decision.dateDebut && !usager.decision.dateFin) { - usager.decision.dateFin = usager.decision.dateDebut; - } else if (!usager.decision.dateDebut && usager.decision.dateFin) { - usager.decision.dateDebut = usager.decision.dateFin; - } else if (usager.decision.dateDecision) { - usager.decision.dateFin = usager.decision.dateDecision; - usager.decision.dateDebut = usager.decision.dateDecision; - } - if (!usager.decision.dateDecision) { - usager.decision.dateDecision = usager.decision.dateDebut; - } + for (const usager of usagers) { + if (usager.decision.dateDebut && !usager.decision.dateFin) { + usager.decision.dateFin = usager.decision.dateDebut; + } else if (!usager.decision.dateDebut && usager.decision.dateFin) { + usager.decision.dateDebut = usager.decision.dateFin; + } else if (usager.decision.dateDecision) { + usager.decision.dateFin = usager.decision.dateDecision; + usager.decision.dateDebut = usager.decision.dateDecision; + } + if (!usager.decision.dateDecision) { + usager.decision.dateDecision = usager.decision.dateDebut; + } - if (!usager.decision.dateDecision) { - usager.decision.dateFin = usager.createdAt; - usager.decision.dateDebut = usager.createdAt; - usager.decision.dateDecision = usager.createdAt; - } + if (!usager.decision.dateDecision) { + usager.decision.dateFin = usager.createdAt; + usager.decision.dateDebut = usager.createdAt; + usager.decision.dateDecision = usager.createdAt; + } - usager.historique[usager.historique.length - 1] = usager.decision; + usager.historique[usager.historique.length - 1] = usager.decision; - if ( - usager.decision.statut && - usager.decision.dateDebut && - usager.decision.dateFin - ) { - await usagerRepository.update( - { uuid: usager.uuid }, - { - decision: usager.decision, - historique: usager.historique, - } - ); + if ( + usager.decision.statut && + usager.decision.dateDebut && + usager.decision.dateFin + ) { + await usagerRepository.update( + { uuid: usager.uuid }, + { + decision: usager.decision, + historique: usager.historique, + } + ); + } } } } diff --git a/packages/backend/src/_migrations/1696941100775-manual-migration.ts b/packages/backend/src/_migrations/1696941100775-manual-migration.ts index 10c8f47ad0..a3e49aad8d 100644 --- a/packages/backend/src/_migrations/1696941100775-manual-migration.ts +++ b/packages/backend/src/_migrations/1696941100775-manual-migration.ts @@ -1,35 +1,42 @@ import { MigrationInterface, QueryRunner } from "typeorm"; import { usagerRepository } from "../database"; import { appLogger } from "../util"; +import { domifaConfig } from "../config"; export class ManualMigration1696941100775 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { - const condition = `("options"->'npai'->>'actif')::boolean is false and decision->>'statut' = 'RADIE' and "structureId" = 201 and decision->>'dateDebut' < '2023-06-01'`; - const before: number = await usagerRepository - .createQueryBuilder("usager") - .select("COUNT(uuid)") - .where(condition) - .getCount(); + if ( + domifaConfig().envId === "prod" || + domifaConfig().envId === "preprod" || + domifaConfig().envId === "local" + ) { + const condition = `("options"->'npai'->>'actif')::boolean is false and decision->>'statut' = 'RADIE' and "structureId" = 201 and decision->>'dateDebut' < '2023-06-01'`; + const before: number = await usagerRepository + .createQueryBuilder("usager") + .select("COUNT(uuid)") + .where(condition) + .getCount(); - appLogger.info( - "[MIGRATION] Mise à jour des 'NPAI' pour les radiés de Bordeaux ..." - ); - appLogger.info("Avant la migration: " + before); + appLogger.info( + "[MIGRATION] Mise à jour des 'NPAI' pour les radiés de Bordeaux ..." + ); + appLogger.info("Avant la migration: " + before); - await queryRunner.query( - `UPDATE usager SET "options" = jsonb_set("options", '{npai,actif}', 'true', false) WHERE ("options"->'npai'->>'actif')::boolean is false AND decision->>'statut' = 'RADIE' AND "structureId" = 201 AND decision->>'dateDebut' < '2023-06-01'` - ); + await queryRunner.query( + `UPDATE usager SET "options" = jsonb_set("options", '{npai,actif}', 'true', false) WHERE ("options"->'npai'->>'actif')::boolean is false AND decision->>'statut' = 'RADIE' AND "structureId" = 201 AND decision->>'dateDebut' < '2023-06-01'` + ); - const after: number = await usagerRepository - .createQueryBuilder("usager") - .select("COUNT(uuid)") - .where(condition) - .getCount(); + const after: number = await usagerRepository + .createQueryBuilder("usager") + .select("COUNT(uuid)") + .where(condition) + .getCount(); - appLogger.info("Après la migration: " + after); - appLogger.info( - "[MIGRATION] Mise à jour des 'NPAI' pour les radiés de Bordeaux ✅" - ); + appLogger.info("Après la migration: " + after); + appLogger.info( + "[MIGRATION] Mise à jour des 'NPAI' pour les radiés de Bordeaux ✅" + ); + } } // eslint-disable-next-line @typescript-eslint/no-unused-vars