feat(settings): add dynamic DB selection based on config

Updated settings to dynamically select the database backend (MySQL, MariaDB, PostgreSQL, or SQLite) based on the configuration file. This change improves flexibility and allows easier customization of the database setup by just modifying the config file.
This commit is contained in:
Kumi 2024-06-22 19:02:50 +02:00
parent dbf7cde183
commit 21d90e6cd8
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -5,10 +5,10 @@ from autosecretkey import AutoSecretKey
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent
ask = AutoSecretKey("settings.ini", template=BASE_DIR / "settings.ini.template")
ASK = AutoSecretKey("settings.ini", template=BASE_DIR / "settings.ini.template")
SECRET_KEY = ask.secret_key
CONFIG = ask.config
SECRET_KEY = ASK.secret_key
CONFIG = ASK.config
DEBUG = CONFIG.getboolean("FreeDOI", "debug", fallback=False)
ALLOWED_HOSTS = CONFIG.get("FreeDOI", "host", fallback="*").split(",")
@ -69,12 +69,37 @@ WSGI_APPLICATION = "freedoi.wsgi.application"
# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
DATABASES = {
if (dbtype := "MySQL") in ASK.config or (dbtype := "MariaDB") in ASK.config:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": ASK.config.get(dbtype, "Database"),
"USER": ASK.config.get(dbtype, "Username"),
"PASSWORD": ASK.config.get(dbtype, "Password"),
"HOST": ASK.config.get(dbtype, "Host", fallback="localhost"),
"PORT": ASK.config.getint(dbtype, "Port", fallback=3306),
}
}
elif (dbtype := "Postgres") in ASK.config or (dbtype := "PostgreSQL") in ASK.config:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": ASK.config.get(dbtype, "Database"),
"USER": ASK.config.get(dbtype, "Username"),
"PASSWORD": ASK.config.get(dbtype, "Password"),
"HOST": ASK.config.get(dbtype, "Host", fallback="localhost"),
"PORT": ASK.config.getint(dbtype, "Port", fallback=5432),
}
}
else:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "db.sqlite3",
}
}
}
# Password validation