2023-02-17 07:02:21 +01:00
|
|
|
import yaml
|
2023-02-16 06:06:42 +01:00
|
|
|
import argparse
|
2023-04-15 06:13:04 +02:00
|
|
|
import logging
|
2023-02-17 07:02:21 +01:00
|
|
|
from core.config import CoreConfig
|
|
|
|
from core.data import Data
|
2023-03-17 02:36:42 +01:00
|
|
|
from os import path, mkdir, access, W_OK
|
2023-02-16 06:06:42 +01:00
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
if __name__ == "__main__":
|
2023-02-17 07:02:21 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Database utilities")
|
2023-03-09 17:38:58 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--config", "-c", type=str, help="Config folder to use", default="config"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--version",
|
|
|
|
"-v",
|
|
|
|
type=str,
|
|
|
|
help="Version of the database to upgrade/rollback to",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--game",
|
|
|
|
"-g",
|
|
|
|
type=str,
|
|
|
|
help="Game code of the game who's schema will be updated/rolled back. Ex. SDFE",
|
|
|
|
)
|
2023-03-04 06:04:47 +01:00
|
|
|
parser.add_argument("--email", "-e", type=str, help="Email for the new user")
|
|
|
|
parser.add_argument("--old_ac", "-o", type=str, help="Access code to transfer from")
|
|
|
|
parser.add_argument("--new_ac", "-n", type=str, help="Access code to transfer to")
|
|
|
|
parser.add_argument("--force", "-f", type=bool, help="Force the action to happen")
|
2023-03-09 17:38:58 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"action", type=str, help="DB Action, create, recreate, upgrade, or rollback"
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
cfg = CoreConfig()
|
2023-03-05 03:58:51 +01:00
|
|
|
if path.exists(f"{args.config}/core.yaml"):
|
2023-04-15 09:06:11 +02:00
|
|
|
cfg_dict = yaml.safe_load(open(f"{args.config}/core.yaml"))
|
|
|
|
cfg_dict.get('database', {})['loglevel'] = 'info'
|
|
|
|
cfg.update(cfg_dict)
|
2023-03-17 02:36:42 +01:00
|
|
|
|
|
|
|
if not path.exists(cfg.server.log_dir):
|
|
|
|
mkdir(cfg.server.log_dir)
|
|
|
|
|
|
|
|
if not access(cfg.server.log_dir, W_OK):
|
|
|
|
print(
|
|
|
|
f"Log directory {cfg.server.log_dir} NOT writable, please check permissions"
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
data = Data(cfg)
|
2023-03-17 02:36:42 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
if args.action == "create":
|
|
|
|
data.create_database()
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
elif args.action == "recreate":
|
|
|
|
data.recreate_database()
|
|
|
|
|
|
|
|
elif args.action == "upgrade" or args.action == "rollback":
|
|
|
|
if args.version is None:
|
2023-04-15 06:13:04 +02:00
|
|
|
data.logger.warn("No version set, upgrading to latest")
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
if args.game is None:
|
2023-04-15 06:13:04 +02:00
|
|
|
data.logger.warn("No game set, upgrading core schema")
|
|
|
|
data.migrate_database("CORE", int(args.version) if args.version is not None else None, args.action)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
else:
|
2023-04-15 06:13:04 +02:00
|
|
|
data.migrate_database(args.game, int(args.version) if args.version is not None else None, args.action)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-03-18 07:12:58 +01:00
|
|
|
elif args.action == "autoupgrade":
|
|
|
|
data.autoupgrade()
|
|
|
|
|
2023-03-04 06:04:47 +01:00
|
|
|
elif args.action == "create-owner":
|
|
|
|
data.create_owner(args.email)
|
|
|
|
|
|
|
|
elif args.action == "migrate-card":
|
|
|
|
data.migrate_card(args.old_ac, args.new_ac, args.force)
|
|
|
|
|
|
|
|
elif args.action == "cleanup":
|
|
|
|
data.delete_hanging_users()
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
data.logger.info("Done")
|