20200729120824_update_subscriptions_manager_ids_index.mjs 942 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* eslint-disable no-unused-vars */
  2. import Helpers from './lib/helpers.mjs'
  3. const tags = ['saas']
  4. const oldIndex = {
  5. unique: true,
  6. key: {
  7. manager_ids: 1,
  8. },
  9. name: 'manager_ids_1',
  10. partialFilterExpression: {
  11. manager_ids: {
  12. $exists: true,
  13. },
  14. },
  15. }
  16. const newIndex = {
  17. key: {
  18. manager_ids: 1,
  19. },
  20. name: 'manager_ids_1',
  21. sparse: true,
  22. }
  23. const migrate = async client => {
  24. const { db } = client
  25. await Helpers.dropIndexesFromCollection(db.subscriptions, [oldIndex])
  26. await Helpers.addIndexesToCollection(db.subscriptions, [newIndex])
  27. }
  28. const rollback = async client => {
  29. const { db } = client
  30. try {
  31. await Helpers.dropIndexesFromCollection(db.subscriptions, [newIndex])
  32. await Helpers.addIndexesToCollection(db.subscriptions, [oldIndex])
  33. } catch (err) {
  34. console.error('Something went wrong rolling back the migrations', err)
  35. }
  36. }
  37. export default {
  38. tags,
  39. migrate,
  40. rollback,
  41. }