Merge pull request #23345 from overleaf/bg-write-latest-history-version-to-project

update project entry with history metadata on chunk creation

GitOrigin-RevId: dd19898f3d16e2e3360ff1bcccbf79f7dd27addb
This commit is contained in:
Brian Gough
2025-02-04 10:05:02 +00:00
committed by Copybot
parent da816f9478
commit 777bb3211e
2 changed files with 28 additions and 0 deletions

View File

@@ -153,6 +153,30 @@ async function confirmCreate(projectId, chunk, chunkId, mongoOpts = {}) {
}
}
/**
* Write the metadata to the project record
*/
async function updateProjectRecord(projectId, chunk, mongoOpts = {}) {
// record the end version against the project
await mongodb.projects.updateOne(
{
'overleaf.history.id': projectId, // string for Object ids, number for postgres ids
},
{
// always store the latest end version and timestamp for the chunk
$max: {
'overleaf.history.currentEndVersion': chunk.getEndVersion(),
'overleaf.history.currentEndTimestamp': chunk.getEndTimestamp(),
'overleaf.history.updatedAt': new Date(),
},
// store the first pending change timestamp for the chunk, this will
// be cleared every time a backup is completed.
$min: { 'overleaf.backup.pendingChangeAt': chunk.getEndTimestamp() },
},
mongoOpts
)
}
/**
* Record that a chunk was replaced by a new one.
*/
@@ -167,6 +191,7 @@ async function confirmUpdate(projectId, oldChunkId, newChunk, newChunkId) {
await session.withTransaction(async () => {
await deleteChunk(projectId, oldChunkId, { session })
await confirmCreate(projectId, newChunk, newChunkId, { session })
await updateProjectRecord(projectId, newChunk, { session })
})
} finally {
await session.endSession()
@@ -273,6 +298,7 @@ module.exports = {
insertPendingChunk,
confirmCreate,
confirmUpdate,
updateProjectRecord,
deleteChunk,
deleteProjectChunks,
getOldChunksBatch,

View File

@@ -3,6 +3,7 @@ const assert = require('../assert')
const knex = require('../knex')
const knexReadOnly = require('../knex_read_only')
const { ChunkVersionConflictError } = require('./errors')
const { updateProjectRecord } = require('./mongo')
const DUPLICATE_KEY_ERROR_CODE = '23505'
@@ -150,6 +151,7 @@ async function confirmUpdate(projectId, oldChunkId, newChunk, newChunkId) {
_deletePendingChunk(tx, projectId, newChunkId),
_insertChunk(tx, projectId, newChunk, newChunkId),
])
await updateProjectRecord(projectId, newChunk)
})
}