database: add autoupgrade command

This commit is contained in:
Hay1tsme 2023-03-18 02:12:58 -04:00
parent 6ff8c4d931
commit 188be2dfc1
3 changed files with 69 additions and 17 deletions

View File

@ -145,25 +145,49 @@ class Data:
) )
return return
if not os.path.exists( if action == "upgrade":
f"core/data/schema/versions/{game.upper()}_{version}_{action}.sql" for x in range(old_ver, version):
): if not os.path.exists(
self.logger.error( f"core/data/schema/versions/{game.upper()}_{x + 1}_{action}.sql"
f"Could not find {action} script {game.upper()}_{version}_{action}.sql in core/data/schema/versions folder" ):
) self.logger.error(
return f"Could not find {action} script {game.upper()}_{x + 1}_{action}.sql in core/data/schema/versions folder"
)
return
with open( with open(
f"core/data/schema/versions/{game.upper()}_{version}_{action}.sql", f"core/data/schema/versions/{game.upper()}_{x + 1}_{action}.sql",
"r", "r",
encoding="utf-8", encoding="utf-8",
) as f: ) as f:
sql = f.read() sql = f.read()
result = self.base.execute(sql) result = self.base.execute(sql)
if result is None: if result is None:
self.logger.error("Error execuing sql script!") self.logger.error("Error execuing sql script!")
return None return None
else:
for x in range(old_ver, version, -1):
if not os.path.exists(
f"core/data/schema/versions/{game.upper()}_{x - 1}_{action}.sql"
):
self.logger.error(
f"Could not find {action} script {game.upper()}_{x - 1}_{action}.sql in core/data/schema/versions folder"
)
return
with open(
f"core/data/schema/versions/{game.upper()}_{x - 1}_{action}.sql",
"r",
encoding="utf-8",
) as f:
sql = f.read()
result = self.base.execute(sql)
if result is None:
self.logger.error("Error execuing sql script!")
return None
result = self.base.set_schema_ver(version, game) result = self.base.set_schema_ver(version, game)
if result is None: if result is None:
@ -237,3 +261,19 @@ class Data:
if not cards: if not cards:
self.logger.info(f"Delete hanging user {user['id']}") self.logger.info(f"Delete hanging user {user['id']}")
self.user.delete_user(user["id"]) self.user.delete_user(user["id"])
def autoupgrade(self) -> None:
all_games = self.base.get_all_schema_vers()
if all_games is None:
self.logger.warn("Failed to get schema versions")
for x in all_games:
game = x["game"].upper()
update_ver = 1
for y in range(2, 100):
if os.path.exists(f"core/data/schema/versions/{game}_{y}_upgrade.sql"):
update_ver = y
else:
break
self.migrate_database(game, update_ver, "upgrade")

View File

@ -2,6 +2,7 @@ import json
import logging import logging
from random import randrange from random import randrange
from typing import Any, Optional, Dict, List from typing import Any, Optional, Dict, List
from sqlalchemy.engine import Row
from sqlalchemy.engine.cursor import CursorResult from sqlalchemy.engine.cursor import CursorResult
from sqlalchemy.engine.base import Connection from sqlalchemy.engine.base import Connection
from sqlalchemy.sql import text, func, select from sqlalchemy.sql import text, func, select
@ -81,6 +82,14 @@ class BaseData:
""" """
return randrange(10000, 9999999) return randrange(10000, 9999999)
def get_all_schema_vers(self) -> Optional[List[Row]]:
sql = select(schema_ver)
result = self.execute(sql)
if result is None:
return None
return result.fetchall()
def get_schema_ver(self, game: str) -> Optional[int]: def get_schema_ver(self, game: str) -> Optional[int]:
sql = select(schema_ver).where(schema_ver.c.game == game) sql = select(schema_ver).where(schema_ver.c.game == game)

View File

@ -64,6 +64,9 @@ if __name__ == "__main__":
else: else:
data.migrate_database(args.game, int(args.version), args.action) data.migrate_database(args.game, int(args.version), args.action)
elif args.action == "autoupgrade":
data.autoupgrade()
elif args.action == "create-owner": elif args.action == "create-owner":
data.create_owner(args.email) data.create_owner(args.email)