From 9c9a18708308966986faa49bd4189395d204dead Mon Sep 17 00:00:00 2001 From: mungai-njoroge Date: Mon, 31 Jul 2023 16:44:37 +0300 Subject: [PATCH] create a base class for all migration + save folder to playlist, sorting by last_mod --- app/api/playlist.py | 3 +++ app/lib/taglib.py | 1 - app/migrations/__init__.py | 14 ++++++------- app/migrations/base.py | 16 +++++++++++++++ app/migrations/v1_3_0/__init__.py | 34 ++++++++++++++++++++----------- 5 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 app/migrations/base.py diff --git a/app/api/playlist.py b/app/api/playlist.py index 957a0477..a6042502 100644 --- a/app/api/playlist.py +++ b/app/api/playlist.py @@ -348,6 +348,9 @@ def save_folder_as_folder(): return {"error": "Playlist already exists"}, 409 tracks = TrackStore.get_tracks_in_path(path) + + # sort tracks by last_mod + tracks = sorted(tracks, key=lambda t: t.last_mod) trackhashes = [t.trackhash for t in tracks] if len(trackhashes) == 0: diff --git a/app/lib/taglib.py b/app/lib/taglib.py index 7e0e9023..099fe810 100644 --- a/app/lib/taglib.py +++ b/app/lib/taglib.py @@ -1,4 +1,3 @@ -import datetime import os from io import BytesIO import pendulum diff --git a/app/migrations/__init__.py b/app/migrations/__init__.py index e91b00ef..61b61c56 100644 --- a/app/migrations/__init__.py +++ b/app/migrations/__init__.py @@ -14,8 +14,9 @@ PS: Fuck that! Do what you want. from app.db.sqlite.migrations import MigrationManager from app.logger import log from app.migrations import v1_3_0 +from app.migrations.base import Migration -migrations = [ +migrations: list[list[Migration]] = [ [ # v1.3.0 v1_3_0.RemovePlaylistArtistHashes, @@ -34,17 +35,16 @@ def apply_migrations(): version = MigrationManager.get_version() - # is clean install - if version == -1: - MigrationManager.set_version(len(migrations)) - return - if version != len(migrations): # run migrations after the previous migration version for migration in migrations[(version - 1) :]: for m in migration: log.info("Running new migration: %s", m.name) - m.migrate() + + try: + m.migrate() + except: + log.error("Failed to run migration: %s", m.name) # bump migration version MigrationManager.set_version(len(migrations)) diff --git a/app/migrations/base.py b/app/migrations/base.py new file mode 100644 index 00000000..30879c21 --- /dev/null +++ b/app/migrations/base.py @@ -0,0 +1,16 @@ +class Migration: + """ + Base migration class. + """ + + name: str + """ + Name of the migration. + """ + + @staticmethod + def migrate(): + """ + Code to run when migrating + """ + pass diff --git a/app/migrations/v1_3_0/__init__.py b/app/migrations/v1_3_0/__init__.py index 6776e72d..547911e0 100644 --- a/app/migrations/v1_3_0/__init__.py +++ b/app/migrations/v1_3_0/__init__.py @@ -1,9 +1,11 @@ import json +from sqlite3 import OperationalError import time from collections import OrderedDict from typing import Generator from app.db.sqlite.utils import SQLiteManager +from app.migrations.base import Migration from app.utils.decorators import coroutine from app.utils.hashing import create_hash @@ -18,9 +20,9 @@ from app.utils.hashing import create_hash # 6: trackhashes -class RemovePlaylistArtistHashes: +class RemovePlaylistArtistHashes(Migration): """ - This migration removes the artisthashes column from the playlists table. + removes the artisthashes column from the playlists table. """ name = "RemovePlaylistArtistHashes" @@ -31,13 +33,17 @@ class RemovePlaylistArtistHashes: sql = "ALTER TABLE playlists DROP COLUMN artisthashes" with SQLiteManager(userdata_db=True) as cur: - cur.execute(sql) + try: + cur.execute(sql) + except OperationalError: + pass + cur.close() -class AddSettingsToPlaylistTable: +class AddSettingsToPlaylistTable(Migration): """ - This migration adds the settings column and removes the banner_pos and has_gif columns + adds the settings column and removes the banner_pos and has_gif columns to the playlists table. """ @@ -115,9 +121,9 @@ class AddSettingsToPlaylistTable: cur.close() -class AddLastUpdatedToTrackTable: +class AddLastUpdatedToTrackTable(Migration): """ - This migration adds the last modified column to the tracks table. + adds the last modified column to the tracks table. """ name = "AddLastUpdatedToTrackTable" @@ -129,13 +135,17 @@ class AddLastUpdatedToTrackTable: sql = f"ALTER TABLE tracks ADD COLUMN last_mod text not null DEFAULT '{timestamp}'" with SQLiteManager() as cur: - cur.execute(sql) + try: + cur.execute(sql) + except OperationalError: + pass + cur.close() -class MovePlaylistsAndFavoritesTo10BitHashes: +class MovePlaylistsAndFavoritesTo10BitHashes(Migration): """ - This migration moves the playlists and favorites to 10 bit hashes. + moves the playlists and favorites to 10 bit hashes. """ name = "MovePlaylistsAndFavoritesTo10BitHashes" @@ -242,9 +252,9 @@ class MovePlaylistsAndFavoritesTo10BitHashes: cur.close() -class RemoveAllTracks: +class RemoveAllTracks(Migration): """ - This migration removes all tracks from the tracks table. + removes all tracks from the tracks table. """ name = "RemoveAllTracks"