rewrite migrations

+ delete older migrations ... oops
+ change migratrions from "migrations" to "dbmigrations"
+ restructure migrations, order them based on release version
+ add a utils/decorators.py file with a coroutine decorator
This commit is contained in:
mungai-njoroge
2023-07-29 06:46:28 +03:00
parent a0c51d5f82
commit 93de3d2f0c
19 changed files with 327 additions and 288 deletions
+9 -55
View File
@@ -6,72 +6,26 @@ from app.db.sqlite.utils import SQLiteManager
class MigrationManager:
all_get_sql = "SELECT * FROM migrations"
_base = "UPDATE migrations SET"
_end = "= ? WHERE id = 1"
pre_init_set_sql = f"{_base} pre_init_version {_end}"
post_init_set_sql = f"{_base} post_init_version {_end}"
@classmethod
def get_preinit_version(cls) -> int:
@staticmethod
def get_version() -> int:
"""
Returns the latest userdata pre-init database version.
Returns the latest userdata database version.
"""
sql = "SELECT * FROM dbmigrations"
with SQLiteManager() as cur:
cur.execute(cls.all_get_sql)
cur.execute(sql)
ver = int(cur.fetchone()[1])
cur.close()
return ver
@classmethod
def get_maindb_postinit_version(cls) -> int:
"""
Returns the latest maindb post-init database version.
"""
with SQLiteManager() as cur:
cur.execute(cls.all_get_sql)
ver = int(cur.fetchone()[2])
cur.close()
return ver
@classmethod
def get_userdatadb_postinit_version(cls) -> int:
"""
Returns the latest userdata post-init database version.
"""
with SQLiteManager(userdata_db=True) as cur:
cur.execute(cls.all_get_sql)
ver = cur.fetchone()[2]
cur.close()
return ver
# 👇 Setters 👇
@classmethod
def set_preinit_version(cls, version: int):
@staticmethod
def set_version(version: int):
"""
Sets the userdata pre-init database version.
"""
sql = "UPDATE dbmigrations SET version = ? WHERE id = 1"
with SQLiteManager() as cur:
cur.execute(cls.pre_init_set_sql, (version,))
cur.execute(sql, (version,))
cur.close()
@classmethod
def set_maindb_postinit_version(cls, version: int):
"""
Sets the maindb post-init database version.
"""
with SQLiteManager() as cur:
cur.execute(cls.post_init_set_sql, (version,))
@classmethod
def set_userdatadb_postinit_version(cls, version: int):
"""
Sets the userdata post-init database version.
"""
with SQLiteManager(userdata_db=True) as cur:
cur.execute(cls.post_init_set_sql, (version,))
+1 -24
View File
@@ -18,13 +18,12 @@ class SQLitePlaylistMethods:
# banner_pos,
# has_gif,
sql = """INSERT INTO playlists(
artisthashes,
image,
last_updated,
name,
settings,
trackhashes
) VALUES(:artisthashes, :image, :last_updated, :name, :settings, :trackhashes)
) VALUES(:image, :last_updated, :name, :settings, :trackhashes)
"""
playlist = OrderedDict(sorted(playlist.items()))
@@ -124,26 +123,6 @@ class SQLitePlaylistMethods:
def add_tracks_to_playlist(cls, playlist_id: int, trackhashes: list[str]):
return cls.add_item_to_json_list(playlist_id, "trackhashes", trackhashes)
@classmethod
@background
def add_artists_to_playlist(
cls,
playlist_id: int,
trackhashes: list[str] = [],
artisthashes: set[str] = None,
):
if not artisthashes:
track = [SQLiteTrackMethods.get_track_by_trackhash(t) for t in trackhashes]
tracks = [t for t in track if t is not None]
artisthashes: set[str] = set() # type: ignore
for track in tracks:
for a in track.artist:
artisthashes.add(a.artisthash)
cls.add_item_to_json_list(playlist_id, "artisthashes", artisthashes)
@staticmethod
def update_playlist(playlist_id: int, playlist: dict):
sql = """UPDATE playlists SET
@@ -156,10 +135,8 @@ class SQLitePlaylistMethods:
del playlist["id"]
del playlist["trackhashes"]
del playlist["artisthashes"]
playlist["settings"] = json.dumps(playlist["settings"])
playlist = OrderedDict(sorted(playlist.items()))
params = (*playlist.values(), playlist_id)
+10 -8
View File
@@ -5,10 +5,10 @@ This file contains the SQL queries to create the database tables.
# banner_pos integer NOT NULL,
# has_gif integer,
CREATE_USERDATA_TABLES = """
CREATE TABLE IF NOT EXISTS playlists (
id integer PRIMARY KEY,
artisthashes text,
image text,
last_updated text not null,
name text not null,
@@ -52,10 +52,10 @@ CREATE TABLE IF NOT EXISTS tracks (
filepath text NOT NULL,
folder text NOT NULL,
genre text,
last_mod float NOT NULL,
title text NOT NULL,
track integer NOT NULL,
trackhash text NOT NULL,
last_mod float NOT NULL,
UNIQUE (filepath)
);
@@ -81,14 +81,16 @@ CREATE TABLE IF NOT EXISTS folders (
);
"""
# changed from migrations to dbmigrations in v1.3.0
# to avoid conflicts with the previous migrations.
CREATE_MIGRATIONS_TABLE = """
CREATE TABLE IF NOT EXISTS migrations (
CREATE TABLE IF NOT EXISTS dbmigrations (
id integer PRIMARY KEY,
pre_init_version integer NOT NULL DEFAULT 0,
post_init_version integer NOT NULL DEFAULT 0
version integer NOT NULL DEFAULT 0
);
INSERT INTO migrations (pre_init_version, post_init_version)
SELECT 0, 0
WHERE NOT EXISTS (SELECT 1 FROM migrations);
INSERT INTO dbmigrations (version)
SELECT 0
WHERE NOT EXISTS (SELECT 1 FROM dbmigrations);
"""
+2
View File
@@ -2,6 +2,7 @@
Helper functions for use with the SQLite database.
"""
from pprint import pprint
import sqlite3
from sqlite3 import Connection, Cursor
import time
@@ -45,6 +46,7 @@ def tuple_to_playlist(playlist: tuple):
"""
Takes a tuple and returns a Playlist object
"""
pprint(playlist)
return Playlist(*playlist)