20190912145030_create_templates_indexes.mjs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 = ['server-pro', 'saas']
  6. const indexes = [
  7. {
  8. unique: true,
  9. key: {
  10. project_id: 1,
  11. },
  12. name: 'project_id_1',
  13. },
  14. {
  15. key: {
  16. user_id: 1,
  17. },
  18. name: 'user_id_1',
  19. },
  20. {
  21. key: {
  22. name: 1,
  23. },
  24. name: 'name_1',
  25. },
  26. ]
  27. async function getCollection() {
  28. // NOTE: This is a stale collection, it will get dropped in a later migration.
  29. return await getCollectionInternal('templates')
  30. }
  31. const migrate = async client => {
  32. const collection = await getCollection()
  33. await Helpers.addIndexesToCollection(collection, indexes)
  34. }
  35. const rollback = async client => {
  36. const collection = await getCollection()
  37. try {
  38. await Helpers.dropIndexesFromCollection(collection, indexes)
  39. } catch (err) {
  40. console.error('Something went wrong rolling back the migrations', err)
  41. }
  42. }
  43. export default {
  44. tags,
  45. migrate,
  46. rollback,
  47. }