Browse Source

News db migration

Owen Diffey 3 years ago
parent
commit
c73e1a68b8

+ 6 - 6
backend/logic/actions/songs.js

@@ -499,8 +499,8 @@ export default {
 								.filter((value, index, self) => self.indexOf(value) === index)
 								.forEach(genre => {
 									PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre })
-										.then(() => { })
-										.catch(() => { });
+										.then(() => {})
+										.catch(() => {});
 								});
 
 							next(null, song);
@@ -776,8 +776,8 @@ export default {
 				(song, next) => {
 					song.genres.forEach(genre => {
 						PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre })
-							.then(() => { })
-							.catch(() => { });
+							.then(() => {})
+							.catch(() => {});
 					});
 
 					SongsModule.runJob("UPDATE_SONG", { songId: song._id });
@@ -844,8 +844,8 @@ export default {
 				(song, next) => {
 					song.genres.forEach(genre => {
 						PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre })
-							.then(() => { })
-							.catch(() => { });
+							.then(() => {})
+							.catch(() => {});
 					});
 
 					SongsModule.runJob("UPDATE_SONG", { songId });

+ 86 - 0
backend/logic/migration/migrations/migration9.js

@@ -0,0 +1,86 @@
+import async from "async";
+
+/**
+ * Migration 9
+ *
+ * Migration for news
+ *
+ * @param {object} MigrationModule - the MigrationModule
+ * @returns {Promise} - returns promise
+ */
+export default async function migrate(MigrationModule) {
+	const newsModel = await MigrationModule.runJob("GET_MODEL", { modelName: "news" }, this);
+
+	return new Promise((resolve, reject) => {
+		async.waterfall(
+			[
+				next => {
+					// return next("BLOCKED");
+					this.log("INFO", `Migration 9. Finding news with document version 1.`);
+					newsModel.find({ documentVersion: 1 }, (err, news) => {
+						if (err) next(err);
+						else {
+							async.eachLimit(
+								news.map(newi => newi._doc),
+								1,
+								(newi, next) => {
+									newi.markdown = `# ${newi.title}\n\n`;
+									newi.markdown += `## ${newi.description}\n\n`;
+
+									if (newi.bugs) {
+										newi.markdown += `**Bugs:**\n\n${newi.bugs.join(", ")}\n\n`;
+									}
+
+									if (newi.features) {
+										newi.markdown += `**Features:**\n\n${newi.features.join(", ")}\n\n`;
+									}
+
+									if (newi.improvements) {
+										newi.markdown += `**Improvements:**\n\n${newi.improvements.join(", ")}\n\n`;
+									}
+
+									if (newi.upcoming) {
+										newi.markdown += `**Upcoming:**\n\n${newi.upcoming.join(", ")}\n`;
+									}
+
+									newsModel.updateOne(
+										{ _id: newi._id },
+										{
+											$set: {
+												markdown: newi.markdown,
+												status: "published",
+												documentVersion: 2
+											},
+											$unset: {
+												description: "",
+												bugs: "",
+												features: "",
+												improvements: "",
+												upcoming: ""
+											}
+										},
+										next
+									);
+								},
+								err => {
+									if (err) next(err);
+									else {
+										this.log("INFO", `Migration 9. News found: ${news.length}.`);
+										next();
+									}
+								}
+							);
+						}
+					});
+				}
+			],
+			err => {
+				if (err) {
+					reject(new Error(err));
+				} else {
+					resolve();
+				}
+			}
+		);
+	});
+}

+ 17 - 10
backend/logic/playlists.js

