-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
50 lines (39 loc) · 1.58 KB
/
run.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
#!/bin/sh
set -e
# Check if LITESTREAM_BUCKET is set
if [ -z "${LITESTREAM_BUCKET}" ]; then
echo "Error: LITESTREAM_BUCKET environment variable is not set" >&2
exit 1
fi
sed "s|\${LITESTREAM_BUCKET}|${LITESTREAM_BUCKET}|g" /etc/litestream.yml.template > /app/litestream.yml
# Remove existing db if exists
if [ -f /app/db/prod.db ]; then
mv /app/db/prod.db /app/db/prod.db.bak
fi
# Restore database from GCS if exists
litestream restore -if-replica-exists -config /app/litestream.yml /app/db/prod.db
if [ -f /app/db/prod.db ]; then
echo "Successfully restored database from Cloud Storage"
rm -f /app/db/prod.db.bak
else
echo "No database backup found in Cloud Storage, using original"
mv /app/db/prod.db.bak /app/db/prod.db
fi
# Before starting the app, ensure WAL mode is disabled for Prisma compatibility
sqlite3 /app/db/prod.db "PRAGMA journal_mode=DELETE;"
# Check for migration mode
if [ "${RUN_MIGRATIONS}" = "true" ]; then
echo "Migration mode detected. Running migrations..."
# WAL mode must be disabled for Prisma compatibility
sqlite3 /app/db/prod.db "PRAGMA journal_mode=DELETE;"
# Run Prisma migrations
DATABASE_URL="file:/app/db/prod.db" npx prisma migrate deploy
echo "Migrations completed successfully"
else
# WAL mode must be disabled for Prisma compatibility
sqlite3 /app/db/prod.db "PRAGMA journal_mode=DELETE;"
fi
# Start Litestream replication with the app as the subprocess
exec litestream replicate \
-config /app/litestream.yml \
-exec "node node_modules/@remix-run/serve/dist/cli.js build/server/index.js"