mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 04:23:01 +00:00
feat: add migration to move old files to xdg directory
+ add db column for migration version + handle pre-init migrations + handle post-init migration
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"""
|
||||
Reads and saves the latest database migrations version.
|
||||
"""
|
||||
|
||||
|
||||
from app.db.sqlite.utils import SQLiteManager
|
||||
|
||||
|
||||
class MigrationManager:
|
||||
all_get_sql = "SELECT * FROM migrations"
|
||||
pre_init_set_sql = "UPDATE migrations SET pre_init_version = ? WHERE id = 1"
|
||||
post_init_set_sql = "UPDATE migrations SET post_init_version = ? WHERE id = 1"
|
||||
|
||||
@classmethod
|
||||
def get_preinit_version(cls) -> int:
|
||||
"""
|
||||
Returns the latest userdata pre-init database version.
|
||||
"""
|
||||
with SQLiteManager() as cur:
|
||||
cur.execute(cls.all_get_sql)
|
||||
return int(cur.fetchone()[1])
|
||||
|
||||
@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)
|
||||
return int(cur.fetchone()[2])
|
||||
|
||||
@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)
|
||||
return cur.fetchone()[2]
|
||||
|
||||
# 👇 Setters 👇
|
||||
@classmethod
|
||||
def set_preinit_version(cls, version: int):
|
||||
"""
|
||||
Sets the userdata pre-init database version.
|
||||
"""
|
||||
with SQLiteManager() as cur:
|
||||
cur.execute(cls.pre_init_set_sql, (version,))
|
||||
|
||||
@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,))
|
||||
@@ -69,3 +69,13 @@ CREATE TABLE IF NOT EXISTS folders (
|
||||
trackcount integer NOT NULL
|
||||
);
|
||||
"""
|
||||
|
||||
CREATE_MIGRATIONS_TABLE = """
|
||||
CREATE TABLE IF NOT EXISTS migrations (
|
||||
id integer PRIMARY KEY,
|
||||
pre_init_version integer NOT NULL DEFAULT 0,
|
||||
post_init_version integer NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
INSERT INTO migrations (pre_init_version, post_init_version) VALUES (0, 0);
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user