20191107191318_saml-indentifiers-index.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* eslint-disable no-unused-vars */
  2. import Helpers from './lib/helpers.mjs'
  3. import mongodb from './lib/mongodb.mjs'
  4. const { getCollectionInternal } = mongodb
  5. const tags = ['saas']
  6. const indexes = [
  7. {
  8. key: {
  9. 'samlIdentifiers.externalUserId': 1,
  10. 'samlIdentifiers.providerId': 1,
  11. },
  12. name: 'samlIdentifiers.externalUserId_1_samlIdentifiers.providerId_1',
  13. sparse: true,
  14. },
  15. ]
  16. // Export indexes for use in the fix-up migration 20220105130000_fix_saml_indexes.js.
  17. const usersIndexes = indexes
  18. async function getCollection() {
  19. // This collection was incorrectly named - it should have been `users` instead
  20. // of `user`. The error is corrected by the subsequent migration
  21. // 20220105130000_fix_saml_indexes.js.
  22. return await getCollectionInternal('user')
  23. }
  24. const migrate = async client => {
  25. const collection = await getCollection()
  26. await Helpers.addIndexesToCollection(collection, indexes)
  27. }
  28. const rollback = async client => {
  29. const collection = await getCollection()
  30. try {
  31. await Helpers.dropIndexesFromCollection(collection, indexes)
  32. } catch (err) {
  33. console.error('Something went wrong rolling back the migrations', err)
  34. }
  35. }
  36. export default {
  37. tags,
  38. migrate,
  39. rollback,
  40. usersIndexes,
  41. }