BackFillDocRevTests.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { db, ObjectId } from '../../../app/src/infrastructure/mongodb.mjs'
  2. import { promisify } from 'node:util'
  3. import { exec } from 'node:child_process'
  4. import logger from '@overleaf/logger'
  5. import { expect } from 'chai'
  6. describe('BackFillDocRevTests', function () {
  7. const docId1 = new ObjectId()
  8. const docId2 = new ObjectId()
  9. const docId3 = new ObjectId()
  10. beforeEach('insert docs', async function () {
  11. await db.docs.insertMany([
  12. { _id: docId1, deleted: true },
  13. { _id: docId2 },
  14. { _id: docId3, rev: 42 },
  15. ])
  16. })
  17. async function runScript() {
  18. let result
  19. try {
  20. result = await promisify(exec)(
  21. 'cd ../../tools/migrations && VERBOSE_LOGGING=true east migrate -t server-ce --force 20230315170739_back_fill_doc_rev'
  22. )
  23. } catch (error) {
  24. // dump details like exit code, stdErr and stdOut
  25. logger.error({ error }, 'script failed')
  26. throw error
  27. }
  28. const { stdout: stdOut } = result
  29. expect(stdOut).to.include('rev missing 2 | deleted=true 1')
  30. expect(stdOut).to.match(
  31. new RegExp(`Running update on batch with ids .+${docId1}`)
  32. )
  33. expect(stdOut).to.match(
  34. new RegExp(`Running update on batch with ids .+${docId2}`)
  35. )
  36. expect(stdOut).to.not.match(
  37. new RegExp(`Running update on batch with ids .+${docId3}`)
  38. )
  39. }
  40. describe('dry-run=false', function () {
  41. beforeEach('run script', async function () {
  42. await runScript()
  43. })
  44. it('should back fill the rev', async function () {
  45. const docs = await db.docs.find({}, { $sort: { _id: 1 } }).toArray()
  46. expect(docs).to.deep.equal([
  47. { _id: docId1, rev: 1, deleted: true },
  48. { _id: docId2, rev: 1 },
  49. { _id: docId3, rev: 42 },
  50. ])
  51. })
  52. })
  53. })