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
+3
View File
@@ -348,6 +348,9 @@ def save_folder_as_folder():
return {"error": "Playlist already exists"}, 409 return {"error": "Playlist already exists"}, 409
tracks = TrackStore.get_tracks_in_path(path) 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] trackhashes = [t.trackhash for t in tracks]
if len(trackhashes) == 0: if len(trackhashes) == 0:
-1
View File
@@ -1,4 +1,3 @@
import datetime
import os import os
from io import BytesIO from io import BytesIO
import pendulum import pendulum
+6 -6
View File
@@ -14,8 +14,9 @@ PS: Fuck that! Do what you want.
from app.db.sqlite.migrations import MigrationManager from app.db.sqlite.migrations import MigrationManager
from app.logger import log from app.logger import log
from app.migrations import v1_3_0 from app.migrations import v1_3_0
from app.migrations.base import Migration
migrations = [ migrations: list[list[Migration]] = [
[ [
# v1.3.0 # v1.3.0
v1_3_0.RemovePlaylistArtistHashes, v1_3_0.RemovePlaylistArtistHashes,
@@ -34,17 +35,16 @@ def apply_migrations():
version = MigrationManager.get_version() version = MigrationManager.get_version()
# is clean install
if version == -1:
MigrationManager.set_version(len(migrations))
return
if version != len(migrations): if version != len(migrations):
# run migrations after the previous migration version # run migrations after the previous migration version
for migration in migrations[(version - 1) :]: for migration in migrations[(version - 1) :]:
for m in migration: for m in migration:
log.info("Running new migration: %s", m.name) log.info("Running new migration: %s", m.name)
try:
m.migrate() m.migrate()
except:
log.error("Failed to run migration: %s", m.name)
# bump migration version # bump migration version
MigrationManager.set_version(len(migrations)) MigrationManager.set_version(len(migrations))
+16
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
+20 -10
View File
@@ -1,9 +1,11 @@
import json import json
from sqlite3 import OperationalError
import time import time
from collections import OrderedDict from collections import OrderedDict
from typing import Generator from typing import Generator
from app.db.sqlite.utils import SQLiteManager from app.db.sqlite.utils import SQLiteManager
from app.migrations.base import Migration
from app.utils.decorators import coroutine from app.utils.decorators import coroutine
from app.utils.hashing import create_hash from app.utils.hashing import create_hash
@@ -18,9 +20,9 @@ from app.utils.hashing import create_hash
# 6: trackhashes # 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" name = "RemovePlaylistArtistHashes"
@@ -31,13 +33,17 @@ class RemovePlaylistArtistHashes:
sql = "ALTER TABLE playlists DROP COLUMN artisthashes" sql = "ALTER TABLE playlists DROP COLUMN artisthashes"
with SQLiteManager(userdata_db=True) as cur: with SQLiteManager(userdata_db=True) as cur:
try:
cur.execute(sql) cur.execute(sql)
except OperationalError:
pass
cur.close() 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. to the playlists table.
""" """
@@ -115,9 +121,9 @@ class AddSettingsToPlaylistTable:
cur.close() 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" name = "AddLastUpdatedToTrackTable"
@@ -129,13 +135,17 @@ class AddLastUpdatedToTrackTable:
sql = f"ALTER TABLE tracks ADD COLUMN last_mod text not null DEFAULT '{timestamp}'" sql = f"ALTER TABLE tracks ADD COLUMN last_mod text not null DEFAULT '{timestamp}'"
with SQLiteManager() as cur: with SQLiteManager() as cur:
try:
cur.execute(sql) cur.execute(sql)
except OperationalError:
pass
cur.close() 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" name = "MovePlaylistsAndFavoritesTo10BitHashes"
@@ -242,9 +252,9 @@ class MovePlaylistsAndFavoritesTo10BitHashes:
cur.close() 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" name = "RemoveAllTracks"