overleaf/services/web/migrations/lib/helpers.mjs
Liangjun Song 9e15c73228 Merge pull request #20732 from overleaf/revert-20731-revert-20480-ls-convert-migration-scripts-to-esm
Revert "Revert "Convert migration scripts to ESM""

GitOrigin-RevId: 0430a3cd02b9d23bf0f4573346351dcf4ee17fa6
2024-10-14 10:58:50 +00:00

64 lines
1.7 KiB
JavaScript

// @ts-check
import mongodb from '../../app/src/infrastructure/mongodb.js'
const { db, getCollectionNames, getCollectionInternal, waitForDb } = 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()
}
/**
* Asserts that a dependent migration has run. Throws an error otherwise.
*
* @param {string} migrationName
*/
async function assertDependency(migrationName) {
await waitForDb()
const migrations = await getCollectionInternal('migrations')
const migration = await migrations.findOne({ name: migrationName })
if (migration == null) {
throw new Error(
`Bad migration order: ${migrationName} should run before this migration`
)
}
}
export default {
addIndexesToCollection,
dropIndexesFromCollection,
dropCollection,
assertDependency,
}