From ae16a444187fe048e05407bfc24d6b8377d87546 Mon Sep 17 00:00:00 2001 From: Yuliang Weng <59968766+yld-weng@users.noreply.github.com> Date: Thu, 20 Jul 2023 11:36:20 +0100 Subject: [PATCH] feat(build): add postgres as a compose service --- .gitignore | 5 ++++- RSEAdmin/settings/dev.py | 35 +++++++++++++++++++++++++++++------ dev.env | 7 +++++++ docker-compose.yml | 15 +++++++++++++++ pyproject.toml | 2 +- scripts/db_init.sh | 24 ++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 dev.env create mode 100644 scripts/db_init.sh diff --git a/.gitignore b/.gitignore index 10315c19..f1484c12 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,7 @@ secrets.json !.gitignore !.github -dist/ \ No newline at end of file +dist/ + +# Docker container volumes +volumes \ No newline at end of file diff --git a/RSEAdmin/settings/dev.py b/RSEAdmin/settings/dev.py index 1221982c..55976d04 100644 --- a/RSEAdmin/settings/dev.py +++ b/RSEAdmin/settings/dev.py @@ -15,13 +15,36 @@ # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), +DEV_CONTAINER = os.getenv('DEV_CONTAINER') + +# Use Postgres with containers +if DEV_CONTAINER is not None: + DATABASE_NAME = os.getenv('DATABASE_NAME', 'django') + + DATABASES = dict( + default={ + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': DATABASE_NAME, + 'USER': os.getenv('DATABASE_USER', 'django'), + 'PASSWORD': os.getenv('DATABASE_PASSWORD', 'django_postgres'), + 'HOST': os.getenv('DATABASE_HOST', 'localhost'), + 'PORT': int(os.getenv('DATABASE_PORT', '5432')), + # Test database + # https://docs.djangoproject.com/en/4.0/topics/testing/overview/#the-test-database + # https://docs.djangoproject.com/en/4.0/ref/settings/#test + 'TEST': { + 'NAME': f"test_{DATABASE_NAME}", + } + } + ) +else: + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } } -} + # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ diff --git a/dev.env b/dev.env new file mode 100644 index 00000000..b803db59 --- /dev/null +++ b/dev.env @@ -0,0 +1,7 @@ +DATABASE_NAME=django +DATABASE_USER=django +DATABASE_PASSWORD=django_postgres +DJANGO_SUPERUSER_PASSWORD=rseadmindjango +DATABASE_HOST=db +DATABASE_PORT=5432 +DEV_CONTAINER=True \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c9a5df36..402e25a5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,8 +3,23 @@ services: app: build: . command: python manage.py runserver 0.0.0.0:8080 + env_file: + - ./dev.env volumes: - .:/var/www ports: - "8080:8080" + + # PostgreSQL docs https://www.postgresql.org/docs/14/index.html + # https://hub.docker.com/_/postgres + db: + image: postgres:14-bullseye + environment: + # postgres superuser + POSTGRES_PASSWORD: django_postgres + POSTGRES_USER: postgres + POSTGRES_DB: postgres + volumes: + - ./scripts/db_init.sh:/tmp/db_init.sh + - "./volumes/postgresql/data:/var/lib/postgresql/data" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 411ff34c..4ed712d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ Django = "^3.2" django-adminlte2 = "~0.4.1" django-polymorphic = "^3.1" gunicorn = {version = "^20.1", optional = true} -psycopg2 = {version = "^2.9", optional = true} +psycopg2 = "^2.9" mysqlclient = {version = "^2", optional = true} python-dateutil = "~2.8.2" cryptography = "^39.0" diff --git a/scripts/db_init.sh b/scripts/db_init.sh new file mode 100644 index 00000000..650f6c02 --- /dev/null +++ b/scripts/db_init.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Configure postgres database +# This should probably be run as the postgres user + +# Options +user=django +database_name=django +password=django_postgres + +# Create the role +psql -c "create role $user with login password '$password';" + +# Create the database +createdb --owner=django $database_name + +# Give the user permission to create databases. +# This is required for Django testing: +# https://docs.djangoproject.com/en/4.0/topics/testing/overview/#the-test-database + echo "Granting $user user permission to create databases..." + psql -c "ALTER ROLE $user CREATEDB" + echo "You might also need to apply migrations." + +