Merge pull request #24539 from overleaf/td-inactive-project-index-2

Update inactive project deactivation index, second attempt

GitOrigin-RevId: adc16761b001b2aa919b5a61cdbd468b7ad4e26d
This commit is contained in:
Tim Down
2025-03-27 09:50:16 +00:00
committed by Copybot
parent c55df0e803
commit 2d4b5e51f1
2 changed files with 64 additions and 3 deletions

View File

@@ -5,7 +5,6 @@ const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandle
const ProjectGetter = require('../Project/ProjectGetter')
const ProjectUpdateHandler = require('../Project/ProjectUpdateHandler')
const { Project } = require('../../models/Project')
const { ObjectId } = require('mongodb-legacy')
const Modules = require('../../infrastructure/Modules')
const { READ_PREFERENCE_SECONDARY } = require('../../infrastructure/mongodb')
const { callbackifyAll } = require('@overleaf/promise-utils')
@@ -62,8 +61,6 @@ const InactiveProjectManager = {
projects = await Project.find({
lastOpened: { $not: { $gt: oldProjectDate } },
})
.where('_id')
.lt(ObjectId.createFromTime(oldProjectDate / 1000))
.where('active')
.equals(true)
.select('_id')

View File

@@ -0,0 +1,64 @@
/* eslint-disable no-unused-vars */
import Helpers from './lib/helpers.mjs'
const tags = ['server-ce', 'server-pro', 'saas']
const oldIndex = {
key: {
lastOpened: 1,
},
name: 'lastOpened_1',
}
const newIndex = {
key: {
lastOpened: 1,
},
name: 'active_true_lastOpened_1',
partialFilterExpression: { active: true },
}
const tmpIndex = {
key: { lastOpened: 1, dummyField: 1 },
name: 'lastOpened_tmp',
}
const migrate = async client => {
const { db } = client
// Create a temporary index so that projects are not left unindexed while we
// drop the index and recreate it.
await Helpers.addIndexesToCollection(db.projects, [tmpIndex])
// Drop and recreate the index with different options
await Helpers.dropIndexesFromCollection(db.projects, [oldIndex])
await Helpers.addIndexesToCollection(db.projects, [newIndex])
// Drop the temporary index
await Helpers.dropIndexesFromCollection(db.projects, [tmpIndex])
}
const rollback = async client => {
const { db } = client
try {
// Create a temporary index so that projects are not left unindexed while we
// drop the index and recreate it.
await Helpers.addIndexesToCollection(db.projects, [tmpIndex])
// Drop and recreate the index with different options
await Helpers.dropIndexesFromCollection(db.projects, [newIndex])
await Helpers.addIndexesToCollection(db.projects, [oldIndex])
// Drop the temporary index
await Helpers.dropIndexesFromCollection(db.projects, [tmpIndex])
} catch (err) {
console.error('Something went wrong rolling back the migrations', err)
}
}
export default {
tags,
migrate,
rollback,
}