20191106102104_saml-log-indexes.mjs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. providerId: 1,
  10. },
  11. name: 'providerId_1',
  12. },
  13. {
  14. key: {
  15. sessionId: 1,
  16. },
  17. name: 'sessionId_1',
  18. },
  19. {
  20. // expire after 30 days
  21. expireAfterSeconds: 60 * 60 * 24 * 30,
  22. key: {
  23. createdAt: 1,
  24. },
  25. name: 'createdAt_1',
  26. },
  27. ]
  28. // Export indexes for use in the fix-up migration 20220105130000_fix_saml_indexes.js.
  29. const samlLogsIndexes = indexes
  30. async function getCollection() {
  31. // This collection was incorrectly named - it should have been `samlLogs`
  32. // instead of `samllog`. The error is corrected by the subsequent migration
  33. // 20220105130000_fix_saml_indexes.js.
  34. return await getCollectionInternal('samllog')
  35. }
  36. const migrate = async client => {
  37. const collection = await getCollection()
  38. await Helpers.addIndexesToCollection(collection, indexes)
  39. }
  40. const rollback = async client => {
  41. const collection = await getCollection()
  42. try {
  43. await Helpers.dropIndexesFromCollection(collection, indexes)
  44. } catch (err) {
  45. console.error('Something went wrong rolling back the migrations', err)
  46. }
  47. }
  48. export default {
  49. tags,
  50. migrate,
  51. rollback,
  52. samlLogsIndexes,
  53. }