@@ -556,16 +556,23 @@ class _PlaylistsModule extends CoreClass {
 					(playlistId, next) => {
 						StationsModule.runJob("GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST", { playlistId }, this)
 							.then(response => {
-								async.eachLimit(response.stationIds, 1, (stationId, next) => {
-									PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this).then(() => {
-										next();
-									}).catch(err => {
-										next(err);
-									});
-								}, err => {
-									if (err) next(err);
-									else next();
-								});
+								async.eachLimit(
+									response.stationIds,
+									1,
+									(stationId, next) => {
+										PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this)
+											.then(() => {
+												next();
+											})
+											.catch(err => {
+												next(err);
+											});
+									},
+									err => {
+										if (err) next(err);
+										else next();
+									}
+								);
 							})
 							.catch(err => {
 								next(err);

+ 96 - 70
backend/logic/songs.js

@@ -203,7 +203,7 @@ class _SongsModule extends CoreClass {
 						} else {
 							const status =
 								(!payload.userId && config.get("hideAnonymousSongs")) ||
-									(payload.automaticallyRequested && config.get("hideAutomaticallyRequestedSongs"))
+								(payload.automaticallyRequested && config.get("hideAutomaticallyRequestedSongs"))
 									? "hidden"
 									: "unverified";
 
@@ -302,40 +302,53 @@ class _SongsModule extends CoreClass {
 							status
 						};
 						this.log("INFO", `Going to update playlists now for song ${_id}`);
-						DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel => {
-							playlistModel.updateMany(
-								{ "songs._id": song._id },
-								{ $set: { "songs.$": trimmedSong } },
-								err => {
-									if (err) next(err);
-									else
-										playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
-											if (err) next(err);
-											else {
-												async.eachLimit(playlists, 1, (playlist, next) => {
-													PlaylistsModule.runJob("UPDATE_PLAYLIST", {
-														playlistId: playlist._id
-													}, this).then(() => {
-														next();
-													}).catch(err => {
-														next(err);
-													});
-												}, err => {
-													if (err) next(err);
-													else next(null, song)
-												});
-											}
-											// playlists.forEach(playlist => {
-											// 	PlaylistsModule.runJob("UPDATE_PLAYLIST", {
-											// 		playlistId: playlist._id
-											// 	});
-											// });
-										});
-								}
-							);
-						}).catch(err => {
-							next(err);
-						});
+						DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
+							.then(playlistModel => {
+								playlistModel.updateMany(
+									{ "songs._id": song._id },
+									{ $set: { "songs.$": trimmedSong } },
+									err => {
+										if (err) next(err);
+										else
+											playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
+												if (err) next(err);
+												else {
+													async.eachLimit(
+														playlists,
+														1,
+														(playlist, next) => {
+															PlaylistsModule.runJob(
+																"UPDATE_PLAYLIST",
+																{
+																	playlistId: playlist._id
+																},
+																this
+															)
+																.then(() => {
+																	next();
+																})
+																.catch(err => {
+																	next(err);
+																});
+														},
+														err => {
+															if (err) next(err);
+															else next(null, song);
+														}
+													);
+												}
+												// playlists.forEach(playlist => {
+												// 	PlaylistsModule.runJob("UPDATE_PLAYLIST", {
+												// 		playlistId: playlist._id
+												// 	});
+												// });
+											});
+									}
+								);
+							})
+							.catch(err => {
+								next(err);
+							});
 					},
 
 					(song, next) => {
@@ -369,42 +382,55 @@ class _SongsModule extends CoreClass {
 						// 	);
 						// });
 						this.log("INFO", `Going to update stations now for song ${_id}`);
-						DBModule.runJob("GET_MODEL", { modelName: "station" }, this).then(stationModel => {
-							stationModel.updateMany(
-								{ "queue._id": song._id },
-								{
-									$set: {
-										"queue.$.youtubeId": youtubeId,
-										"queue.$.title": title,
-										"queue.$.artists": artists,
-										"queue.$.thumbnail": thumbnail,
-										"queue.$.duration": duration,
-										"queue.$.status": status
+						DBModule.runJob("GET_MODEL", { modelName: "station" }, this)
+							.then(stationModel => {
+								stationModel.updateMany(
+									{ "queue._id": song._id },
+									{
+										$set: {
+											"queue.$.youtubeId": youtubeId,
+											"queue.$.title": title,
+											"queue.$.artists": artists,
+											"queue.$.thumbnail": thumbnail,
+											"queue.$.duration": duration,
+											"queue.$.status": status
+										}
+									},
+									err => {
+										if (err) this.log("ERROR", err);
+										else
+											stationModel.find({ "queue._id": song._id }, (err, stations) => {
+												if (err) next(err);
+												else {
+													async.eachLimit(
+														stations,
+														1,
+														(station, next) => {
+															StationsModule.runJob(
+																"UPDATE_STATION",
+																{ stationId: station._id },
+																this
+															)
+																.then(() => {
+																	next();
+																})
+																.catch(err => {
+																	next(err);
+																});
+														},
+														err => {
+															if (err) next(err);
+															else next(null, song);
+														}
+													);
+												}
+											});
 									}
-								},
-								err => {
-									if (err) this.log("ERROR", err);
-									else
-										stationModel.find({ "queue._id": song._id }, (err, stations) => {
-											if (err) next(err);
-											else {
-												async.eachLimit(stations, 1, (station, next) => {
-													StationsModule.runJob("UPDATE_STATION", { stationId: station._id }, this).then(() => {
-														next();
-													}).catch(err => {
-														next(err);
-													});
-												}, err => {
-													if (err) next(err);
-													else next(null, song);
-												});
-											}
-										});
-								}
-							);
-						}).catch(err => {
-							next(err);
-						});
+								);
+							})
+							.catch(err => {
+								next(err);
+							});
 					},
 
 					(song, next) => {