chore(useremails): add logging in backfill migration

This commit is contained in:
Alessandro Magionami 2024-07-31 09:51:30 +02:00
Родитель 3f7d3347d4
Коммит e43ba68001
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: EC367516F896CBA4
2 изменённых файлов: 21 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,3 @@
import { extendLoggerComponent, moduleLogger } from '@/logging/logging'
export const coreLogger = extendLoggerComponent(moduleLogger, 'core')

Просмотреть файл

@ -1,14 +1,24 @@
import { Knex } from 'knex'
import crs from 'crypto-random-string'
import { scanTableFactory } from '@/modules/core/helpers/scanTable'
import { coreLogger } from '@/modules/core/logger'
export async function up(knex: Knex): Promise<void> {
const batchSize = 1000
coreLogger.info('Migration user_emails_backfill started')
const batchSize = 1000
const [countQuery] = await knex('users').count()
const usersCount = parseInt(countQuery.count.toString())
const maxLoops = usersCount / batchSize
coreLogger.info(`Number of loops estimated: ${maxLoops}`)
let currentIteration = 1
for await (const rows of scanTableFactory({ db: knex })({
tableName: 'users',
batchSize
})) {
coreLogger.info(`Starting iteration ${currentIteration} with ${rows.length} rows`)
if (rows.length) {
await knex('user_emails')
.insert(
@ -23,7 +33,14 @@ export async function up(knex: Knex): Promise<void> {
.onConflict(['userId', 'email'])
.ignore()
}
currentIteration++
coreLogger.info(`Completed iteration ${currentIteration}`)
if (currentIteration > maxLoops + 2) {
throw new Error('Stopping migration to avoid infinite loop')
}
}
coreLogger.info('Migration user_emails_backfill started')
}
export async function down(knex: Knex): Promise<void> {