find_malformed_filetrees.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const {
  2. db,
  3. waitForDb,
  4. READ_PREFERENCE_SECONDARY,
  5. } = require('../app/src/infrastructure/mongodb')
  6. async function main() {
  7. await waitForDb()
  8. const projects = db.projects.find(
  9. {},
  10. {
  11. projection: { rootFolder: 1 },
  12. readPreference: READ_PREFERENCE_SECONDARY,
  13. }
  14. )
  15. let projectsProcessed = 0
  16. for await (const project of projects) {
  17. projectsProcessed += 1
  18. if (projectsProcessed % 100000 === 0) {
  19. console.log(projectsProcessed, 'projects processed')
  20. }
  21. processProject(project)
  22. }
  23. }
  24. function processProject(project) {
  25. if (!project.rootFolder || !Array.isArray(project.rootFolder)) {
  26. console.log('BAD PATH:', project._id, 'rootFolder')
  27. return
  28. }
  29. if (!project.rootFolder[0]) {
  30. console.log('BAD PATH:', project._id, 'rootFolder.0')
  31. return
  32. }
  33. const badPaths = findBadPaths(project.rootFolder[0])
  34. for (const path of badPaths) {
  35. console.log('BAD PATH:', project._id, `rootFolder.0.${path}`)
  36. }
  37. }
  38. function findBadPaths(folder) {
  39. const result = []
  40. if (!folder._id) {
  41. result.push('_id')
  42. }
  43. if (typeof folder.name !== 'string' || !folder.name) {
  44. result.push('name')
  45. }
  46. if (folder.folders) {
  47. if (Array.isArray(folder.folders)) {
  48. for (const [i, subfolder] of folder.folders.entries()) {
  49. if (!subfolder || typeof subfolder !== 'object') {
  50. result.push(`folders.${i}`)
  51. continue
  52. }
  53. for (const badPath of findBadPaths(subfolder)) {
  54. result.push(`folders.${i}.${badPath}`)
  55. }
  56. }
  57. } else {
  58. result.push('folders')
  59. }
  60. }
  61. if (folder.docs) {
  62. if (Array.isArray(folder.docs)) {
  63. for (const [i, doc] of folder.docs.entries()) {
  64. if (!doc || typeof doc !== 'object') {
  65. result.push(`docs.${i}`)
  66. continue
  67. }
  68. if (!doc._id) {
  69. result.push(`docs.${i}._id`)
  70. // no need to check further: this doc can be deleted
  71. continue
  72. }
  73. if (typeof doc.name !== 'string' || !doc.name) {
  74. result.push(`docs.${i}.name`)
  75. }
  76. }
  77. } else {
  78. result.push('docs')
  79. }
  80. }
  81. if (folder.fileRefs) {
  82. if (Array.isArray(folder.fileRefs)) {
  83. for (const [i, file] of folder.fileRefs.entries()) {
  84. if (!file || typeof file !== 'object') {
  85. result.push(`fileRefs.${i}`)
  86. continue
  87. }
  88. if (!file._id) {
  89. result.push(`fileRefs.${i}._id`)
  90. // no need to check further: this file can be deleted
  91. continue
  92. }
  93. if (typeof file.name !== 'string' || !file.name) {
  94. result.push(`fileRefs.${i}.name`)
  95. }
  96. }
  97. } else {
  98. result.push('fileRefs')
  99. }
  100. }
  101. return result
  102. }
  103. main()
  104. .then(() => {
  105. process.exit(0)
  106. })
  107. .catch(err => {
  108. console.error(err)
  109. process.exit(1)
  110. })