From b839ecf64a033ea11da0182c3ee5eb83b1dae521 Mon Sep 17 00:00:00 2001 From: Yassine R Date: Thu, 7 Sep 2023 22:15:17 +0200 Subject: [PATCH] =?UTF-8?q?fix(migration):=20correction=20de=20la=20migrat?= =?UTF-8?q?ion=20de=20cr=C3=A9ation=20de=20comptes=20de=20Bordeaux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/package.json | 1 - ...n.ts => 1694090707867-manual-migration.ts} | 56 +++++++++---------- .../_postgres/PG_CONNECT_OPTIONS.const.ts | 8 +-- 3 files changed, 30 insertions(+), 35 deletions(-) rename packages/backend/src/_migrations/{1694090707865-manual-migration.ts => 1694090707867-manual-migration.ts} (74%) diff --git a/packages/backend/package.json b/packages/backend/package.json index 9e5d246dbd..3ed845147f 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -15,7 +15,6 @@ "db:dev:create": "typeorm-ts-node-commonjs migration:create ./src/_migrations/manual-migration", "db:prod:data-anonymize": "node dist/run-data-anonymization.js ", "db:prod:migrate-down:last": "node dist/run-migrate-down-last.js", - "db:prod:migrate-up": "node dist/run-migrate-up.js", "db:test:migrate-down:last": "ENV_FILE=tests-local npx ts-node -r tsconfig-paths/register --transpile-only src/run-migrate-down-last.ts", "db:test:migrate-redo": "ENV_FILE=tests-local npx ts-node -r tsconfig-paths/register --transpile-only src/run-migrate-up-redo.ts", "db:test:migrate-up": "ENV_FILE=tests-local npx ts-node -r tsconfig-paths/register --transpile-only src/run-migrate-up.ts", diff --git a/packages/backend/src/_migrations/1694090707865-manual-migration.ts b/packages/backend/src/_migrations/1694090707867-manual-migration.ts similarity index 74% rename from packages/backend/src/_migrations/1694090707865-manual-migration.ts rename to packages/backend/src/_migrations/1694090707867-manual-migration.ts index a773e34cd1..cb87595e68 100644 --- a/packages/backend/src/_migrations/1694090707865-manual-migration.ts +++ b/packages/backend/src/_migrations/1694090707867-manual-migration.ts @@ -3,28 +3,39 @@ import { userUsagerCreator } from "../users/services"; import { format } from "date-fns"; import { getPhoneString } from "../util/phone"; import { join } from "path"; -import { ensureDir, writeFile } from "fs-extra"; +import { appendFile, ensureDir } from "fs-extra"; import { MigrationInterface, QueryRunner } from "typeorm"; import { domifaConfig } from "../config"; import { userStructureRepository, usagerRepository } from "../database"; -export class ManualMigration1694090707865 implements MigrationInterface { +const serializeUserUsager = (newUserUsager: any) => { + const { + ref, + customRef, + nom, + prenom, + dateNaissance, + telephone, + login, + temporaryPassword, + } = newUserUsager; + + return `${ref}\t${customRef}\t${nom}\t${prenom}\t${dateNaissance}\t${telephone}\t${login}\t${temporaryPassword}`; +}; + +export class ManualMigration1694090707866 implements MigrationInterface { // eslint-disable-next-line @typescript-eslint/no-unused-vars public async up(_queryRunner: QueryRunner): Promise { const message = "[MIGRATION] [BORDEAUX] "; await ensureDir(join(domifaConfig().upload.basePath, "tmp-bordeaux")); - console.log(message + "Activation des comptes usagers de bordeaux"); - let jsonToExport: { - ref: number; - telephone: string; - nom: string; - prenom: string; - login: string; - temporaryPassword: string; - customRef: string; - dateNaissance: string; - }[] = []; + const fileName = join( + domifaConfig().upload.basePath, + "tmp-bordeaux", + "comptes-bordeaux" + ); + + console.log(message + "Activation des comptes usagers de bordeaux"); const user: UserStructureProfile = await userStructureRepository.findOneBy({ role: "admin", @@ -65,7 +76,6 @@ export class ManualMigration1694090707865 implements MigrationInterface { login, temporaryPassword, }; - jsonToExport.push(newUserUsager); const options: UsagerOptions = { ...usager.options, @@ -73,28 +83,14 @@ export class ManualMigration1694090707865 implements MigrationInterface { }; await usagerRepository.update({ uuid: usager.uuid }, { options }); + await appendFile(fileName, serializeUserUsager(newUserUsager) + "\n"); cpt++; - if (cpt % 500 === 0) { - const fileName = join( - domifaConfig().upload.basePath, - "tmp-bordeaux", - "export_bordeaux" + Date.now() + ".json" - ); - await writeFile(fileName, JSON.stringify(jsonToExport)); - jsonToExport = []; - console.log(message + " écriture du fichier " + fileName); + if (cpt % 200 === 0) { console.log(message + cpt + "/" + usagers.length + " comptes créés"); } } - const fileName = join( - domifaConfig().upload.basePath, - "tmp-bordeaux", - "export_bordeaux" + Date.now() + ".json" - ); - await writeFile(fileName, JSON.stringify(jsonToExport)); - console.log(message + " écriture du fichier " + fileName); console.log(message + cpt + "/" + usagers.length + " comptes créés"); } diff --git a/packages/backend/src/database/services/_postgres/PG_CONNECT_OPTIONS.const.ts b/packages/backend/src/database/services/_postgres/PG_CONNECT_OPTIONS.const.ts index b0a67f2879..7076560fe7 100644 --- a/packages/backend/src/database/services/_postgres/PG_CONNECT_OPTIONS.const.ts +++ b/packages/backend/src/database/services/_postgres/PG_CONNECT_OPTIONS.const.ts @@ -2,11 +2,12 @@ import { PostgresConnectionOptions } from "typeorm/driver/postgres/PostgresConne import { domifaConfig } from "../../../config"; import { appLogger } from "../../../util"; import { CustomTypeOrmLogger } from "../../../util/CustomTypeOrmLogger"; +import { isCronEnabled } from "../../../config/services/isCronEnabled.service"; const isTypescriptMode = __filename.split(".").pop() === "ts"; // if current file extension is "ts": use src/*.ts files, eles use dist/*.js files const executeMigrations = - domifaConfig().envId === "preprod" || - domifaConfig().envId === "prod" || + ((domifaConfig().envId === "preprod" || domifaConfig().envId === "prod") && + isCronEnabled()) || domifaConfig().envId === "local"; let connectOptionsPaths: Pick< @@ -51,8 +52,7 @@ export const PG_CONNECT_OPTIONS: PostgresConnectionOptions = { }, type: "postgres", synchronize: false, - cache: true, - migrationsTransactionMode: "none", + migrationsTransactionMode: "each", migrationsRun: executeMigrations, host: domifaConfig().postgres.host, port: domifaConfig().postgres.port,