Skip to content

Commit 548e7f0

Browse files
authored
fix: migration unique check (#3756)
1 parent 38f2b64 commit 548e7f0

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

bin/migrateKratosUsers.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { logger as parentLogger } from '../src/logger';
66
const logger = parentLogger.child({ command: 'migrate-kratos-users' });
77

88
const DEFAULT_LIMIT = 10_000;
9-
const BATCH_SIZE = 500;
9+
const BATCH_SIZE = 2_000;
1010

1111
type Cursor = {
1212
createdAt: Date;
@@ -129,7 +129,9 @@ const migratePasswordAccounts = async (
129129
if (remaining <= 0) break;
130130

131131
const fetchSize = Math.min(BATCH_SIZE, remaining);
132+
const fetchStart = Date.now();
132133
const batch = await fetchPasswordIdentities(kratosPool, cursor, fetchSize);
134+
const fetchMs = Date.now() - fetchStart;
133135
if (batch.length === 0) break;
134136

135137
processed += batch.length;
@@ -152,12 +154,14 @@ const migratePasswordAccounts = async (
152154
);
153155
}
154156

157+
const insertStart = Date.now();
155158
const { rowCount } = await dailyPool.query(
156159
`INSERT INTO ba_account (id, "userId", "providerId", "accountId", password, "createdAt", "updatedAt")
157160
VALUES ${placeholders.join(', ')}
158161
ON CONFLICT ("userId", "providerId") DO NOTHING`,
159162
values,
160163
);
164+
const insertMs = Date.now() - insertStart;
161165
total += rowCount ?? 0;
162166

163167
cursor = cursorFromRow(batch[batch.length - 1]);
@@ -167,6 +171,8 @@ const migratePasswordAccounts = async (
167171
batchSize: batch.length,
168172
inserted: rowCount,
169173
total,
174+
fetchMs,
175+
insertMs,
170176
},
171177
'Migrated password batch',
172178
);
@@ -193,7 +199,9 @@ const migrateOidcAccounts = async (
193199
if (remaining <= 0) break;
194200

195201
const fetchSize = Math.min(BATCH_SIZE, remaining);
202+
const fetchStart = Date.now();
196203
const rawBatch = await fetchOidcIdentities(kratosPool, cursor, fetchSize);
204+
const fetchMs = Date.now() - fetchStart;
197205
if (rawBatch.length === 0) break;
198206

199207
processed += rawBatch.length;
@@ -226,12 +234,14 @@ const migrateOidcAccounts = async (
226234

227235
if (placeholders.length === 0) continue;
228236

237+
const insertStart = Date.now();
229238
const { rowCount } = await dailyPool.query(
230239
`INSERT INTO ba_account (id, "userId", "providerId", "accountId", "createdAt", "updatedAt")
231240
VALUES ${placeholders.join(', ')}
232241
ON CONFLICT ("userId", "providerId") DO NOTHING`,
233242
values,
234243
);
244+
const insertMs = Date.now() - insertStart;
235245
total += rowCount ?? 0;
236246

237247
logger.info(
@@ -240,6 +250,8 @@ const migrateOidcAccounts = async (
240250
batchSize: rawBatch.length,
241251
inserted: rowCount,
242252
total,
253+
fetchMs,
254+
insertMs,
243255
},
244256
'Migrated OIDC batch',
245257
);
@@ -271,28 +283,20 @@ const parseLimit = (): number => {
271283
const limit = parseLimit();
272284

273285
try {
286+
const overallStart = Date.now();
274287
logger.info(
275288
{ resumeFrom: startCursor.createdAt, limit },
276289
'Starting Kratos to BetterAuth user migration',
277290
);
278291

279-
const password = await migratePasswordAccounts(
280-
kratosPool,
281-
dailyPool,
282-
startCursor,
283-
limit,
284-
);
292+
const [password, oidc] = await Promise.all([
293+
migratePasswordAccounts(kratosPool, dailyPool, startCursor, limit),
294+
migrateOidcAccounts(kratosPool, dailyPool, startCursor, limit),
295+
]);
285296
logger.info(
286297
{ count: password.count },
287298
'Password account migration complete',
288299
);
289-
290-
const oidc = await migrateOidcAccounts(
291-
kratosPool,
292-
dailyPool,
293-
startCursor,
294-
limit,
295-
);
296300
logger.info({ count: oidc.count }, 'OIDC account migration complete');
297301

298302
const endCursor =
@@ -306,6 +310,7 @@ const parseLimit = (): number => {
306310
passwordCount: password.count,
307311
oidcCount: oidc.count,
308312
cursor: encoded,
313+
totalMs: Date.now() - overallStart,
309314
},
310315
'Kratos to BetterAuth migration complete',
311316
);

0 commit comments

Comments
 (0)