forked from Hay1tsme/artemis
database: add autoupgrade command
This commit is contained in:
parent
6ff8c4d931
commit
188be2dfc1
@ -145,16 +145,40 @@ class Data:
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if action == "upgrade":
|
||||||
|
for x in range(old_ver, version):
|
||||||
if not os.path.exists(
|
if not os.path.exists(
|
||||||
f"core/data/schema/versions/{game.upper()}_{version}_{action}.sql"
|
f"core/data/schema/versions/{game.upper()}_{x + 1}_{action}.sql"
|
||||||
):
|
):
|
||||||
self.logger.error(
|
self.logger.error(
|
||||||
f"Could not find {action} script {game.upper()}_{version}_{action}.sql in core/data/schema/versions folder"
|
f"Could not find {action} script {game.upper()}_{x + 1}_{action}.sql in core/data/schema/versions folder"
|
||||||
)
|
)
|
||||||
return
|
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",
|
||||||
|
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
|
||||||
|
|
||||||
|
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",
|
"r",
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
) as f:
|
) as f:
|
||||||
@ -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")
|
@ -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)
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user