debug_translate_updates.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 '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. [update],
  25. cb => cb(), // extend lock
  26. (err, result) => {
  27. console.log('err', err, 'result', JSON.stringify(result, null, 2))
  28. process.exit()
  29. }
  30. )
  31. }
  32. function expandUpdates(updates) {
  33. const wrappedUpdates = updates.map(update => ({ update }))
  34. let changes
  35. try {
  36. changes = UpdateTranslator.convertToChanges(projectId, wrappedUpdates)
  37. } catch (err) {
  38. error(err)
  39. }
  40. console.log(JSON.stringify(changes, null, 2))
  41. }
  42. if (updates[0].resyncProjectStructure) {
  43. expandResyncProjectStructure(chunk, updates[0])
  44. } else {
  45. expandUpdates(updates)
  46. }
  47. function parseArgs() {
  48. const args = process.argv.slice(2)
  49. if (args.length !== 1) {
  50. console.log('Usage: debug_translate_updates.js DUMP_FILE')
  51. process.exit(1)
  52. }
  53. const filename = args[0]
  54. return { filename }
  55. }
  56. function parseDumpFile(filename) {
  57. const json = fs.readFileSync(filename)
  58. const { project_id: projectId, updates, chunk } = JSON.parse(json)
  59. return { projectId, updates, chunk }
  60. }
  61. function error(err) {
  62. console.error(err)
  63. process.exit(1)
  64. }