migration1.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. this.log(
  31. "INFO",
  32. `Migration 1 (activity). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  33. );
  34. next();
  35. }
  36. }
  37. );
  38. },
  39. next => {
  40. newsModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  41. if (err) next(err);
  42. else {
  43. this.log(
  44. "INFO",
  45. `Migration 1 (news). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  46. );
  47. next();
  48. }
  49. });
  50. },
  51. next => {
  52. playlistModel.updateMany(
  53. { documentVersion: null },
  54. { $set: { documentVersion: 1 } },
  55. (err, res) => {
  56. if (err) next(err);
  57. else {
  58. this.log(
  59. "INFO",
  60. `Migration 1 (playlist). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  61. );
  62. next();
  63. }
  64. }
  65. );
  66. },
  67. next => {
  68. punishmentModel.updateMany(
  69. { documentVersion: null },
  70. { $set: { documentVersion: 1 } },
  71. (err, res) => {
  72. if (err) next(err);
  73. else {
  74. this.log(
  75. "INFO",
  76. `Migration 1 (punishment). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  77. );
  78. next();
  79. }
  80. }
  81. );
  82. },
  83. next => {
  84. queueSongModel.updateMany(
  85. { documentVersion: null },
  86. { $set: { documentVersion: 1 } },
  87. (err, res) => {
  88. if (err) next(err);
  89. else {
  90. this.log(
  91. "INFO",
  92. `Migration 1 (queueSong). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  93. );
  94. next();
  95. }
  96. }
  97. );
  98. },
  99. next => {
  100. reportModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  101. if (err) next(err);
  102. else {
  103. this.log(
  104. "INFO",
  105. `Migration 1 (report). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  106. );
  107. next();
  108. }
  109. });
  110. },
  111. next => {
  112. songModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  113. if (err) next(err);
  114. else {
  115. this.log(
  116. "INFO",
  117. `Migration 1 (song). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  118. );
  119. next();
  120. }
  121. });
  122. },
  123. next => {
  124. stationModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  125. if (err) next(err);
  126. else {
  127. this.log(
  128. "INFO",
  129. `Migration 1 (station). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  130. );
  131. next();
  132. }
  133. });
  134. },
  135. next => {
  136. userModel.updateMany({ documentVersion: null }, { $set: { documentVersion: 1 } }, (err, res) => {
  137. if (err) next(err);
  138. else {
  139. this.log(
  140. "INFO",
  141. `Migration 1 (user). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  142. );
  143. next();
  144. }
  145. });
  146. }
  147. ],
  148. (err, response) => {
  149. if (err) {
  150. reject(new Error(err));
  151. } else {
  152. resolve(response);
  153. }
  154. }
  155. );
  156. });
  157. }