delete_test_dupes.mjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { db } from '../../app/src/infrastructure/mongodb.mjs'
  2. import minimist from 'minimist'
  3. const argv = minimist(process.argv.slice(2))
  4. const commit = argv.commit !== undefined
  5. if (!commit) {
  6. console.log('DOING DRY RUN. TO SAVE CHANGES PASS --commit')
  7. }
  8. async function getDupes(commit) {
  9. const entries = await db.splittests.aggregate([
  10. {
  11. $match: {
  12. archived: { $eq: true },
  13. },
  14. },
  15. { $unwind: '$versions' },
  16. {
  17. $group: {
  18. // Group by fields to match on (a,b)
  19. _id: {
  20. _id: '$_id',
  21. name: '$name',
  22. creationDate: '$version.creationDate',
  23. },
  24. // Count number of matching docs for the group
  25. count: { $sum: 1 },
  26. // Save the _id for matching docs
  27. docs: { $push: '$_id' },
  28. },
  29. },
  30. // Limit results to duplicates (more than 1 match)
  31. {
  32. $match: {
  33. count: { $gt: 1 },
  34. },
  35. },
  36. ])
  37. let entry
  38. const removed = []
  39. while ((entry = await entries.next())) {
  40. const name = entry._id.name
  41. const test = await db.splittests.findOne({ name })
  42. if (hasArchiveDupe(test.versions)) {
  43. removed.push(test.name)
  44. removeLastVersion(test, commit)
  45. }
  46. }
  47. const message = commit
  48. ? `removed dupes from ${removed.length} feature flags`
  49. : `planning to remove dupes from ${removed.length} feature flags`
  50. console.info(message, removed)
  51. console.log('DONE')
  52. process.exit()
  53. }
  54. function hasArchiveDupe(versions) {
  55. const last = versions.length - 1
  56. // guard in case we somehow get smthn with only one version here flagged as having a dupe
  57. if (last < 2) return false
  58. // need to string compare dates, as otherwise will compare the isoDate objects (diff objs so not equal)
  59. return (
  60. versions[last].createdAt.toString() ===
  61. versions[last - 1].createdAt.toString()
  62. )
  63. }
  64. function removeLastVersion(test, commit) {
  65. const name = test.name
  66. const numVersions = test.versions.length
  67. if (name && numVersions > 1) {
  68. const lastVersion = test.versions[numVersions - 1].versionNumber
  69. console.log(`removing test ${test.name} version ${lastVersion}`)
  70. if (commit) {
  71. db.splittests.updateOne(
  72. { name },
  73. { $pull: { versions: { versionNumber: lastVersion } } }
  74. )
  75. }
  76. }
  77. }
  78. getDupes(commit)