From 2747b04ea90c8b87c0413f07bda117547176a52f Mon Sep 17 00:00:00 2001 From: Vincent Pochet Date: Fri, 22 Sep 2023 08:35:20 +0200 Subject: [PATCH] misc: Add config for a test database on API (#283) --- docker-compose.dev.yml | 4 +++- .../create-multiple-postgresql-databases.sh | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 scripts/pg-init-scripts/create-multiple-postgresql-databases.sh diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 8b752f3..93d2f56 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -30,11 +30,12 @@ services: container_name: lago_db restart: unless-stopped environment: - POSTGRES_DB: ${POSTGRES_DB:-lago} POSTGRES_USER: ${POSTGRES_USER:-lago} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme} PGDATA: /data/postgres + POSTGRES_MULTIPLE_DATABASES: lago,lago_test volumes: + - ./pg-init-scripts:/docker-entrypoint-initdb.d - postgres_data:/data/postgres ports: - 5432:5432 @@ -86,6 +87,7 @@ services: environment: - LAGO_API_URL=https://api.lago.dev - DATABASE_URL=postgresql://${POSTGRES_USER:-lago}:${POSTGRES_PASSWORD:-changeme}@db:5432/${POSTGRES_DB:-lago} + - DATABASE_TEST_URL=postgresql://${POSTGRES_USER:-lago}:${POSTGRES_PASSWORD:-changeme}@db:5432/${POSTGRES_DB:-lago_test} - REDIS_URL=redis://redis:6379 - SECRET_KEY_BASE=${SECRET_KEY_BASE:-your-secret-key-base-hex-64} - LAGO_FRONT_URL=${FRONT_URL:-https://app.lago.dev} diff --git a/scripts/pg-init-scripts/create-multiple-postgresql-databases.sh b/scripts/pg-init-scripts/create-multiple-postgresql-databases.sh new file mode 100644 index 0000000..aa665fa --- /dev/null +++ b/scripts/pg-init-scripts/create-multiple-postgresql-databases.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +set -e +set -u + +function create_user_and_database() { + local database=$1 + echo " Creating user and database '$database'" + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL + CREATE USER $database; + CREATE DATABASE $database; + GRANT ALL PRIVILEGES ON DATABASE $database TO $database; +EOSQL +} + +if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then + echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" + for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do + create_user_and_database $db + done + echo "Multiple databases created" +fi