Skip to content

Commit

Permalink
Slow down migration
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Oct 28, 2021
1 parent 0a7fdac commit 2cbcfa5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/server/src/migrations/20211027112530_item_owner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Knex } from 'knex';
import { DbConnection } from '../db';
import { msleep } from '../utils/time';

export async function up(db: DbConnection): Promise<any> {
if (!(await db.schema.hasColumn('items', 'owner_id'))) {
Expand All @@ -22,7 +23,7 @@ export async function up(db: DbConnection): Promise<any> {
user_id: string;
}

const pageSize = 10000;
const pageSize = 1000;

const itemCount = (await db('items')
.count('id', { as: 'total' })
Expand All @@ -42,11 +43,15 @@ export async function up(db: DbConnection): Promise<any> {

console.info(`Processing items ${itemDone} / ${itemCount}`);

for (const item of items) {
await db('items').update({ owner_id: item.user_id }).where('id', '=', item.id);
}
await db.transaction(async trx => {
for (const item of items) {
await trx('items').update({ owner_id: item.user_id }).where('id', '=', item.id);
}
});

itemDone += items.length;

await msleep(10000);
}

await db.schema.alterTable('items', (table: Knex.CreateTableBuilder) => {
Expand Down

0 comments on commit 2cbcfa5

Please sign in to comment.