debug_translate_updates.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. UpdateTranslator.convertToChanges(
  35. projectId,
  36. wrappedUpdates,
  37. (err, changes) => {
  38. if (err != null) {
  39. error(err)
  40. }
  41. console.log(JSON.stringify(changes, null, 2))
  42. }
  43. )
  44. }
  45. if (updates[0].resyncProjectStructure) {
  46. expandResyncProjectStructure(chunk, updates[0])
  47. } else {
  48. expandUpdates(updates)
  49. }
  50. function parseArgs() {
  51. const args = process.argv.slice(2)
  52. if (args.length !== 1) {
  53. console.log('Usage: debug_translate_updates.js DUMP_FILE')
  54. process.exit(1)
  55. }
  56. const filename = args[0]
  57. return { filename }
  58. }
  59. function parseDumpFile(filename) {
  60. const json = fs.readFileSync(filename)
  61. const { project_id: projectId, updates, chunk } = JSON.parse(json)
  62. return { projectId, updates, chunk }
  63. }
  64. function error(err) {
  65. console.error(err)
  66. process.exit(1)
  67. }