-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TECH] Remplit la colonne
reconciledAt
pour les anciennes certifica…
- Loading branch information
1 parent
dc4f100
commit 6397a88
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
api/db/migrations/20240924091540_add-reconciliation-date-to-former-candidates.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |