migration1.js 4.6 KB

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