Skip to content

Commit c5ab7c0

Browse files
committed
refactor: improve error handling and logging in Supabase database migration script
1 parent 0d0dc34 commit c5ab7c0

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

bin/supabase-db.sh

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,8 @@ setup_project() {
227227
if [ -z "$PROJECT_REF" ]; then
228228
echo -e "${RED}Error: Could not extract project reference from SUPABASE_URL: $SUPABASE_URL${NC}"
229229
echo -e "${YELLOW}SUPABASE_URL should be in the format: https://your-project-ref.supabase.co${NC}"
230-
231-
# Hardcode the project reference as a fallback
232-
PROJECT_REF="arokhsfbkdnfuklmqajh"
233-
echo -e "${YELLOW}Using hardcoded project reference as fallback: $PROJECT_REF${NC}"
230+
echo -e "${RED}Please check your SUPABASE_URL environment variable and try again.${NC}"
231+
exit 1
234232
fi
235233

236234
echo -e "${YELLOW}Project reference: ${PROJECT_REF}${NC}"
@@ -307,23 +305,29 @@ run_migrations() {
307305
if [ -z "$PROJECT_REF" ]; then
308306
echo -e "${RED}Error: Could not extract project reference from SUPABASE_URL: $SUPABASE_URL${NC}"
309307
echo -e "${YELLOW}SUPABASE_URL should be in the format: https://your-project-ref.supabase.co${NC}"
310-
311-
# Hardcode the project reference as a fallback
312-
PROJECT_REF="arokhsfbkdnfuklmqajh"
313-
echo -e "${YELLOW}Using hardcoded project reference as fallback: $PROJECT_REF${NC}"
308+
echo -e "${RED}Please check your SUPABASE_URL environment variable and try again.${NC}"
309+
exit 1
314310
fi
315311

316312
# Step 1: Sync local migration history with remote database
317313
echo -e "${YELLOW}Step 1: Syncing local migration history with remote database...${NC}"
318314
echo -e "${YELLOW}Running: supabase db pull${NC}"
319315

320-
# Capture the output from supabase db pull to parse repair commands
321-
PULL_OUTPUT=$(supabase db pull --schema public,auth,storage,graphql_public,supabase_functions,extensions 2>&1)
316+
# Try to capture output, but also show it in real-time
317+
echo -e "${YELLOW}Attempting to sync migration history...${NC}"
318+
319+
# Create a temporary file to capture output
320+
TEMP_OUTPUT=$(mktemp)
321+
322+
# Run the command and capture both stdout and stderr, while also showing output
323+
supabase db pull --schema public,auth,storage,graphql_public,supabase_functions,extensions 2>&1 | tee "$TEMP_OUTPUT"
322324
PULL_EXIT_CODE=$?
323325

324326
if [ $PULL_EXIT_CODE -ne 0 ]; then
325-
echo -e "${YELLOW}Migration history sync failed. Analyzing output for repair commands...${NC}"
326-
echo "$PULL_OUTPUT"
327+
echo -e "${YELLOW}Migration history sync failed (exit code: $PULL_EXIT_CODE). Analyzing output for repair commands...${NC}"
328+
329+
# Read the captured output
330+
PULL_OUTPUT=$(cat "$TEMP_OUTPUT")
327331

328332
# Extract repair commands from the output
329333
REPAIR_COMMANDS=$(echo "$PULL_OUTPUT" | grep "supabase migration repair" | sed 's/^[[:space:]]*//')
@@ -355,19 +359,27 @@ run_migrations() {
355359
echo -e "${YELLOW}Warning: Sync still failed after repairs. Continuing with migration application...${NC}"
356360
fi
357361
else
358-
echo -e "${YELLOW}No repair commands found in output. Continuing with migration application...${NC}"
362+
echo -e "${YELLOW}No repair commands found in output. This might be a different type of error.${NC}"
363+
echo -e "${YELLOW}Common issues:${NC}"
364+
echo -e "${YELLOW} - Network connectivity problems${NC}"
365+
echo -e "${YELLOW} - Authentication issues${NC}"
366+
echo -e "${YELLOW} - Database connection problems${NC}"
367+
echo -e "${YELLOW}Continuing with migration application...${NC}"
359368
fi
360369
else
361370
echo -e "${GREEN}Migration history synced successfully!${NC}"
362371
fi
363372

373+
# Clean up temporary file
374+
rm -f "$TEMP_OUTPUT"
375+
364376
# Step 2: Apply any pending migrations
365377
echo -e "${YELLOW}Step 2: Applying pending migrations to database...${NC}"
366378
echo "Using database password: ${SUPABASE_DB_PASSWORD:0:3}*****"
367379

368-
# Use the Supabase CLI to push migrations
369-
echo -e "${YELLOW}Running: supabase db push${NC}"
370-
supabase db push
380+
# Use the Supabase CLI to push migrations with --include-all flag
381+
echo -e "${YELLOW}Running: supabase db push --include-all${NC}"
382+
supabase db push --include-all
371383

372384
if [ $? -eq 0 ]; then
373385
echo -e "${GREEN}Migration successful!${NC}"

0 commit comments

Comments
 (0)