Skip to content

Commit 56e3d95

Browse files
committed
Added static seed
1 parent 0e71d9d commit 56e3d95

6 files changed

Lines changed: 885 additions & 115 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
1212
# Runtime data
1313
pids
1414
*.pid
15-
seed
1615
*.pid.lock
1716

1817
# Directory for instrumented libs generated by jscoverage/JSCover

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ RUN bunx prisma generate
3737
# -----------------------------
3838

3939

40-
FROM deps AS development
40+
FROM base AS development
4141

4242
COPY --from=deps /app/node_modules ./node_modules
4343
COPY --from=deps /app/src/generated ./src/generated
@@ -54,7 +54,7 @@ CMD ["bun", "src/server.ts"]
5454
# -----------------------------
5555

5656

57-
FROM deps AS production
57+
FROM base AS production
5858

5959
RUN addgroup -g 1001 -S nodejs && adduser -S bunjs -u 1001
6060
RUN cp -r /root/.bun /usr/local/bun && chown -R bunjs:nodejs /usr/local/bun

LOCAL_DEVELOPMENT.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This document explains how to set up and manage the local development environmen
55
## Prerequisites
66

77
- **Docker & Docker Compose**: Ensure you have Docker installed and running.
8-
- **PostgreSQL Client (`pg_dump`)**: The setup script uses `pg_dump` to pull data from the remote database.
8+
- **PostgreSQL Client (`pg_dump`)**: Optional. The current local setup prefers loading a local `seed/dump.sql` file. `pg_dump` is only required if you want to create a fresh dump from a remote database manually.
99

1010
## Setup Instructions
1111

@@ -23,7 +23,7 @@ Key variables for the setup script:
2323

2424
### 2. Run the Setup Script
2525

26-
The `scripts/setup-local.sh` script automates the entire process:
26+
The `scripts/setup-local.sh` script automates the local environment bring-up. By default it will load the local `seed/dump.sql` into the local Postgres instance.
2727

