Skip to content

Commit 80a3a68

Browse files
authored
feat: add scripts to copy production data to preview environment (#574)
# Add scripts for copying production data to preview environment This PR adds functionality to copy pgflow data from production to preview environments, which helps with testing and debugging using real-world data. Key additions: - Added two new scripts: - `download-production-dump.sh`: Downloads pgflow schema data from production - `restore-dump-to-preview.sh`: Restores downloaded data to preview environment - Updated documentation in `DEPLOYMENT.md` with instructions for using these scripts - Added `dumps` directory to `.gitignore` to prevent accidental commits of database dumps The scripts handle copying flow definitions, run history, and worker registrations while preserving preview environment secrets and schema structure. This allows developers to test with production data without affecting sensitive information in the preview environment.
1 parent 05738ed commit 80a3a68

4 files changed

Lines changed: 174 additions & 0 deletions

File tree

apps/demo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ Thumbs.db
2222
vite.config.js.timestamp-*
2323
vite.config.ts.timestamp-*
2424
supabase/functions/_vendor/
25+
dumps

apps/demo/DEPLOYMENT.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,38 @@ pnpm supabase migration up --file 20251230202447_20251225163110_pgflow_add_flow_
209209
pnpm wrangler deploy
210210
```
211211

212+
## Copying Production Data to Preview
213+
214+
To copy pgflow data (flows, runs, step states, etc.) from production to preview:
215+
216+
```bash
217+
cd apps/demo
218+
219+
# 1. Download pgflow data from production
220+
./scripts/download-production-dump.sh
221+
# Creates: dumps/production-data-<timestamp>.sql
222+
223+
# 2. Restore to preview (will ask for confirmation)
224+
./scripts/restore-dump-to-preview.sh dumps/production-data-<timestamp>.sql
225+
```
226+
227+
**What gets copied:**
228+
229+
- `pgflow.flows`, `pgflow.steps`, `pgflow.deps` - flow definitions
230+
- `pgflow.runs`, `pgflow.step_states`, `pgflow.step_tasks` - run history
231+
- `pgflow.workers`, `pgflow.worker_functions` - worker registrations
232+
233+
**What stays unchanged:**
234+
235+
- Preview vault secrets (API keys, etc.)
236+
- Schema/migrations - only data is copied
237+
238+
**Requirements:**
239+
240+
- `.env.production` with `SUPABASE_DB_URL` for production
241+
- `.env.preview` with `SUPABASE_DB_URL` for preview
242+
- `pg_dump` and `psql` CLI tools installed
243+
212244
## Troubleshooting
213245

214246
**Edge Functions failing:**
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Download data dump from demo-production
4+
# Run this from apps/demo directory
5+
#
6+
7+
set -euo pipefail
8+
9+
cd "$(dirname "$0")/.."
10+
11+
# Colors for output
12+
GREEN='\033[0;32m'
13+
RED='\033[0;31m'
14+
NC='\033[0m'
15+
16+
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
17+
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
18+
19+
# Check for required tools
20+
if ! command -v pg_dump &> /dev/null; then
21+
log_error "pg_dump not found. Please install PostgreSQL client tools."
22+
exit 1
23+
fi
24+
25+
# Verify env file exists
26+
if [ ! -f .env.production ]; then
27+
log_error ".env.production not found"
28+
exit 1
29+
fi
30+
31+
# Create output directory
32+
mkdir -p dumps
33+
DUMP_FILE="dumps/production-data-$(date +%Y%m%d-%H%M%S).sql"
34+
35+
log_info "Sourcing .env.production..."
36+
set -a; source .env.production; set +a
37+
38+
if [ -z "${SUPABASE_DB_URL:-}" ]; then
39+
log_error "SUPABASE_DB_URL not set in .env.production"
40+
exit 1
41+
fi
42+
43+
log_info "Dumping pgflow schema data from production to $DUMP_FILE"
44+
45+
pg_dump "$SUPABASE_DB_URL" \
46+
--data-only \
47+
--schema=pgflow \
48+
--no-owner \
49+
--no-privileges \
50+
> "$DUMP_FILE"
51+
52+
log_info "Dump completed successfully!"
53+
log_info "File: $DUMP_FILE"
54+
ls -lh "$DUMP_FILE"
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Restore data dump to demo-preview
4+
# Run this from apps/demo directory after downloading dump
5+
#
6+
7+
set -euo pipefail
8+
9+
cd "$(dirname "$0")/.."
10+
11+
# Colors for output
12+
GREEN='\033[0;32m'
13+
RED='\033[0;31m'
14+
YELLOW='\033[1;33m'
15+
NC='\033[0m'
16+
17+
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
18+
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
19+
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
20+
21+
# Check for required tools
22+
if ! command -v psql &> /dev/null; then
23+
log_error "psql not found. Please install PostgreSQL client tools."
24+
exit 1
25+
fi
26+
27+
# Check for dump file
28+
if [ $# -eq 0 ]; then
29+
log_error "Usage: $0 <dump-file.sql>"
30+
log_info "Available dumps in dumps/:"
31+
ls -lh dumps/*.sql 2>/dev/null || log_info " (none)"
32+
exit 1
33+
fi
34+
35+
DUMP_FILE="$1"
36+
37+
if [ ! -f "$DUMP_FILE" ]; then
38+
log_error "Dump file not found: $DUMP_FILE"
39+
exit 1
40+
fi
41+
42+
# Verify env file exists
43+
if [ ! -f .env.preview ]; then
44+
log_error ".env.preview not found"
45+
exit 1
46+
fi
47+
48+
log_info "Sourcing .env.preview..."
49+
set -a; source .env.preview; set +a
50+
51+
if [ -z "${SUPABASE_DB_URL:-}" ]; then
52+
log_error "SUPABASE_DB_URL not set in .env.preview"
53+
exit 1
54+
fi
55+
56+
log_warn "This will REPLACE ALL pgflow schema data in preview"
57+
log_warn "Preview DB: $SUPABASE_DB_URL"
58+
read -p "Continue? (yes/no): " -r
59+
if [[ ! $REPLY =~ ^yes$ ]]; then
60+
log_info "Aborted"
61+
exit 0
62+
fi
63+
64+
log_info "Step 1: Truncating pgflow schema data..."
65+
66+
psql "$SUPABASE_DB_URL" <<'EOSQL'
67+
SET session_replication_role = 'replica';
68+
69+
-- Truncate all pgflow tables (order matters due to FKs, CASCADE handles it)
70+
TRUNCATE TABLE pgflow.step_tasks CASCADE;
71+
TRUNCATE TABLE pgflow.step_states CASCADE;
72+
TRUNCATE TABLE pgflow.runs CASCADE;
73+
TRUNCATE TABLE pgflow.workers CASCADE;
74+
TRUNCATE TABLE pgflow.worker_functions CASCADE;
75+
TRUNCATE TABLE pgflow.deps CASCADE;
76+
TRUNCATE TABLE pgflow.steps CASCADE;
77+
TRUNCATE TABLE pgflow.flows CASCADE;
78+
79+
SET session_replication_role = 'origin';
80+
EOSQL
81+
82+
log_info "Step 2: Loading dump..."
83+
84+
psql "$SUPABASE_DB_URL" < "$DUMP_FILE"
85+
86+
log_info "Restore completed successfully!"
87+
log_info "Preview database now has production pgflow data"

0 commit comments

Comments
 (0)