diff --git a/services/history-v1/storage/lib/chunk_store/mongo.js b/services/history-v1/storage/lib/chunk_store/mongo.js index a49da744ed..e70bff3179 100644 --- a/services/history-v1/storage/lib/chunk_store/mongo.js +++ b/services/history-v1/storage/lib/chunk_store/mongo.js @@ -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, diff --git a/services/history-v1/storage/lib/chunk_store/postgres.js b/services/history-v1/storage/lib/chunk_store/postgres.js index 0f2b569260..857281151a 100644 --- a/services/history-v1/storage/lib/chunk_store/postgres.js +++ b/services/history-v1/storage/lib/chunk_store/postgres.js @@ -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) }) }