mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-10-04 00:06:18 +00:00
feat: make the prefix configurable
This commit is contained in:
@@ -1,25 +1,44 @@
|
||||
# ConnectionString.py
|
||||
import configparser
|
||||
import os
|
||||
from sqlalchemy_utils import database_exists, create_database
|
||||
from flask import current_app
|
||||
|
||||
def ConnectionString(database) -> str:
|
||||
parser = configparser.ConfigParser(strict=False)
|
||||
parser.read_file(open('wg-dashboard.ini', "r+"))
|
||||
sqlitePath = os.path.join("db")
|
||||
if not os.path.isdir(sqlitePath):
|
||||
os.mkdir(sqlitePath)
|
||||
if parser.get("Database", "type") == "postgresql":
|
||||
cn = f'postgresql+psycopg://{parser.get("Database", "username")}:{parser.get("Database", "password")}@{parser.get("Database", "host")}/{database}'
|
||||
elif parser.get("Database", "type") == "mysql":
|
||||
cn = f'mysql+pymysql://{parser.get("Database", "username")}:{parser.get("Database", "password")}@{parser.get("Database", "host")}/{database}'
|
||||
# Read and parse the INI file once at startup
|
||||
parser = configparser.ConfigParser(strict=False)
|
||||
parser.read("wg-dashboard.ini")
|
||||
|
||||
# Ensure SQLite folder exists
|
||||
SQLITE_PATH = "db"
|
||||
os.makedirs(SQLITE_PATH, exist_ok=True)
|
||||
|
||||
DEFAULT_DB = "wgdashboard"
|
||||
DEFAULT_LOG_DB = "wgdashboard_log"
|
||||
DEFAULT_JOB_DB = "wgdashboard_log"
|
||||
|
||||
def ConnectionString(database_name: str) -> str:
|
||||
"""
|
||||
Returns a SQLAlchemy-compatible connection string for the chosen database.
|
||||
Creates the database if it doesn't exist.
|
||||
"""
|
||||
db_type = parser.get("Database", "type")
|
||||
db_prefix = parser.get("Database", "prefix")
|
||||
database_name = f"{db_prefix}_{database_name}"
|
||||
|
||||
if db_type == "postgresql":
|
||||
username = parser.get("Database", "username")
|
||||
password = parser.get("Database", "password")
|
||||
host = parser.get("Database", "host")
|
||||
cn = f"postgresql+psycopg://{username}:{password}@{host}/{database_name}"
|
||||
elif db_type == "mysql":
|
||||
username = parser.get("Database", "username")
|
||||
password = parser.get("Database", "password")
|
||||
host = parser.get("Database", "host")
|
||||
cn = f"mysql+pymysql://{username}:{password}@{host}/{database_name}"
|
||||
else:
|
||||
cn = f'sqlite:///{os.path.join(sqlitePath, f"{database}.db")}'
|
||||
try:
|
||||
if not database_exists(cn):
|
||||
create_database(cn)
|
||||
except Exception as e:
|
||||
current_app.logger.error("Database error. Terminating...", e)
|
||||
exit(1)
|
||||
|
||||
cn = f"sqlite:///{os.path.join(SQLITE_PATH, f'{database_name}.db')}"
|
||||
|
||||
# Ensure database exists
|
||||
if not database_exists(cn):
|
||||
create_database(cn)
|
||||
|
||||
return cn
|
Reference in New Issue
Block a user