🐛 Skip failed file(s) for file migration (#896)

This commit is contained in:
Montassar Ghanmy
2025-05-19 15:34:49 +01:00
committed by GitHub
parent 87417b5ada
commit 34e935027f
@@ -67,37 +67,38 @@ const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
console.log(`User ${user.id} has ${userFiles.getEntities().length} files`); console.log(`User ${user.id} has ${userFiles.getEntities().length} files`);
const userFilesObjects = []; const userFilesObjects = [];
const failedFiles: { id: string; name: string }[] = [];
for (const userFile of userFiles.getEntities()) { for (const userFile of userFiles.getEntities()) {
if (userFile.migrated) { let fileObject: any = {};
continue; try {
} if (userFile.migrated) {
const filePathItems = await getPath(userFile.id, documentsRepo, true, { continue;
company: { id: userCompany }, }
} as any); const filePathItems = await getPath(userFile.id, documentsRepo, true, {
company: { id: userCompany },
} as any);
const filePath = filePathItems const filePath = filePathItems
.slice(1, -1) .slice(1, -1)
.map(p => p.name) .map(p => p.name)
.join("/"); .join("/");
const fileObject = { fileObject = {
owner: userId, owner: userId,
_id: userFile.id, _id: userFile.id,
is_in_trash: userFile.is_in_trash, is_in_trash: userFile.is_in_trash,
is_directory: userFile.is_directory, is_directory: userFile.is_directory,
name: userFile.name, name: userFile.name,
added: userFile.added, added: userFile.added,
last_modified: userFile.last_modified, last_modified: userFile.last_modified,
size: userFile.size, size: userFile.size,
path: filePath !== "My Drive" ? filePath : "", path: filePath !== "My Drive" ? filePath : "",
company_id: userCompany, company_id: userCompany,
}; };
userFilesObjects.push(fileObject); userFilesObjects.push(fileObject);
if (!dryRun) { if (!dryRun) {
try {
const cozyUrl = `${userId}.${COZY_DOMAIN}`; const cozyUrl = `${userId}.${COZY_DOMAIN}`;
const userToken = await getDriveToken(cozyUrl); const userToken = await getDriveToken(cozyUrl);
const client = new CozyClient({ const client = new CozyClient({
@@ -124,7 +125,8 @@ const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
); );
if (!archiveOrFile.file) { if (!archiveOrFile.file) {
console.error(`File ${userFile.id} was returned as archive. Skipping.`); console.error(`⚠️ File ${userFile.id} was returned as archive. Skipping.`);
failedFiles.push({ id: userFile.id, name: userFile.name });
continue; continue;
} }
let uploadedBytes = 0; let uploadedBytes = 0;
@@ -148,6 +150,7 @@ const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
if (!resp.ok) { if (!resp.ok) {
console.error(`❌ ERROR UPLOADING THE FILE: ${fileObject.name}`); console.error(`❌ ERROR UPLOADING THE FILE: ${fileObject.name}`);
console.error(`❌ ERROR: ${JSON.stringify(resp)} ${resp}`); console.error(`❌ ERROR: ${JSON.stringify(resp)} ${resp}`);
failedFiles.push({ id: userFile.id, name: userFile.name });
continue; continue;
} }
// 3. Migrate file // 3. Migrate file
@@ -156,14 +159,24 @@ const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
await documentsRepo.save(userFile); await documentsRepo.save(userFile);
console.log(`\n✅ File migrated successfully: ${fileObject.name}`); console.log(`\n✅ File migrated successfully: ${fileObject.name}`);
} catch (error) { } else {
console.error(`❌ ERROR CREATING THE FILE: ${fileObject.name}`); console.log(
console.error(`❌ ERROR: ${JSON.stringify(error)} ${error}`); `[DRY-RUN] Would create Cozy instance for user ${user.email_canonical}`,
);
} }
} else { } catch (error) {
console.log(`[DRY-RUN] Would create Cozy instance for user ${user.email_canonical}`); console.error(`❌ Exception while processing: ${fileObject.name}`);
console.error(error);
failedFiles.push({ id: userFile.id, name: userFile.name });
} }
} }
if (failedFiles.length > 0) {
console.log("\n📋 Migration failed for the following files:");
console.table(failedFiles);
console.log(`❌ Total failed files: ${failedFiles.length}`);
} else {
console.log("\n🎉 All files migrated successfully. No failures.");
}
} }
}); });
}); });