Enable dynamic database backend configuration

Adjusted the project settings to support MySQL or MariaDB as the database backend based on runtime configuration. This update allows for more flexible deployment options, utilizing either SQLite for development or production-ready databases like MySQL or MariaDB depending on the CONFIG variables. Defaults to SQLite when no external database is specified, maintaining backward compatibility.
This commit is contained in:
Kumi 2023-12-04 16:19:06 +01:00
parent 27eca84ada
commit 57119dfc4c
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -71,12 +71,25 @@ WSGI_APPLICATION = 'expalert.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
if (dbtype := "MySQL") in CONFIG or (dbtype := "MariaDB") in CONFIG:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': CONFIG.get(dbtype, "Database"),
'USER': CONFIG.get(dbtype, "Username"),
'PASSWORD': CONFIG.get(dbtype, "Password"),
'HOST': CONFIG.get(dbtype, "Host", fallback="localhost"),
'PORT': CONFIG.getint(dbtype, "Port", fallback=3306)
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
}
# Password validation