Skip to content

Commit

Permalink
fix(migration): correction de la migration de création de comptes de …
Browse files Browse the repository at this point in the history
…Bordeaux
  • Loading branch information
pYassine committed Sep 7, 2023
1 parent 793ed35 commit b839ecf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
1 change: 0 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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",
Expand Down Expand Up @@ -65,36 +76,21 @@ export class ManualMigration1694090707865 implements MigrationInterface {
login,
temporaryPassword,
};
jsonToExport.push(newUserUsager);

const options: UsagerOptions = {
...usager.options,
portailUsagerEnabled: true,
};

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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b839ecf

Please sign in to comment.