overleaf/services/web/migrations/lib/adapter.js
Jakob Ackermann 9dd047fb04 Merge pull request #15930 from overleaf/jpa-fix-east-hang
[web] migrations: connect to mongo lazily

GitOrigin-RevId: 565fc6c851bf5493d548330eb2024d418a977779
2023-11-27 09:04:05 +00:00

54 lines
1.2 KiB
JavaScript

const Path = require('path')
class Adapter {
constructor(params) {
if (
!process.env.SKIP_TAG_CHECK &&
!process.argv.includes('create') &&
!(process.argv.includes('-t') || process.argv.includes('--tags'))
) {
console.error("ERROR: must pass tags using '-t' or '--tags', exiting")
process.exit(1)
}
this.params = params || {}
}
getTemplatePath() {
return Path.resolve(__dirname, 'template.js')
}
async connect() {
const { waitForDb, db } = require('../../app/src/infrastructure/mongodb')
await waitForDb()
return { db }
}
disconnect() {
return Promise.resolve()
}
async getExecutedMigrationNames() {
const { db } = await this.connect()
const migrations = await db.migrations
.find({}, { sort: { migratedAt: 1 }, projection: { name: 1 } })
.toArray()
return migrations.map(migration => migration.name)
}
async markExecuted(name) {
const { db } = await this.connect()
return db.migrations.insertOne({
name,
migratedAt: new Date(),
})
}
async unmarkExecuted(name) {
const { db } = await this.connect()
return db.migrations.deleteOne({
name,
})
}
}
module.exports = Adapter