2828
```bash
2929
bun run local
@@ -32,10 +32,12 @@ bun run local
3232
#### What the script does:
3333
1. **Starts Postgres**: Launches the `db` container.
3434
2. **Installs Extensions**: Pre-installs `pgcrypto`, `uuid-ossp`, and `pg_stat_statements` into the `public` schema.
35-
3. **Dumps Remote DB**: Uses `pg_dump` to create a snapshot of the production database.
36-
4. **Cleans the Dump**: Strips Supabase-specific extensions and patches schema references to work locally.
37-
5. **Seeds the Database**: Loads the cleaned dump into your local Postgres container.
38-
6. **Starts the API**: Launches the `api` container and waits for it to be healthy.
35+
3. **Loads Local Seed**: If `public` has no tables, the script loads `seed/dump.sql` into the DB. This is the default and recommended local flow.
36+
4. **Starts the API**: Launches the `api` container and waits for it to be healthy.
37+
38+
Flags:
39+
- `--skip-dump`: reuse existing `seed/dump.sql` (no remote dump step)
40+
- `--skip-seed`: skip loading the seed and just start containers
3941

4042
### 3. Useful Commands
4143

@@ -48,11 +50,5 @@ bun run local
4850

4951
## Troubleshooting
5052

51-
### "Tenant or user not found" during dump
52-
This usually means your `SESSION_POOLER` username is just `postgres`. Supabase requires the format `postgres.PROJECT_REF`.
53-
5453
### Tables not in `public` schema
5554
The script automatically patches the dump to ensure tables are placed in the `public` schema. If you manually imported a dump, ensure you've installed the required extensions first.
56-
57-
### Re-seeding
58-
The script skips seeding if it detects existing tables in the `public` schema. To force a re-seed, run `docker compose down -v` before running the setup script.

docker-compose.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ services:
44
build:
55
context: .
66
dockerfile: Dockerfile
7+
target: development
78
ports:
8-
- "3000:3000"
9+
- "127.0.0.1:3000:3000"
10+
env_file:
11+
- .env
912
environment:
1013
NODE_ENV: development
1114
PORT: 3000
@@ -31,7 +34,7 @@ services:
3134
volumes:
3235
- pgdata:/var/lib/postgresql/data
3336
ports:
34-
- "5432:5432"
37+
- "127.0.0.1:5432:5432"
3538
healthcheck:
3639
test: ["CMD", "pg_isready", "-U", "postgres", "-d", "coc", "-h", "127.0.0.1", "-p", "5432"]
3740
interval: 5s

scripts/setup-local.sh

Lines changed: 7 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# --skip-seed Skip seeding entirely (just start containers)
1616
# =============================================================================
1717

18+
!/usr/bin/env bash
1819
set -euo pipefail
1920

2021
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -101,112 +102,20 @@ info "Extensions installed."
101102

102103

103104
# ========================================================
104-
# 4. Check if DB already has data (skip seeding if so)
105+
# 4. Check if seeding is required
105106
# ========================================================
106107
if [[ "$SKIP_SEED" == true ]]; then
107108
warn "--skip-seed passed. Skipping dump and load."
108109
else
109-
TABLE_COUNT=$(docker compose exec -T db psql -U postgres -d coc -tAc \
110-
"SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE';" 2>/dev/null || echo "0")
111-
TABLE_COUNT="${TABLE_COUNT//[[:space:]]/}"
112-
113-
if [[ "$TABLE_COUNT" -gt 0 ]]; then
114-
warn "Database already has $TABLE_COUNT table(s) in public schema. Skipping seed."
115-
warn "To re-seed, run: docker compose down -v then re-run this script."
116-
else
117-
info "Database is empty — proceeding with seed."
118-
119-
120-
# ======================================================
121-
# 5. pg_dump remote database
122-
# ======================================================
123-
command -v pg_dump >/dev/null 2>&1 || error "pg_dump is not installed. Install postgresql-client and retry."
124-
125-
if [[ -n "${SESSION_POOLER:-}" ]]; then
126-
REMOTE_URL="$SESSION_POOLER"
127-
info "Using SESSION_POOLER for database dump"
128-
else
129-
error "Set SESSION_POOLER in $ENV_FILE"
130-
fi
131-
132110
mkdir -p "$SEED_DIR"
111+
if [[ ! -f "$DUMP_FILE" ]]; then
112+
error "$DUMP_FILE not found. Create the seed SQL at $DUMP_FILE and re-run this script."
113+
fi
133114

134-
if [[ "$SKIP_DUMP" == true ]]; then
135-
if [[ ! -f "$DUMP_FILE" ]]; then
136-
error "--skip-dump was passed but $DUMP_FILE does not exist. Run without --skip-dump first."
137-
fi
138-
warn "Skipping pg_dump — reusing existing $DUMP_FILE"
139-
else
140-
info "Dumping remote database to $DUMP_FILE ..."
141-
info "(This may take a moment depending on database size)"
142-
143-
PGDUMP_ERR_LOG="/tmp/pgdump_err_$$.log"
144-
if pg_dump \
145-
--no-owner \
146-
--no-acl \
147-
--if-exists \
148-
--clean \
149-
"$REMOTE_URL" \
150-
> "$DUMP_FILE" 2>"$PGDUMP_ERR_LOG"; then
151-
info "Dump complete: $DUMP_FILE ($(du -sh "$DUMP_FILE" | cut -f1))"
152-
153-
# ---- Strip Supabase-only extensions ----
154-
info "Stripping Supabase-only extensions from dump..."
155-
SUPABASE_EXTS="pg_graphql|pg_net|pgsodium|supabase_vault|wrappers|pg_stat_monitor|hypopg"
156-
157-
sed "${SED_INPLACE[@]}" -E \
158-
"s#^(CREATE EXTENSION IF NOT EXISTS ($SUPABASE_EXTS).*)#-- [local] \1#g" \
159-
"$DUMP_FILE"
160-
161-
sed "${SED_INPLACE[@]}" -E \
162-
"s#^(DROP EXTENSION IF EXISTS ($SUPABASE_EXTS).*)#-- [local] \1#g" \
163-
"$DUMP_FILE"
164-
165-
166-
# Strip COMMENT ON EXTENSION for stripped extensions
167-
sed "${SED_INPLACE[@]}" -E \
168-
"s#^(COMMENT ON EXTENSION ($SUPABASE_EXTS) .*)#-- [local] \1#g" \
169-
"$DUMP_FILE"
170-
info "Supabase-only extension cleanup done."
171-
172-
# ---- Patch extension schema: 'extensions' -> 'public' ----
173-
info "Patching extension schema references (extensions -> public)..."
174-
sed "${SED_INPLACE[@]}" -E \
175-
"s#WITH SCHEMA extensions#WITH SCHEMA public#g" \
176-
"$DUMP_FILE"
177-
info "Extension schema patch done."
178-
179-
else
180-
DUMP_ERR="$(cat "$PGDUMP_ERR_LOG")"
181-
rm -f "$DUMP_FILE" "$PGDUMP_ERR_LOG"
182-
warn "pg_dump failed:"
183-
warn " $DUMP_ERR"
184-
warn ""
185-
if echo "$DUMP_ERR" | grep -qi "tenant or user not found"; then
186-
warn "Tip: The session pooler requires username format: postgres.PROJECT_REF"
187-
warn " e.g. postgres.riqqtbuoaycwwiemnmri (not just: postgres)"
188-
warn " Update SESSION_POOLER in $ENV_FILE."
189-
elif echo "$DUMP_ERR" | grep -qi "network is unreachable\|no route to host"; then
190-
warn "Tip: The host appears unreachable (IPv6-only?). Try a VPN or an IPv4 URL."
191-
fi
192-
warn ""
193-
warn "Options:"
194-
warn " 1. Fix SESSION_POOLER in $ENV_FILE"
195-
warn " 2. Manually copy a dump to $DUMP_FILE and re-run with: bash scripts/setup-local.sh --skip-dump"
196-
error "Aborting: cannot seed without a database dump."
197-
fi
198-
fi # end skip-dump
199-
200-
201-
# ======================================================
202-
# 6. Load dump into running postgres container
203-
# ======================================================
204-
info "Loading dump into postgres..."
115+
info "Loading local seed SQL from $DUMP_FILE ..."
205116
docker compose exec -T db psql -U postgres -d coc < "$DUMP_FILE"
206117
info "Seed complete."
207-
208-
fi # end table count check
209-
fi # end skip-seed
118+
fi
210119

211120

212121
# ========================================================

0 commit comments

Comments
 (0)