mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-09-01 16:48:19 +00:00
bake db migrations into app on start (#12)
This commit is contained in:
+5
-2
@@ -6,7 +6,10 @@ COPY . .
|
||||
RUN go build -o tower ./cmd/tower
|
||||
|
||||
FROM alpine:3.19
|
||||
RUN apk add --no-cache ca-certificates tzdata
|
||||
RUN apk add --no-cache ca-certificates tzdata postgresql-client
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/tower .
|
||||
ENTRYPOINT ["./tower"]
|
||||
COPY db/migrations/ ./migrations/
|
||||
COPY .build/docker-entrypoint.sh .
|
||||
RUN chmod +x docker-entrypoint.sh
|
||||
ENTRYPOINT ["./docker-entrypoint.sh"]
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
if [ -n "$POSTGRES_DSN" ]; then
|
||||
echo "Running database migrations..."
|
||||
|
||||
psql "$POSTGRES_DSN" -c "CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
filename TEXT PRIMARY KEY,
|
||||
applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);"
|
||||
|
||||
for f in /app/migrations/*.sql; do
|
||||
name=$(basename "$f")
|
||||
already=$(psql "$POSTGRES_DSN" -tAc "SELECT 1 FROM schema_migrations WHERE filename = '$name'")
|
||||
if [ "$already" != "1" ]; then
|
||||
echo " Applying $name..."
|
||||
psql "$POSTGRES_DSN" -f "$f"
|
||||
psql "$POSTGRES_DSN" -c "INSERT INTO schema_migrations (filename) VALUES ('$name');"
|
||||
else
|
||||
echo " Skipping $name (already applied)"
|
||||
fi
|
||||
done
|
||||
|
||||
echo "Migrations complete."
|
||||
fi
|
||||
|
||||
exec ./tower "$@"
|
||||
Reference in New Issue
Block a user