Skip to content
Snippets Groups Projects
Commit 9a68e118 authored by Roang's avatar Roang
Browse files

Update database connections

Simplyfied the database connection setup by removing the schema setup and
using the postgres user created by the postgres image.
parent f4105d66
Branches
No related tags found
No related merge requests found
...@@ -172,7 +172,6 @@ app_version: ...@@ -172,7 +172,6 @@ app_version:
FF_NETWORK_PER_BUILD: 1 FF_NETWORK_PER_BUILD: 1
POSTGRES_DB: migrations POSTGRES_DB: migrations
DATABASE_URL: "postgis://ci:runner@db_server/migrations" DATABASE_URL: "postgis://ci:runner@db_server/migrations"
HUB_DB_SCHEMA: migrations
script: script:
- python3 -V - python3 -V
- pdm install -d --no-editable -G dev - pdm install -d --no-editable -G dev
...@@ -210,7 +209,6 @@ migration_check: ...@@ -210,7 +209,6 @@ migration_check:
FF_NETWORK_PER_BUILD: 1 FF_NETWORK_PER_BUILD: 1
POSTGRES_DB: migrations POSTGRES_DB: migrations
DATABASE_URL: "postgis://ci:runner@db_server/migrations" DATABASE_URL: "postgis://ci:runner@db_server/migrations"
HUB_DB_SCHEMA: migrations
script: script:
- python3 -V - python3 -V
- pdm install -d --no-editable -G dev - pdm install -d --no-editable -G dev
...@@ -228,7 +226,6 @@ django-tests: ...@@ -228,7 +226,6 @@ django-tests:
FF_NETWORK_PER_BUILD: 1 FF_NETWORK_PER_BUILD: 1
POSTGRES_DB: self_test POSTGRES_DB: self_test
DATABASE_URL: "postgis://ci:runner@db_server/self_test" DATABASE_URL: "postgis://ci:runner@db_server/self_test"
HUB_DB_SCHEMA: self_test
script: script:
- python3 -V - python3 -V
- pdm install -d --no-editable -G dev - pdm install -d --no-editable -G dev
......
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE DATABASE ${HUB_DB};
CREATE USER ${HUB_DB_USER} PASSWORD '${HUB_DB_PASSWORD}';
GRANT ALL PRIVILEGES ON DATABASE ${HUB_DB} TO ${HUB_DB_USER};
ALTER ROLE ${HUB_DB_USER} SET client_encoding TO 'utf8';
ALTER ROLE ${HUB_DB_USER} SET default_transaction_isolation TO 'read committed';
ALTER ROLE ${HUB_DB_USER} SET timezone TO 'UTC';
EOSQL
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$HUB_DB" <<-EOSQL
CREATE SCHEMA ${HUB_DB_SCHEMA} AUTHORIZATION ${HUB_DB_USER}
EOSQL
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$HUB_DB" <<-EOSQL
CREATE EXTENSION postgis WITH SCHEMA ${HUB_DB_SCHEMA};
EOSQL
...@@ -12,8 +12,7 @@ services: ...@@ -12,8 +12,7 @@ services:
environment: environment:
DJANGO_MIGRATE: "${DJANGO_MIGRATE:-yes}" DJANGO_MIGRATE: "${DJANGO_MIGRATE:-yes}"
DJANGO_LOAD_FIXTURE: ${DJANGO_LOAD_FIXTURE:-core/fixtures/anhalter.json} DJANGO_LOAD_FIXTURE: ${DJANGO_LOAD_FIXTURE:-core/fixtures/anhalter.json}
DATABASE_URL: "postgis://${HUB_DB_USER:-hub}:${HUB_DB_PASSWORD:-hub}@${HUB_DB_HOST:-db}:${DB_PORT:-5432}/${HUB_DB:-hub}" DATABASE_URL: "postgis://${HUB_DB_USER:-postgres}:${HUB_DB_PASSWORD:-postgres}@${HUB_DB_HOST:-db}:${DB_PORT:-5432}/${HUB_DB:-postgres}"
HUB_DB_SCHEMA: "${HUB_SCHEMA:-hubschema}"
CLIENT_IP_HEADER: "HTTP_X_REAL_IP" CLIENT_IP_HEADER: "HTTP_X_REAL_IP"
NUM_WORKERS: 1 NUM_WORKERS: 1
SERVE_ADMIN: True SERVE_ADMIN: True
...@@ -30,8 +29,7 @@ services: ...@@ -30,8 +29,7 @@ services:
volumes: volumes:
- ./src:/app - ./src:/app
environment: environment:
DATABASE_URL: "postgis://${HUB_DB_USER:-postgres}:${HUB_DB_PASSWORD:-postgres}@${HUB_DB_HOST:-db}:${DB_PORT:-5432}/${HUB_DB:-postgres}" DATABASE_URL: "postgis://${HUB_DB_USER:-postgres}:${HUB_DB_PASSWORD:-postgres}@${HUB_DB_HOST:-db}:${DB_PORT:-5432}/${HUB_DB:-hub_test}"
HUB_DB_SCHEMA: "${HUB_SCHEMA:-hubschema}"
SSO_SECRET_GENERATE: True SSO_SECRET_GENERATE: True
depends_on: depends_on:
- db - db
...@@ -52,15 +50,10 @@ services: ...@@ -52,15 +50,10 @@ services:
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- postgres-data:/var/lib/postgresql/data - postgres-data:/var/lib/postgresql/data
- ./deployment/postgres/init:/docker-entrypoint-initdb.d
environment: environment:
POSTGRES_USER: "${POSTGRES_USER:-postgres}" POSTGRES_USER: "${HUB_DB_USER:-postgres}"
POSTGRES_DB: "${POSTGRES_DB:-postgres}" POSTGRES_DB: "${HUB_DB:-postgres}"
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD:-postgres}" POSTGRES_PASSWORD: "${HUB_DB_PASSWORD:-postgres}"
HUB_DB: "${HUB_DB:-hub}"
HUB_DB_SCHEMA: "${HUB_SCHEMA:-hubschema}"
HUB_DB_USER: "${HUB_DB_USER:-hub}"
HUB_DB_PASSWORD: "${HUB_DB_PASSWORD:-hub}"
ports: ports:
- "5432:5432" - "5432:5432"
- "80:80" - "80:80"
......
...@@ -6,12 +6,6 @@ from django.test.runner import DiscoverRunner ...@@ -6,12 +6,6 @@ from django.test.runner import DiscoverRunner
def prepare_database(self): def prepare_database(self):
self.connect() self.connect()
self.connection.cursor().execute(
f"""
CREATE SCHEMA IF NOT EXISTS hubschema AUTHORIZATION {self.connection.info.user};
GRANT ALL ON SCHEMA hubschema TO {self.connection.info.user};
"""
)
class PostgresSchemaTestRunner(DiscoverRunner): class PostgresSchemaTestRunner(DiscoverRunner):
......
...@@ -6,7 +6,6 @@ from hub.settings.base import * # noqa: F403 ...@@ -6,7 +6,6 @@ from hub.settings.base import * # noqa: F403
default_env = environ.FileAwareEnv( default_env = environ.FileAwareEnv(
# set casting, default value # set casting, default value
HUB_DB_SCHEMA=(str, ''),
DJANGO_HOST=(str, ''), DJANGO_HOST=(str, ''),
SERVE_ADMIN=(bool, False), SERVE_ADMIN=(bool, False),
SERVE_API=(bool, True), SERVE_API=(bool, True),
...@@ -27,10 +26,6 @@ def print_banner(message: str, filler: str = '*'): ...@@ -27,10 +26,6 @@ def print_banner(message: str, filler: str = '*'):
# configure database using environment variable "DATABASE_URL" # configure database using environment variable "DATABASE_URL"
DATABASES['default'] = default_env.db(default='postgis://localhost/hub') # noqa: F405 DATABASES['default'] = default_env.db(default='postgis://localhost/hub') # noqa: F405
if DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql', 'django.contrib.gis.db.backends.postgis'] and default_env('HUB_DB_SCHEMA'): # noqa:F405
DATABASES['default']['OPTIONS'] = { # noqa:F405
'options': f'-c search_path={default_env("HUB_DB_SCHEMA")}'
}
# ensure the DJANGO_HOST variable's content is an allowed host (used for health check) # ensure the DJANGO_HOST variable's content is an allowed host (used for health check)
if (django_host := default_env('DJANGO_HOST')) is not None: if (django_host := default_env('DJANGO_HOST')) is not None:
......
...@@ -11,7 +11,6 @@ setenv= ...@@ -11,7 +11,6 @@ setenv=
DJANGO_DEBUG=I_KNOW_WHAT_I_AM_DOING DJANGO_DEBUG=I_KNOW_WHAT_I_AM_DOING
DJANGO_SETTINGS_MODULE=hub.settings.test DJANGO_SETTINGS_MODULE=hub.settings.test
DISABLE_REQUEST_LOGGING=True DISABLE_REQUEST_LOGGING=True
HUB_DB_SCHEMA={env:HUB_SCHEMA}
SERVE_API=True SERVE_API=True
SERVE_BACKOFFICE=True SERVE_BACKOFFICE=True
SERVE_FRONTEND=True SERVE_FRONTEND=True
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment