debug_translate_updates.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env node
  2. /**
  3. * This script takes a dump file, obtained via the /project/:project_id/dump
  4. * endpoint and feeds it to the update translator to how updates are transfomed
  5. * into changes sent to v1 history.
  6. */
  7. import fs from 'node:fs'
  8. import * as UpdateTranslator from '../app/js/UpdateTranslator.js'
  9. import * as SyncManager from '../app/js/SyncManager.js'
  10. import * as HistoryStoreManager from '../app/js/HistoryStoreManager.js'
  11. const { filename } = parseArgs()
  12. const { projectId, updates, chunk } = parseDumpFile(filename)
  13. function expandResyncProjectStructure(chunk, update) {
  14. HistoryStoreManager._mocks.getMostRecentChunk = function (
  15. projectId,
  16. projectHistoryId,
  17. callback
  18. ) {
  19. callback(null, chunk)
  20. }
  21. SyncManager.expandSyncUpdates(
  22. projectId,
  23. 99999, // dummy history id
  24. chunk,
  25. [update],
  26. cb => cb(), // extend lock
  27. (err, result) => {
  28. console.log('err', err, 'result', JSON.stringify(result, null, 2))
  29. process.exit()
  30. }
  31. )
  32. }
  33. function expandUpdates(updates) {
  34. const wrappedUpdates = updates.map(update => ({ update }))
  35. let changes
  36. try {
  37. changes = UpdateTranslator.convertToChanges(projectId, wrappedUpdates)
  38. } catch (err) {
  39. error(err)
  40. }
  41. console.log(JSON.stringify(changes, null, 2))
  42. }
  43. if (updates[0].resyncProjectStructure) {
  44. expandResyncProjectStructure(chunk, updates[0])
  45. } else {
  46. expandUpdates(updates)
  47. }
  48. function parseArgs() {
  49. const args = process.argv.slice(2)
  50. if (args.length !== 1) {
  51. console.log('Usage: debug_translate_updates.js DUMP_FILE')
  52. process.exit(1)
  53. }
  54. const filename = args[0]
  55. return { filename }
  56. }
  57. function parseDumpFile(filename) {
  58. const json = fs.readFileSync(filename)
  59. const { project_id: projectId, updates, chunk } = JSON.parse(json)
  60. return { projectId, updates, chunk }
  61. }
  62. function error(err) {
  63. console.error(err)
  64. process.exit(1)
  65. }