@@ -317,12 +317,46 @@ run_migrations() {
317317 echo -e " ${YELLOW} Step 1: Syncing local migration history with remote database...${NC} "
318318 echo -e " ${YELLOW} Running: supabase db pull${NC} "
319319
320- # Pull remote migration history to sync local state
321- supabase db pull --schema public,auth,storage,graphql_public,supabase_functions,extensions
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 )
322+ PULL_EXIT_CODE=$?
322323
323- if [ $? -ne 0 ]; then
324- echo -e " ${YELLOW} Warning: Could not sync migration history. This might be expected for new projects.${NC} "
325- echo -e " ${YELLOW} Continuing with migration application...${NC} "
324+ 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+
328+ # Extract repair commands from the output
329+ REPAIR_COMMANDS=$( echo " $PULL_OUTPUT " | grep " supabase migration repair" | sed ' s/^[[:space:]]*//' )
330+
331+ if [ -n " $REPAIR_COMMANDS " ]; then
332+ echo -e " ${YELLOW} Found repair commands. Executing them...${NC} "
333+
334+ # Execute each repair command
335+ echo " $REPAIR_COMMANDS " | while IFS= read -r repair_cmd; do
336+ if [ -n " $repair_cmd " ]; then
337+ echo -e " ${YELLOW} Running: $repair_cmd ${NC} "
338+ eval " $repair_cmd "
339+
340+ if [ $? -eq 0 ]; then
341+ echo -e " ${GREEN} Repair command executed successfully${NC} "
342+ else
343+ echo -e " ${RED} Repair command failed: $repair_cmd ${NC} "
344+ fi
345+ fi
346+ done
347+
348+ # Try pulling again after repairs
349+ echo -e " ${YELLOW} Retrying sync after repairs...${NC} "
350+ supabase db pull --schema public,auth,storage,graphql_public,supabase_functions,extensions
351+
352+ if [ $? -eq 0 ]; then
353+ echo -e " ${GREEN} Migration history synced successfully after repairs!${NC} "
354+ else
355+ echo -e " ${YELLOW} Warning: Sync still failed after repairs. Continuing with migration application...${NC} "
356+ fi
357+ else
358+ echo -e " ${YELLOW} No repair commands found in output. Continuing with migration application...${NC} "
359+ fi
326360 else
327361 echo -e " ${GREEN} Migration history synced successfully!${NC} "
328362 fi
@@ -456,6 +490,9 @@ case "$1" in
456490 migrate)
457491 run_migrations
458492 ;;
493+ sync)
494+ sync_with_remote
495+ ;;
459496 new)
460497 if [ $# -lt 2 ]; then
461498 echo -e " ${RED} Error: Migration name is required.${NC} "
0 commit comments