Database toggle

This commit is contained in:
Midgard 2020-07-25 11:53:37 +02:00
parent 17650b72ba
commit a29f4e627e
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 29 additions and 25 deletions

View file

@ -15,15 +15,6 @@ import os
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
def get_env_value(env_variable, default):
try:
return os.environ[env_variable]
except KeyError:
return default
# error_msg = 'Set the {} environment variable'.format(env_variable)
# raise ImproperlyConfigured(error_msg)
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@ -90,16 +81,24 @@ WSGI_APPLICATION = 'KeRS.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases # https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = { if os.getenv('KERS_DB_BACKEND', 'mysql') == 'sqlite3':
'default': { DATABASES = {
'ENGINE': 'django.db.backends.mysql', 'default': {
'NAME': get_env_value('DATABASE_NAME', 'kers'), 'ENGINE': 'django.db.backends.sqlite3',
'USER': get_env_value('DATABASE_USER', 'kers'), 'NAME': os.getenv('KERS_SQLITE_FILE', 'kers.sqlite3'),
'PASSWORD': get_env_value('DATABASE_PASSWORD', 'kers'), }
'HOST': get_env_value('DATABASE_HOST', '127.0.0.1'), }
'PORT': get_env_value('DATABASE_PORT', '3306'), else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.getenv('DATABASE_NAME', 'kers'),
'USER': os.getenv('DATABASE_USER', 'kers'),
'PASSWORD': os.getenv('DATABASE_PASSWORD', 'kers'),
'HOST': os.getenv('DATABASE_HOST', '127.0.0.1'),
'PORT': os.getenv('DATABASE_PORT', '3306'),
}
} }
}
# Password validation # Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
@ -142,8 +141,8 @@ STATICFILES_DIRS = [
# Celery # Celery
CELERY_BROKER = get_env_value('CELERY_BROKER', 'redis://localhost:6379/0') CELERY_BROKER = os.getenv('CELERY_BROKER', 'redis://localhost:6379/0')
CELERY_BACKEND = get_env_value('CELERY_BACKEND', 'redis://localhost:6379/1') CELERY_BACKEND = os.getenv('CELERY_BACKEND', 'redis://localhost:6379/1')
# Custom stuff # Custom stuff
SERVER_URL = 'http://localhost:8000' SERVER_URL = 'http://localhost:8000'

View file

@ -1,4 +1,5 @@
SQLITE_FILE ?= kers.sqlite3 KERS_DB_BACKEND ?= mysql
KERS_SQLITE_FILE ?= kers.sqlite3
KERS_SUPERUSER_ZEUSID ?= 1 KERS_SUPERUSER_ZEUSID ?= 1
KERS_SUPERUSER_NAME ?= admin KERS_SUPERUSER_NAME ?= admin
KERS_SUPERUSER_PASSWORD ?= admin KERS_SUPERUSER_PASSWORD ?= admin
@ -28,11 +29,15 @@ superuser:
(sleep 1; echo "$(KERS_SUPERUSER_PASSWORD)"; sleep 0.5; echo "$(KERS_SUPERUSER_PASSWORD)"; sleep 1; echo "y") | \ (sleep 1; echo "$(KERS_SUPERUSER_PASSWORD)"; sleep 0.5; echo "$(KERS_SUPERUSER_PASSWORD)"; sleep 1; echo "y") | \
socat - EXEC:'$(PYTHON) manage.py createsuperuser --username $(KERS_SUPERUSER_NAME) --zeus_id $(KERS_SUPERUSER_ZEUSID)',pty,setsid,ctty socat - EXEC:'$(PYTHON) manage.py createsuperuser --username $(KERS_SUPERUSER_NAME) --zeus_id $(KERS_SUPERUSER_ZEUSID)',pty,setsid,ctty
.PHONY: rewrite_migrations .PHONY: reset_db rewrite_migrations
reset_db:
case "$(KERS_DB_BACKEND)" in \
mysql) printf "Please drop your MySQL/MariaDB tables and press enter"; read; ;; \
sqlite3) rm -f "$(SQLITE_FILE)"; ;; \
esac
make migrate
rewrite_migrations: rewrite_migrations:
@printf "Only for when there is no data in production yet. Continue? (y/N) "; read continue; [ "$$continue" = y ] @printf "Only for when there is no data in production yet. Continue? (y/N) "; read continue; [ "$$continue" = y ]
rm -f events/migrations/0*_*.py users/migrations/0*_*.py rm -f events/migrations/0*_*.py users/migrations/0*_*.py
rm -f "$(SQLITE_FILE)"
make migrations make migrations
make migrate make reset_db
make superuser