Skip to content

Commit

Permalink
fix: add cards on playlists schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane committed Oct 24, 2023
1 parent 270e1d7 commit 104cd2b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
22 changes: 19 additions & 3 deletions src/database/18102023_migrate_formate_storage_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion src/database/19102023_migrate_playlist_favorite_cards_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ 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];

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);
Expand Down
8 changes: 0 additions & 8 deletions src/utils/formatData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 104cd2b

Please sign in to comment.