Skip to content

Commit

Permalink
fix(api): fix migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Oct 10, 2023
1 parent b314db7 commit 7fb2e4d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 78 deletions.
45 changes: 26 additions & 19 deletions packages/backend/src/_migrations/1696369714284-auto-migration.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void> {
Expand Down
81 changes: 44 additions & 37 deletions packages/backend/src/_migrations/1696882900837-manual-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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,
}
);
}
}
}
}
Expand Down
51 changes: 29 additions & 22 deletions packages/backend/src/_migrations/1696941100775-manual-migration.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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
Expand Down

0 comments on commit 7fb2e4d

Please sign in to comment.