Skip to content

Commit

Permalink
[TECH] Remplit la colonne reconciledAt pour les anciennes certifica…
Browse files Browse the repository at this point in the history
…tions (PIX-14403). (#10190)
  • Loading branch information
alexandrecoin authored Sep 26, 2024
1 parent dc4f100 commit 6397a88
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { logger } from '../../src/shared/infrastructure/utils/logger.js';

const TABLE_NAME = 'certification-candidates';

const up = async function (knex) {
let numberOfBatchProcessed = 0;
const CHUNK_SIZE = 250000;

let hasNext = false;
do {
const updates = await knex.raw(
`
update "certification-candidates"
set "reconciledAt" = "subquery"."reconciledAt"
from (select
"certification-candidates"."id" as "id",
"certification-courses"."createdAt" as "reconciledAt"
from "certification-candidates"
left join "certification-courses" on "certification-courses"."userId" = "certification-candidates"."userId"
and "certification-courses"."sessionId" = "certification-candidates"."sessionId"
where "certification-candidates"."userId" is not null
and "certification-candidates"."reconciledAt" is null
limit ?) as "subquery"
where "certification-candidates"."id" = "subquery"."id"
returning "certification-candidates"."id", "certification-candidates"."reconciledAt";`,
[CHUNK_SIZE],
);

hasNext = updates.rowCount === CHUNK_SIZE;
++numberOfBatchProcessed;
logger.info(
`${TABLE_NAME}: Batch number ${numberOfBatchProcessed} : ${updates.rowCount} items updated. hasNext = ${hasNext}`,
);
} while (hasNext);
};

const down = async function (knex) {
return knex(TABLE_NAME).update('reconciledAt', null);
};

// We UPDATE in batch of CHUNK_SIZE, hence not needing a transaction for this migration
// @see: https://knexjs.org/guide/migrations.html#migration-api
const config = { transaction: false };

export { config, down, up };

0 comments on commit 6397a88

Please sign in to comment.