find_malformed_filetrees.mjs 2.7 KB

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