Skip to content

Commit 18d1f8e

Browse files
committed
Better handling of individual errors
1 parent c0e2bcb commit 18d1f8e

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

legacy_migrate/src/migrate.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,9 @@ async function migrateUserEmailXref() {
11231123

11241124
async function migrateUserSocialLogin() {
11251125
console.log('→ user_social_login');
1126-
let skip = 0, total = 0;
1126+
let skip = 0,
1127+
total = 0,
1128+
failures = 0;
11271129
while (true) {
11281130
const batch: Rows<typeof sourceIdentity.user_social_login.findMany> =
11291131
await sourceIdentity.user_social_login.findMany(
@@ -1139,7 +1141,7 @@ async function migrateUserSocialLogin() {
11391141
);
11401142
if (batch.length === 0) break;
11411143

1142-
await target.$transaction(
1144+
const results = await Promise.allSettled(
11431145
batch.map((r: any) =>
11441146
target.user_social_login.upsert({
11451147
where: {
@@ -1166,14 +1168,38 @@ async function migrateUserSocialLogin() {
11661168
modify_date: r.modify_date ?? null,
11671169
},
11681170
})
1169-
),
1170-
{ timeout: 120_000 }
1171+
)
11711172
);
11721173

1173-
total += batch.length; skip += batch.length;
1174-
console.log(` … ${total}`);
1174+
results.forEach((result, idx) => {
1175+
if (result.status === 'fulfilled') {
1176+
total += 1;
1177+
} else {
1178+
const err: any = result.reason;
1179+
const record = batch[idx];
1180+
if (err?.code === 'P2003' && err?.meta?.constraint === 'user_social_user_fk') {
1181+
failures += 1;
1182+
appendNdjson('bad-user-social-login.ndjson', {
1183+
reason: 'foreign key violation: user missing',
1184+
user_id: record.user_id,
1185+
social_login_provider_id: record.social_login_provider_id,
1186+
social_user_id: record.social_user_id ?? null,
1187+
error: err.message,
1188+
});
1189+
} else {
1190+
throw err;
1191+
}
1192+
}
1193+
});
1194+
1195+
skip += batch.length;
1196+
console.log(` … imported=${total} (failed=${failures} so far)`);
11751197
}
1176-
console.log(`✓ user_social_login: ${total}`);
1198+
const summarySuffix =
1199+
failures > 0
1200+
? `, failed=${failures}. See logs/bad-user-social-login.ndjson`
1201+
: '';
1202+
console.log(`✓ user_social_login: imported=${total}${summarySuffix}`);
11771203
}
11781204

11791205
async function migrateUserSsoLogin() {

0 commit comments

Comments
 (0)