From d6cf10ccaf8391f0186415f46103bc39d32b8b38 Mon Sep 17 00:00:00 2001 From: voldemort <5692900+yell0wsuit@users.noreply.github.com> Date: Wed, 23 Jul 2025 01:48:14 +0700 Subject: [PATCH] Refactor database checks to enhance path handling, add module docstrings for clarity, and streamline database creation logic --- custom_functions/prechecks/database_checks.py | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/custom_functions/prechecks/database_checks.py b/custom_functions/prechecks/database_checks.py index 6827d46..76b17a4 100644 --- a/custom_functions/prechecks/database_checks.py +++ b/custom_functions/prechecks/database_checks.py @@ -1,44 +1,52 @@ +"""Module to check for the database.""" + import os import yaml +from custom_functions.database.cache_to_db_mariadb import ( + create_database as create_mariadb_database, +) +from custom_functions.database.cache_to_db_sqlite import ( + create_database as create_sqlite_database, +) +from custom_functions.database.user_db import create_user_database + def check_for_sqlite_database(): - with open(f"{os.getcwd()}/configs/config.yaml", "r") as file: + """Check for the SQLite database.""" + with open( + os.path.join(os.getcwd(), "configs", "config.yaml"), "r", encoding="utf-8" + ) as file: config = yaml.safe_load(file) - if os.path.exists(f"{os.getcwd()}/databases/key_cache.db"): + if os.path.exists(os.path.join(os.getcwd(), "databases", "key_cache.db")): return - else: - if config["database_type"].lower() != "mariadb": - from custom_functions.database.cache_to_db_sqlite import create_database - - create_database() - return - else: - return + if config["database_type"].lower() == "sqlite": + create_sqlite_database() + return + return def check_for_user_database(): - if os.path.exists(f"{os.getcwd()}/databases/users.db"): + """Check for the user database.""" + if os.path.exists(os.path.join(os.getcwd(), "databases", "users.db")): return - else: - from custom_functions.database.user_db import create_user_database - - create_user_database() + create_user_database() def check_for_mariadb_database(): - with open(f"{os.getcwd()}/configs/config.yaml", "r") as file: + """Check for the MariaDB database.""" + with open( + os.path.join(os.getcwd(), "configs", "config.yaml"), "r", encoding="utf-8" + ) as file: config = yaml.safe_load(file) if config["database_type"].lower() == "mariadb": - from custom_functions.database.cache_to_db_mariadb import create_database - - create_database() - return - else: + create_mariadb_database() return + return def check_for_sql_database(): + """Check for the SQL database.""" check_for_sqlite_database() check_for_mariadb_database() check_for_user_database()