-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup-db.sh
executable file
·80 lines (67 loc) · 2.35 KB
/
setup-db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
# Exit on any error
set -e
# Check for db/.env.local and create if it doesn't exist
if [ ! -f db/.env.local ]; then
echo "⚠️ db/.env.local not found. Creating with default values..."
cat > db/.env.local << EOF
WFPREV_DB_PASSWORD=password
POSTGRES_PASSWORD=password
EOF
echo "Created db/.env.local with default passwords"
fi
echo "🐘 Setting up PostgreSQL..."
docker pull postgis/postgis:16-3.4
# Stop and remove existing container if it exists
docker rm -f wfprev-postgres 2>/dev/null || true
# Create a Docker network if it doesn't exist
docker network create wfprev-network 2>/dev/null || true
# Start PostgreSQL with database creation
docker run --name wfprev-postgres \
--network wfprev-network \
--env-file db/.env.local \
-e POSTGRES_USER=wfprev \
-e POSTGRES_DB=wfprev \
-e POSTGRES_HOST_AUTH_METHOD=trust \
-p 5432:5432 \
-d postgis/postgis:16-3.4
echo "⏳ Waiting for PostgreSQL to be ready..."
# More comprehensive health check
max_attempts=30
attempt=1
while [ $attempt -le $max_attempts ]; do
if docker exec wfprev-postgres pg_isready -U wfprev; then
# Try to actually connect and run a query
if docker exec wfprev-postgres psql -U wfprev -d wfprev -c '\l' >/dev/null 2>&1; then
echo "✅ PostgreSQL is ready and accepting connections!"
break
fi
fi
if [ $attempt -eq $max_attempts ]; then
echo "❌ Failed to connect to PostgreSQL after $max_attempts attempts"
echo "🔍 Checking PostgreSQL logs:"
docker logs wfprev-postgres
exit 1
fi
echo "PostgreSQL is unavailable - attempt $attempt/$max_attempts - sleeping 2s"
sleep 2
attempt=$((attempt + 1))
done
echo "🏗️ Building Liquibase image..."
docker build -t liquibase -f db/Dockerfile.liquibase.local ./db
echo "📦 Running database migrations..."
docker run --rm \
--network wfprev-network \
--env-file db/.env.local \
-v $(pwd)/db:/liquibase/workspace \
liquibase \
/bin/bash -c 'cd /liquibase/workspace && liquibase \
--url=jdbc:postgresql://wfprev-postgres:5432/wfprev \
--changelog-file=main-changelog.json \
--username=wfprev \
--password=$WFPREV_DB_PASSWORD \
--logFile=/liquibase/workspace/liquibase.log \
update'
# Clean up log file if successful
rm -f db/liquibase.log
echo "✅ Database setup complete!"