2024-11-04 04:09:11 -05:00
|
|
|
import fs from 'node:fs'
|
2024-10-22 04:21:39 -04:00
|
|
|
import minimist from 'minimist'
|
2024-11-04 04:09:11 -05:00
|
|
|
import readline from 'node:readline'
|
2024-10-31 08:16:54 -04:00
|
|
|
import { db, ObjectId } from '../../app/src/infrastructure/mongodb.js'
|
2024-10-22 04:21:39 -04:00
|
|
|
import DocstoreManagerModule from '../../app/src/Features/Docstore/DocstoreManager.js'
|
|
|
|
|
|
|
|
const { promises: DocstoreManager } = DocstoreManagerModule
|
2020-03-09 09:36:41 -04:00
|
|
|
|
|
|
|
const argv = minimist(process.argv.slice(2))
|
|
|
|
const commit = argv.commit !== undefined
|
|
|
|
const offset = parseInt(argv.offset) || 0
|
|
|
|
const limit = parseInt(argv.limit) || 0
|
|
|
|
|
|
|
|
if (!commit) {
|
|
|
|
console.log('DOING DRY RUN. TO SAVE CHANGES PASS --commit')
|
|
|
|
}
|
|
|
|
|
|
|
|
const input = fs.createReadStream(argv._[0])
|
|
|
|
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
crlfDelay: Infinity,
|
2021-04-27 03:52:58 -04:00
|
|
|
input,
|
2020-03-09 09:36:41 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
const orphanedDocs = {}
|
|
|
|
|
|
|
|
console.log('Loading Data')
|
|
|
|
|
|
|
|
let idx = 0
|
|
|
|
let processed = 0
|
|
|
|
|
|
|
|
rl.on('line', async line => {
|
|
|
|
if (offset && idx++ < offset) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (limit && processed++ >= limit) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let [docId, projectId] = line.split(',')
|
|
|
|
docId = docId.replace(/^ObjectId\(/, '').replace(/\)$/, '')
|
|
|
|
projectId = projectId.replace(/^ObjectId\(/, '').replace(/\)$/, '')
|
|
|
|
|
|
|
|
try {
|
2023-12-18 05:54:01 -05:00
|
|
|
docId = new ObjectId(docId).toString()
|
|
|
|
projectId = new ObjectId(projectId).toString()
|
2020-03-09 09:36:41 -04:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`Invalid id: ${docId}, ${projectId}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!orphanedDocs[projectId]) {
|
|
|
|
orphanedDocs[projectId] = []
|
|
|
|
}
|
|
|
|
|
|
|
|
orphanedDocs[projectId].push(docId)
|
|
|
|
})
|
|
|
|
|
|
|
|
rl.on('close', async () => {
|
|
|
|
const docCount = Object.values(orphanedDocs).reduce((i, v) => i + v.length, 0)
|
|
|
|
const projectCount = Object.keys(orphanedDocs).length
|
|
|
|
|
|
|
|
console.log(`Loaded Data for ${docCount} docs in ${projectCount} Projects`)
|
|
|
|
|
|
|
|
for (const projectId of Object.keys(orphanedDocs)) {
|
|
|
|
await deleteOrphanedDocs(projectId, orphanedDocs[projectId])
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('DONE')
|
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
|
|
|
async function deleteOrphanedDocs(projectId, docIds) {
|
|
|
|
try {
|
|
|
|
if (await projectIdExists(projectId)) {
|
|
|
|
console.error(`Project id exists: ${projectId}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Error checking if project exists: ${projectId}`, err.stack)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Delete docs ${docIds.join(', ')} for project ${projectId}`)
|
|
|
|
|
|
|
|
if (!commit) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await DocstoreManager.destroyProject(projectId)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Error deleting project ${projectId}`, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function projectIdExists(projectId) {
|
|
|
|
// check both projects and deletedProjects to see if project id exists
|
|
|
|
const [project, deletedProject] = await Promise.all([
|
|
|
|
findProject(projectId),
|
2021-04-27 03:52:58 -04:00
|
|
|
findDeletedProject(projectId),
|
2020-03-09 09:36:41 -04:00
|
|
|
])
|
|
|
|
|
|
|
|
return project !== null || deletedProject !== null
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findProject(projectId) {
|
2020-10-07 09:17:49 -04:00
|
|
|
return db.projects.findOne(
|
2023-12-18 05:54:01 -05:00
|
|
|
{ _id: new ObjectId(projectId) },
|
2020-10-07 09:17:49 -04:00
|
|
|
{ projection: { _id: 1 } }
|
|
|
|
)
|
2020-03-09 09:36:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function findDeletedProject(projectId) {
|
2020-10-07 09:17:49 -04:00
|
|
|
return db.deletedProjects.findOne(
|
2023-12-18 05:54:01 -05:00
|
|
|
{ 'project._id': new ObjectId(projectId) },
|
2020-10-07 09:17:49 -04:00
|
|
|
{ projection: { _id: 1 } }
|
|
|
|
)
|
2020-03-09 09:36:41 -04:00
|
|
|
}
|