2022-04-06 09:27:35 -04:00
|
|
|
const { ReadPreference } = require('mongodb')
|
|
|
|
const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
await waitForDb()
|
|
|
|
const projects = db.projects.find(
|
|
|
|
{},
|
|
|
|
{ projection: { rootFolder: 1 }, readPreference: ReadPreference.SECONDARY }
|
|
|
|
)
|
|
|
|
let projectsProcessed = 0
|
|
|
|
for await (const project of projects) {
|
|
|
|
projectsProcessed += 1
|
|
|
|
if (projectsProcessed % 100000 === 0) {
|
|
|
|
console.log(projectsProcessed, 'projects processed')
|
|
|
|
}
|
|
|
|
processProject(project)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processProject(project) {
|
2022-04-07 07:54:28 -04:00
|
|
|
if (!project.rootFolder || !Array.isArray(project.rootFolder)) {
|
|
|
|
console.log('BAD PATH:', project._id, 'rootFolder')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!project.rootFolder[0]) {
|
|
|
|
console.log('BAD PATH:', project._id, 'rootFolder.0')
|
|
|
|
return
|
|
|
|
}
|
2022-04-06 09:27:35 -04:00
|
|
|
const badPaths = findBadPaths(project.rootFolder[0])
|
|
|
|
for (const path of badPaths) {
|
|
|
|
console.log('BAD PATH:', project._id, `rootFolder.0.${path}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function findBadPaths(folder) {
|
|
|
|
const result = []
|
2022-05-18 08:41:39 -04:00
|
|
|
|
2022-05-19 07:29:29 -04:00
|
|
|
if (!folder._id) {
|
|
|
|
result.push('_id')
|
|
|
|
}
|
|
|
|
|
2022-05-18 08:41:39 -04:00
|
|
|
if (typeof folder.name !== 'string' || !folder.name) {
|
|
|
|
result.push('name')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (folder.folders) {
|
|
|
|
if (Array.isArray(folder.folders)) {
|
|
|
|
for (const [i, subfolder] of folder.folders.entries()) {
|
|
|
|
if (!subfolder || typeof subfolder !== 'object') {
|
|
|
|
result.push(`folders.${i}`)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for (const badPath of findBadPaths(subfolder)) {
|
|
|
|
result.push(`folders.${i}.${badPath}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push('folders')
|
2022-04-06 09:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
2022-05-18 08:41:39 -04:00
|
|
|
|
|
|
|
if (folder.docs) {
|
|
|
|
if (Array.isArray(folder.docs)) {
|
|
|
|
for (const [i, doc] of folder.docs.entries()) {
|
|
|
|
if (!doc || typeof doc !== 'object') {
|
|
|
|
result.push(`docs.${i}`)
|
|
|
|
continue
|
|
|
|
}
|
2022-05-19 07:29:29 -04:00
|
|
|
if (!doc._id) {
|
|
|
|
result.push(`docs.${i}._id`)
|
|
|
|
// no need to check further: this doc can be deleted
|
|
|
|
continue
|
|
|
|
}
|
2022-05-18 08:41:39 -04:00
|
|
|
if (typeof doc.name !== 'string' || !doc.name) {
|
|
|
|
result.push(`docs.${i}.name`)
|
|
|
|
}
|
2022-04-07 11:09:51 -04:00
|
|
|
}
|
2022-05-18 08:41:39 -04:00
|
|
|
} else {
|
|
|
|
result.push('docs')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (folder.fileRefs) {
|
|
|
|
if (Array.isArray(folder.fileRefs)) {
|
|
|
|
for (const [i, file] of folder.fileRefs.entries()) {
|
|
|
|
if (!file || typeof file !== 'object') {
|
|
|
|
result.push(`fileRefs.${i}`)
|
|
|
|
continue
|
|
|
|
}
|
2022-05-19 07:29:29 -04:00
|
|
|
if (!file._id) {
|
|
|
|
result.push(`fileRefs.${i}._id`)
|
|
|
|
// no need to check further: this file can be deleted
|
|
|
|
continue
|
|
|
|
}
|
2022-05-18 08:41:39 -04:00
|
|
|
if (typeof file.name !== 'string' || !file.name) {
|
|
|
|
result.push(`fileRefs.${i}.name`)
|
|
|
|
}
|
2022-04-06 09:27:35 -04:00
|
|
|
}
|
2022-05-18 08:41:39 -04:00
|
|
|
} else {
|
|
|
|
result.push('fileRefs')
|
2022-04-06 09:27:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|