-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
init-db.sh
executable file
·64 lines (52 loc) · 1.55 KB
/
init-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
#!/bin/bash
# Enable error handling and debug tracing
set -e
# set -x ( DEBUG )
error_handling() {
echo "An error occurred. Exiting..."
exit 1
}
# trap errors
trap error_handling ERR
cmd="$@"
# Create .env file
echo "Creating .env file..."
cat << EOF > .env
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?schema=public
ZT_ADDR=${ZT_ADDR}
NEXT_PUBLIC_APP_VERSION=${NEXT_PUBLIC_APP_VERSION}
EOF
# config
envFilename='.env'
nextFolder='.next'
# Currently not in use.
function apply_path {
# echo "Applying path..."
while read line; do
if [ "${line:0:1}" == "#" ] || [ "${line}" == "" ]; then
continue
fi
configName="$(cut -d'=' -f1 <<<"$line")"
configValue="$(cut -d'=' -f2 <<<"$line")"
envValue="${!configName}";
if [ -n "$configValue" ] && [ -n "$envValue" ]; then
echo "Replace: ${configValue} with: ${envValue}"
find $nextFolder \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#$configValue#$envValue#g"
fi
done < $envFilename
}
# apply_path
until PGPASSWORD=$POSTGRES_PASSWORD psql -h "$POSTGRES_HOST" -p "$POSTGRES_PORT" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c '\q'; do
echo "Postgres is unavailable - sleeping"
sleep 1
done
# apply migrations to the database
echo "Applying migrations to the database..."
npx prisma migrate deploy
echo "Migrations applied successfully!"
# seed the database
echo "Seeding the database..."
npx prisma db seed
echo "Database seeded successfully!"
echo "Executing command"
exec $cmd