Barazo Docker Compose templates for self-hosting
barazo.forum
1#!/usr/bin/env bash
2# Barazo Staging Reset Script
3#
4# Drops and recreates the staging database, then restarts all services.
5# Only for use on the staging environment -- never run this on production.
6#
7# Usage:
8# ./scripts/reset-staging.sh # Reset with confirmation prompt
9# ./scripts/reset-staging.sh --force # Reset without confirmation
10#
11# What it does:
12# 1. Stops API and Web services (keeps postgres/valkey running)
13# 2. Drops and recreates the staging database
14# 3. Restarts all services (schema is applied on startup)
15#
16# Environment:
17# COMPOSE_FILE Docker Compose files (default: docker-compose.yml -f docker-compose.staging.yml)
18
19set -euo pipefail
20
21COMPOSE_CMD="docker compose -f docker-compose.yml -f docker-compose.staging.yml"
22FORCE=false
23
24# Parse arguments
25for arg in "$@"; do
26 case "$arg" in
27 --force) FORCE=true ;;
28 --help|-h)
29 echo "Usage: $0 [--force]"
30 echo ""
31 echo "Drops and recreates the staging database, then restarts services."
32 echo ""
33 echo "Options:"
34 echo " --force Skip confirmation prompt"
35 exit 0
36 ;;
37 *)
38 echo "Unknown argument: $arg" >&2
39 exit 1
40 ;;
41 esac
42done
43
44# Safety check: refuse to run if NODE_ENV=production is detected
45if [ "${NODE_ENV:-}" = "production" ]; then
46 echo "Error: NODE_ENV is set to 'production'. This script is for staging only." >&2
47 exit 1
48fi
49
50# Confirmation prompt
51if [ "$FORCE" = false ]; then
52 echo "WARNING: This will destroy ALL data in the staging database."
53 echo ""
54 read -r -p "Are you sure? Type 'reset staging' to confirm: " CONFIRM
55 if [ "$CONFIRM" != "reset staging" ]; then
56 echo "Aborted."
57 exit 0
58 fi
59fi
60
61echo ""
62echo "Resetting staging environment..."
63echo ""
64
65# Load .env for database credentials
66if [ -f .env ]; then
67 # shellcheck disable=SC2046
68 export $(grep -v '^#' .env | grep -v '^\s*$' | xargs)
69fi
70
71DB_NAME="${POSTGRES_DB:-barazo_staging}"
72DB_USER="${POSTGRES_USER:-barazo}"
73
74# Step 1: Stop application services (keep infrastructure running)
75echo "Stopping application services..."
76$COMPOSE_CMD stop barazo-api barazo-web caddy
77
78# Step 2: Drop and recreate database
79echo "Dropping database '$DB_NAME'..."
80$COMPOSE_CMD exec -T postgres psql -U "$DB_USER" -d postgres \
81 -c "DROP DATABASE IF EXISTS \"$DB_NAME\";"
82
83echo "Creating database '$DB_NAME'..."
84$COMPOSE_CMD exec -T postgres psql -U "$DB_USER" -d postgres \
85 -c "CREATE DATABASE \"$DB_NAME\" OWNER \"$DB_USER\";"
86
87# Enable pgvector extension
88echo "Enabling pgvector extension..."
89$COMPOSE_CMD exec -T postgres psql -U "$DB_USER" -d "$DB_NAME" \
90 -c "CREATE EXTENSION IF NOT EXISTS vector;"
91
92# Step 3: Flush Valkey cache
93echo "Flushing Valkey cache..."
94# Use FLUSHALL via direct redis protocol since the command is renamed in production compose.
95# On staging, we restart valkey instead to clear all data.
96$COMPOSE_CMD restart valkey
97
98# Step 4: Restart all services (schema is applied on startup)
99echo "Starting all services..."
100$COMPOSE_CMD up -d
101
102echo ""
103echo "Waiting for services to become healthy..."
104sleep 10
105
106# Check health
107if $COMPOSE_CMD exec -T postgres pg_isready -U "$DB_USER" &>/dev/null; then
108 echo " PostgreSQL: healthy"
109else
110 echo " PostgreSQL: NOT healthy" >&2
111fi
112
113echo ""
114echo "Staging reset complete."
115echo "The database schema will be applied automatically on startup."
116echo ""
117echo "To seed test data, run: ./scripts/seed-staging.sh"