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:
geoffrey45
2023-02-12 03:22:21 +03:00
parent 97e29c3254
commit b77b1747f1
10 changed files with 215 additions and 19 deletions
+64
View File
@@ -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,))