From 104cd2bb085a5eb2e470a8f7f729a2a85f7bc1a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane?= Date: Tue, 24 Oct 2023 11:09:06 +0200 Subject: [PATCH] fix: add cards on playlists schema --- .../18102023_migrate_formate_storage_data.ts | 22 ++++++++++++++++--- ...023_migrate_playlist_favorite_cards_key.ts | 10 ++++++++- src/utils/formatData.ts | 8 ------- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/database/18102023_migrate_formate_storage_data.ts b/src/database/18102023_migrate_formate_storage_data.ts index c8cc263..e18ec1e 100644 --- a/src/database/18102023_migrate_formate_storage_data.ts +++ b/src/database/18102023_migrate_formate_storage_data.ts @@ -9,9 +9,15 @@ import { } from "../utils/formatData"; const formatedPlaylists = () => { + if (!db.columnExists("playlists", "cards")) { + db.alterTable("history", "thumbnail", null); + db.commit(); + } + const playlists = db.queryAll("playlists") as Playlist[]; + const history = db.queryAll("history") as CardVideo[]; - return playlists.map((p) => { + const updatedPlaylists = playlists.map((p) => { if ((p as FavoritePlaylist).title === "Favorites") { return { ...p, @@ -38,17 +44,27 @@ const formatedPlaylists = () => { videos: formatedVideos, }; }); + + const updatedHistory = history.map((v) => formatedCardVideo(v)); + + return { + playlists: updatedPlaylists, + history: updatedHistory, + }; }; const migration = () => { try { - const playlists = formatedPlaylists(); - console.log(playlists); + const { playlists, history } = formatedPlaylists(); for (const playlist of playlists) { db.update("playlists", { title: playlist.title }, () => playlist); } + for (const video of history) { + db.update("history", () => video); + } + db.commit(); } catch (error) { console.log(error); diff --git a/src/database/19102023_migrate_playlist_favorite_cards_key.ts b/src/database/19102023_migrate_playlist_favorite_cards_key.ts index 333e79f..f735e2f 100644 --- a/src/database/19102023_migrate_playlist_favorite_cards_key.ts +++ b/src/database/19102023_migrate_playlist_favorite_cards_key.ts @@ -2,6 +2,11 @@ import { db } from "."; const migration = () => { try { + if (!db.columnExists("playlists", "cards")) { + db.alterTable("playlists", "cards", []); + db.commit(); + } + const favoritePlaylist = db.queryAll("playlists", { query: { title: "Favorites" }, })[0]; @@ -9,7 +14,10 @@ const migration = () => { favoritePlaylist.cards = favoritePlaylist.videos; delete favoritePlaylist.videos; - db.update("playlists", { title: "Favorites" }, () => favoritePlaylist); + db.update("playlists", { title: "Favorites" }, () => ({ + ...favoritePlaylist, + videos: [], + })); db.commit(); } catch (error) { console.log(error); diff --git a/src/utils/formatData.ts b/src/utils/formatData.ts index b57dffc..af7fd55 100644 --- a/src/utils/formatData.ts +++ b/src/utils/formatData.ts @@ -59,10 +59,6 @@ export const formatedCardPlaylist = ( export const formatedCardChannel = ( channel: CardChannel | Channel, ): CardChannel => { - if (channel.thumbnail) { - return channel; - } - return { type: channel.type, author: channel.author, @@ -80,10 +76,6 @@ export const formatedCardChannel = ( }; export const formatedCardVideo = (video: CardVideo | Video): CardVideo => { - if (video.thumbnail) { - return video as CardVideo; - } - return { videoId: video.videoId, title: video.title,