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:
parent
dbf7cde183
commit
21d90e6cd8
1 changed files with 33 additions and 8 deletions
|
@ -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,6 +69,31 @@ WSGI_APPLICATION = "freedoi.wsgi.application"
|
|||
# Database
|
||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#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",
|
||||
|
|
Loading…
Reference in a new issue