delete_test_dupes.js 2.3 KB

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