File tree Expand file tree Collapse file tree 1 file changed +21
-8
lines changed Expand file tree Collapse file tree 1 file changed +21
-8
lines changed Original file line number Diff line number Diff line change @@ -4,26 +4,39 @@ set -euf -o pipefail
4
4
5
5
export PGSSLMODE=require
6
6
7
+ wait_for_db () {
8
+ local retries=30
9
+ local sleep_time=2
10
+
11
+ echo " Waiting for PostgreSQL server to be ready..."
12
+ until psql " $DATABASE_URL " -c ' \q' 2> /dev/null || [ " $retries " -eq 0 ]; do
13
+ echo " PostgreSQL is unavailable - sleeping ($(( retries-- )) retries left)..."
14
+ sleep " $sleep_time "
15
+ done
16
+
17
+ if [ " $retries " -eq 0 ]; then
18
+ echo " ERROR: PostgreSQL server did not become ready in time."
19
+ exit 1
20
+ fi
21
+
22
+ echo " PostgreSQL server is ready!"
23
+ }
24
+
7
25
if [ -z " ${DATABASE_URL:- " " } " ]; then
8
26
echo " WARNING: DATABASE_URL not found in environment."
9
27
else
10
- echo " DEBUG: DATABASE_URL=$DATABASE_URL "
11
28
12
29
# Extract connection details from DATABASE_URL
13
30
# shellcheck disable=SC2046
14
31
export $( parse_database_url.py | xargs)
15
32
16
- # Log the parsed variables for debugging
17
- echo " DEBUG: Parsed variables:"
18
- echo " HOST=$HOST "
19
- echo " PORT=$PORT "
20
- echo " NAME=$NAME "
21
- echo " USER=$USER "
22
-
23
33
# Setup PGSERVICE so `psql` just does the right thing
24
34
/bin/echo -e " [$NAME ]\nhost=$HOST \nport=$PORT \ndbname=$NAME \nuser=$USER " > ~ /.pg_service.conf
25
35
export PGSERVICE=" $NAME "
26
36
37
+ # Wait for PostgreSQL to be ready
38
+ wait_for_db
39
+
27
40
# Detect PostgreSQL server version
28
41
SERVER_VERSION=$( psql " $DATABASE_URL " -tAc " SHOW server_version;" | cut -d ' .' -f 1 || true)
29
42
if [ -z " $SERVER_VERSION " ]; then
You can’t perform that action at this time.
0 commit comments