|
15 | 15 | # --skip-seed Skip seeding entirely (just start containers) |
16 | 16 | # ============================================================================= |
17 | 17 |
|
| 18 | +!/usr/bin/env bash |
18 | 19 | set -euo pipefail |
19 | 20 |
|
20 | 21 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
@@ -101,112 +102,20 @@ info "Extensions installed." |
101 | 102 |
|
102 | 103 |
|
103 | 104 | # ======================================================== |
104 | | -# 4. Check if DB already has data (skip seeding if so) |
| 105 | +# 4. Check if seeding is required |
105 | 106 | # ======================================================== |
106 | 107 | if [[ "$SKIP_SEED" == true ]]; then |
107 | 108 | warn "--skip-seed passed. Skipping dump and load." |
108 | 109 | 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 | | - |
132 | 110 | 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 |
133 | 114 |
|
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 ..." |
205 | 116 | docker compose exec -T db psql -U postgres -d coc < "$DUMP_FILE" |
206 | 117 | info "Seed complete." |
207 | | - |
208 | | - fi # end table count check |
209 | | -fi # end skip-seed |
| 118 | +fi |
210 | 119 |
|
211 | 120 |
|
212 | 121 | # ======================================================== |
|
0 commit comments