create a base class for all migration

+ save folder to playlist, sorting by last_mod
This commit is contained in:
mungai-njoroge 2023-07-31 16:44:37 +03:00
parent 782cfc8da8
commit 9c9a187083
5 changed files with 48 additions and 20 deletions

View File

@ -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:

View File

@ -1,4 +1,3 @@
import datetime
import os
from io import BytesIO
import pendulum

View File

@ -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))

16
app/migrations/base.py Normal file
View File

@ -0,0 +1,16 @@
class Migration:
"""
Base migration class.
"""
name: str
"""
Name of the migration.
"""
@staticmethod
def migrate():
"""
Code to run when migrating
"""
pass

View File

@ -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"