diff --git a/compose.yml b/compose.yml index 43b204e..955f3c3 100644 --- a/compose.yml +++ b/compose.yml @@ -214,10 +214,6 @@ services: - type: volume source: postgres-database target: /var/lib/postgresql/data - - type: bind - source: ./scripts/run-in-container/postgres-init.sh - target: /docker-entrypoint-initdb.d/postgres-init.sh - read_only: true networks: internal: dns: 172.20.0.200 diff --git a/scripts/internal/firstrun-postgres-container.sh b/scripts/internal/firstrun-postgres-container.sh new file mode 100755 index 0000000..1697658 --- /dev/null +++ b/scripts/internal/firstrun-postgres-container.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +# shellcheck source=./framework.sh +source "$(dirname "$(realpath "$0")")/framework.sh" +parse_arguments "$@" + +print_info "Setting up the Postgres container..." + +load_dotenv postgres.env postgres.local.env +postgres_init_script=$(cat "$git_base_dir/scripts/run-in-container/postgres-init.sh") + +compose_no_progress up -d postgres +run_command_until_success "Waiting for Postgres to be ready..." 5 \ + docker compose exec postgres psql -U "$POSTGRES_USER" -c "\\l" + +run_verbose docker compose exec postgres sh -c "$postgres_init_script" + +print_success "Postgres container is set up." diff --git a/scripts/internal/migrations.sh b/scripts/internal/migrations.sh index 62b74c1..1b301b3 100755 --- a/scripts/internal/migrations.sh +++ b/scripts/internal/migrations.sh @@ -9,7 +9,7 @@ print_info "Running migrations..." load_dotenv postgres.env postgres.local.env compose_no_progress up -d postgres -if [[ $(docker compose exec postgres psql -U "$POSTGRES_USER" -d friends -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '3ds';") == *"(1 row)" ]]; then +if [[ $(docker compose exec postgres psql -At -U "$POSTGRES_USER" -d friends -c "SELECT 1 FROM information_schema.schemata WHERE schema_name = '3ds';") = "1" ]]; then print_info "Migrating friends to the nex-go rewrite..." migration=$(cat "$git_base_dir/scripts/run-in-container/friends-nex-go-rewrite-migration.sql") # shellcheck disable=SC2046 diff --git a/scripts/run-in-container/postgres-init.sh b/scripts/run-in-container/postgres-init.sh index 80d463c..3d667bc 100644 --- a/scripts/run-in-container/postgres-init.sh +++ b/scripts/run-in-container/postgres-init.sh @@ -5,6 +5,8 @@ set -eu databases="friends super_mario_maker" for database in $databases; do - echo "Creating database: $database" - psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "CREATE DATABASE $database;" + if [ "$(psql -At -U "$POSTGRES_USER" -c "SELECT 1 FROM pg_database WHERE datname='$database'")" = '' ]; then + echo "Creating database: $database" + psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -c "CREATE DATABASE $database;" + fi done diff --git a/setup.sh b/setup.sh index f91cb76..d8a2f20 100755 --- a/setup.sh +++ b/setup.sh @@ -57,6 +57,7 @@ setup_environment_variables() { setup_containers() { ./scripts/internal/firstrun-mongodb-container.sh ./scripts/internal/firstrun-minio-container.sh + ./scripts/internal/firstrun-postgres-container.sh ./scripts/internal/update-account-servers-database.sh ./scripts/internal/update-miiverse-endpoints.sh ./scripts/internal/update-postgres-password.sh