migration1.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import async from "async";
  2. /**
  3. * Migration 1
  4. *
  5. * This migration is used to set the documentVersion to 1 for all documents that don't have a documentVersion yet, meaning they were created before the migration system
  6. * @param {object} MigrationModule - the MigrationModule
  7. * @returns {Promise} - returns promise
  8. */
  9. export default async function migrate(MigrationModule) {
  10. const activityModel = await MigrationModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  11. const newsModel = await MigrationModule.runJob("GET_MODEL", { modelName: "news" }, this);
  12. const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  13. const punishmentModel = await MigrationModule.runJob("GET_MODEL", { modelName: "punishment" }, this);
  14. const queueSongModel = await MigrationModule.runJob("GET_MODEL", { modelName: "queueSong" }, this);
  15. const reportModel = await MigrationModule.runJob("GET_MODEL", { modelName: "report" }, this);
  16. const songModel = await MigrationModule.runJob("GET_MODEL", { modelName: "song" }, this);
  17. const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
  18. const userModel = await MigrationModule.runJob("GET_MODEL", { modelName: "user" }, this);
  19. return new Promise((resolve, reject) => {
  20. async.waterfall(
  21. [
  22. next => {
  23. activityModel.updateMany(
  24. { documentVersion: null },
  25. { $set: { documentVersion: 1 } },
  26. (err, res) => {
  27. if (err) next(err);
  28. else {
  29. this.log(
  30. "INFO",
  31. `Migration 1 (activity). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  32. );
  33. next();
  34. }
  35. }
  36. );
  37. },
  38. next => {
  39. newsModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  40. if (err) next(err);
  41. else {
  42. this.log(
  43. "INFO",
  44. `Migration 1 (news). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  45. );
  46. next();
  47. }
  48. });
  49. },
  50. next => {
  51. playlistModel.updateMany(
  52. { documentVersion: null },
  53. { $set: { documentVersion: 1 } },
  54. (err, res) => {
  55. if (err) next(err);
  56. else {
  57. this.log(
  58. "INFO",
  59. `Migration 1 (playlist). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  60. );
  61. next();
  62. }
  63. }
  64. );
  65. },
  66. next => {
  67. punishmentModel.updateMany(
  68. { documentVersion: null },
  69. { $set: { documentVersion: 1 } },
  70. (err, res) => {
  71. if (err) next(err);
  72. else {
  73. this.log(
  74. "INFO",
  75. `Migration 1 (punishment). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  76. );
  77. next();
  78. }
  79. }
  80. );
  81. },
  82. next => {
  83. queueSongModel.updateMany(
  84. { documentVersion: null },
  85. { $set: { documentVersion: 1 } },
  86. (err, res) => {
  87. if (err) next(err);
  88. else {
  89. this.log(
  90. "INFO",
  91. `Migration 1 (queueSong). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  92. );
  93. next();
  94. }
  95. }
  96. );
  97. },
  98. next => {
  99. reportModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  100. if (err) next(err);
  101. else {
  102. this.log(
  103. "INFO",
  104. `Migration 1 (report). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  105. );
  106. next();
  107. }
  108. });
  109. },
  110. next => {
  111. songModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  112. if (err) next(err);
  113. else {
  114. this.log(
  115. "INFO",
  116. `Migration 1 (song). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  117. );
  118. next();
  119. }
  120. });
  121. },
  122. next => {
  123. stationModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  124. if (err) next(err);
  125. else {
  126. this.log(
  127. "INFO",
  128. `Migration 1 (station). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  129. );
  130. next();
  131. }
  132. });
  133. },
  134. next => {
  135. userModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  136. if (err) next(err);
  137. else {
  138. this.log(
  139. "INFO",
  140. `Migration 1 (user). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  141. );
  142. next();
  143. }
  144. });
  145. }
  146. ],
  147. (err, response) => {
  148. if (err) {
  149. reject(new Error(err));
  150. } else {
  151. resolve(response);
  152. }
  153. }
  154. );
  155. });
  156. }