From 2b74919874a400375c93fb698646d5c98dba67cf Mon Sep 17 00:00:00 2001 From: Jelmer Draaijer Date: Thu, 15 Feb 2024 11:21:16 +0100 Subject: [PATCH] Remove DATABASE where possible --- docs/developing-locally.rst | 4 ++- {{cookiecutter.project_slug}}/.drone.yml | 2 -- .../.github/workflows/ci.yml | 6 ++++- {{cookiecutter.project_slug}}/.gitlab-ci.yml | 2 -- .../compose/production/django/Dockerfile | 3 +-- .../compose/production/django/entrypoint | 1 - .../config/settings/base.py | 25 +++++++++++-------- 7 files changed, 24 insertions(+), 19 deletions(-) diff --git a/docs/developing-locally.rst b/docs/developing-locally.rst index 92379f4fd3..2642194dab 100644 --- a/docs/developing-locally.rst +++ b/docs/developing-locally.rst @@ -55,7 +55,9 @@ First things first. #. Set the environment variables for your database(s): :: - $ export DATABASE_URL=postgres://postgres:@127.0.0.1:5432/ + $ export POSTGRES_USER= + $ export POSTGRES_PASSWORD= + $ export POSTGRES_DB= # Optional: set broker URL if using Celery $ export CELERY_BROKER_URL=redis://localhost:6379/0 diff --git a/{{cookiecutter.project_slug}}/.drone.yml b/{{cookiecutter.project_slug}}/.drone.yml index dc08bfbab0..6b15b7fd4b 100644 --- a/{{cookiecutter.project_slug}}/.drone.yml +++ b/{{cookiecutter.project_slug}}/.drone.yml @@ -28,8 +28,6 @@ steps: pull: if-not-exists {%- if cookiecutter.use_docker == 'y' %} image: docker/compose:1.29.2 - environment: - DATABASE_URL: pgsql://$POSTGRES_USER:$POSTGRES_PASSWORD@postgres/$POSTGRES_DB commands: - docker-compose -f local.yml build - docker-compose -f local.yml run --rm django python manage.py migrate diff --git a/{{cookiecutter.project_slug}}/.github/workflows/ci.yml b/{{cookiecutter.project_slug}}/.github/workflows/ci.yml index e39933fe15..5d1d1675f0 100644 --- a/{{cookiecutter.project_slug}}/.github/workflows/ci.yml +++ b/{{cookiecutter.project_slug}}/.github/workflows/ci.yml @@ -60,7 +60,11 @@ jobs: CELERY_BROKER_URL: 'redis://localhost:6379/0' {%- endif %} # postgres://user:password@host:port/database - DATABASE_URL: 'postgres://postgres:postgres@localhost:5432/postgres' + POSTGRES_USER: 'postgres' + POSTGRES_PASSWORD: 'postgres' + POSTGRES_DB: 'postgres' + POSTGRES_HOST: 'postgres' + {%- endif %} steps: diff --git a/{{cookiecutter.project_slug}}/.gitlab-ci.yml b/{{cookiecutter.project_slug}}/.gitlab-ci.yml index 3502120038..ebafd5c96f 100644 --- a/{{cookiecutter.project_slug}}/.gitlab-ci.yml +++ b/{{cookiecutter.project_slug}}/.gitlab-ci.yml @@ -45,8 +45,6 @@ pytest: - python services: - postgres:{{ cookiecutter.postgresql_version }} - variables: - DATABASE_URL: pgsql://$POSTGRES_USER:$POSTGRES_PASSWORD@postgres/$POSTGRES_DB before_script: - pip install -r requirements/local.txt script: diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index fb7fec50fd..317fce4394 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -121,8 +121,7 @@ RUN chown django:django ${APP_HOME} USER django -RUN DATABASE_URL="" \ - {%- if cookiecutter.use_celery == "y" %} +RUN {%- if cookiecutter.use_celery == "y" %} CELERY_BROKER_URL="" \ {%- endif %} DJANGO_SETTINGS_MODULE="config.settings.test" \ diff --git a/{{cookiecutter.project_slug}}/compose/production/django/entrypoint b/{{cookiecutter.project_slug}}/compose/production/django/entrypoint index dd07f2d2a6..1b309baeb4 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/entrypoint +++ b/{{cookiecutter.project_slug}}/compose/production/django/entrypoint @@ -14,7 +14,6 @@ if [ -z "${POSTGRES_USER}" ]; then base_postgres_image_default_user='postgres' export POSTGRES_USER="${base_postgres_image_default_user}" fi -export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" python << END import sys diff --git a/{{cookiecutter.project_slug}}/config/settings/base.py b/{{cookiecutter.project_slug}}/config/settings/base.py index 55a064e748..ae1f037bd2 100644 --- a/{{cookiecutter.project_slug}}/config/settings/base.py +++ b/{{cookiecutter.project_slug}}/config/settings/base.py @@ -45,16 +45,21 @@ # DATABASES # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#databases -{% if cookiecutter.use_docker == "y" -%} -DATABASES = {"default": env.db("DATABASE_URL")} -{%- else %} -DATABASES = { - "default": env.db( - "DATABASE_URL", - default="postgres://{% if cookiecutter.windows == 'y' %}localhost{% endif %}/{{cookiecutter.project_slug}}", - ), -} -{%- endif %} + +if db_url := env.db("DATABASE_URL", default=None): + DATABASES = {"default": db_url} +else: + DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql", + "NAME": env.str("POSTGRES_DB"), + "USER": env.str("POSTGRES_USER"), + "PASSWORD": env.str("POSTGRES_PASSWORD"), + "HOST": env.str("POSTGRES_HOST", default="{% if cookiecutter.windows == 'y' or cookiecutter.use_docker == 'n' %}localhost{%else%}postgres{% endif %}"), + "PORT": env.str("POSTGRES_PORT", default="5432"), + }, + } + DATABASES["default"]["ATOMIC_REQUESTS"] = True # https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-DEFAULT_AUTO_FIELD DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"