فهرست منبع

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
Brian Gough 1 سال پیش
والد
کامیت
098d91f0bb
2فایلهای تغییر یافته به همراه28 افزوده شده و 0 حذف شده
  1. 26 0
      services/history-v1/storage/lib/chunk_store/mongo.js
  2. 2 0
      services/history-v1/storage/lib/chunk_store/postgres.js

+ 26 - 0
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,

+ 2 - 0
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)
   })
 }