artemis/dbutils.py

91 lines
2.8 KiB
Python
Raw Normal View History

2024-03-22 16:22:07 +00:00
#!/usr/bin/env python3
import argparse
2024-03-22 16:22:07 +00:00
import asyncio
import logging
2024-03-22 16:22:07 +00:00
from os import W_OK, access, mkdir, path
import yaml
from core.config import CoreConfig
from core.data import Data
2023-03-09 16:38:58 +00:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Database utilities")
2023-03-09 16:38:58 +00: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",
)
2024-03-22 16:22:07 +00:00
parser.add_argument("--email", "-e", type=str, help="Email for the new user")
2023-03-09 16:38:58 +00:00
parser.add_argument(
2024-03-22 16:22:07 +00:00
"--access_code",
"-a",
2023-03-09 16:38:58 +00:00
type=str,
2024-03-22 16:22:07 +00:00
help="Access code for new/transfer user",
default="00000000000000000000",
2023-03-09 16:38:58 +00:00
)
2024-03-22 16:22:07 +00:00
parser.add_argument("--message", "-m", type=str, help="Revision message")
<<<<<<< Updated upstream
parser.add_argument("action", type=str, help="create, upgrade, downgrade, create-owner, migrate, create-revision, create-autorevision")
=======
2023-03-09 16:38:58 +00:00
parser.add_argument(
2024-03-22 16:22:07 +00:00
"action",
type=str,
help="create, upgrade, create-owner, migrate, create-revision",
2023-03-09 16:38:58 +00:00
)
>>>>>>> Stashed changes
args = parser.parse_args()
cfg = CoreConfig()
if path.exists(f"{args.config}/core.yaml"):
cfg_dict = yaml.safe_load(open(f"{args.config}/core.yaml"))
cfg_dict.get("database", {})["loglevel"] = "info"
cfg.update(cfg_dict)
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)
data = Data(cfg)
if args.action == "create":
data.create_database()
2023-03-09 16:38:58 +00:00
2024-03-22 16:22:07 +00:00
elif args.action == "upgrade":
data.schema_upgrade(args.version)
2024-03-22 16:22:07 +00:00
elif args.action == "downgrade":
if not args.version:
logging.getLogger("database").error(
f"Version argument required for downgrade"
)
2024-03-22 16:22:07 +00:00
exit(1)
data.schema_downgrade(args.version)
2023-03-18 06:12:58 +00:00
elif args.action == "create-owner":
2024-03-22 16:22:07 +00:00
loop = asyncio.get_event_loop()
loop.run_until_complete(data.create_owner(args.email, args.access_code))
elif args.action == "migrate":
loop = asyncio.get_event_loop()
loop.run_until_complete(data.migrate())
2024-03-22 16:22:07 +00:00
elif args.action == "create-revision":
loop = asyncio.get_event_loop()
loop.run_until_complete(data.create_revision(args.message))
2024-03-22 16:22:07 +00:00
elif args.action == "create-autorevision":
loop = asyncio.get_event_loop()
loop.run_until_complete(data.create_revision_auto(args.message))
2024-03-22 16:22:07 +00:00
else:
logging.getLogger("database").info(f"Unknown action {args.action}")