diff --git a/packages/server/modules/core/logger.ts b/packages/server/modules/core/logger.ts new file mode 100644 index 000000000..c7d5f7424 --- /dev/null +++ b/packages/server/modules/core/logger.ts @@ -0,0 +1,3 @@ +import { extendLoggerComponent, moduleLogger } from '@/logging/logging' + +export const coreLogger = extendLoggerComponent(moduleLogger, 'core') diff --git a/packages/server/modules/core/migrations/20240710154658_user_emails_backfill.ts b/packages/server/modules/core/migrations/20240710154658_user_emails_backfill.ts index 22bbba222..cc71f791a 100644 --- a/packages/server/modules/core/migrations/20240710154658_user_emails_backfill.ts +++ b/packages/server/modules/core/migrations/20240710154658_user_emails_backfill.ts @@ -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 { - 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 { .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 {