mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-25 19:02:20 +00:00
a4fa86deed
Do not fail to drop non-existent indexes GitOrigin-RevId: 226eba50a395f62751028e22030a53a0487f5343
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
const {
|
|
db,
|
|
getCollectionNames,
|
|
getCollectionInternal,
|
|
waitForDb,
|
|
} = require('../../app/src/infrastructure/mongodb')
|
|
|
|
async function addIndexesToCollection(collection, indexes) {
|
|
return Promise.all(
|
|
indexes.map(index => {
|
|
index.background = true
|
|
return collection.createIndex(index.key, index)
|
|
})
|
|
)
|
|
}
|
|
|
|
async function dropIndexesFromCollection(collection, indexes) {
|
|
return Promise.all(
|
|
indexes.map(async index => {
|
|
try {
|
|
await collection.dropIndex(index.name)
|
|
} catch (err) {
|
|
if (err.code === 27 /* IndexNotFound */) {
|
|
console.log(`Index ${index.name} not found; drop was a no-op.`)
|
|
} else {
|
|
throw err
|
|
}
|
|
}
|
|
})
|
|
)
|
|
}
|
|
|
|
async function dropCollection(collectionName) {
|
|
await waitForDb()
|
|
if (db[collectionName]) {
|
|
throw new Error(`blocking drop of an active collection: ${collectionName}`)
|
|
}
|
|
|
|
const allCollections = await getCollectionNames()
|
|
if (!allCollections.includes(collectionName)) return
|
|
const collection = await getCollectionInternal(collectionName)
|
|
await collection.drop()
|
|
}
|
|
|
|
module.exports = {
|
|
addIndexesToCollection,
|
|
dropIndexesFromCollection,
|
|
dropCollection,
|
|
}
|