Skip to content

Commit 8fe204c

Browse files
committed
feat: add database sync command and improve migration workflow with remote sync step
1 parent dbfe19c commit 8fe204c

File tree

1 file changed

+59
-8
lines changed

1 file changed

+59
-8
lines changed

bin/supabase-db.sh

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ usage() {
1919
echo ""
2020
echo "Commands:"
2121
echo " setup - Install Supabase CLI and link to your cloud project"
22-
echo " migrate - Run migrations on your Supabase database"
22+
echo " migrate - Sync with remote DB and run migrations"
23+
echo " sync - Sync local migration history with remote database"
2324
echo " new NAME - Create a new migration file"
2425
echo " update - Check for Supabase CLI updates and upgrade if available"
2526
echo ""
2627
echo "Example:"
2728
echo " $0 setup"
2829
echo " $0 migrate"
30+
echo " $0 sync"
2931
echo " $0 new add_user_preferences"
3032
echo " $0 update"
3133
exit 1
@@ -284,10 +286,6 @@ run_migrations() {
284286
setup_project
285287
fi
286288

287-
# Run migrations
288-
echo -e "${YELLOW}Applying migrations to database...${NC}"
289-
echo "Using database password: ${SUPABASE_DB_PASSWORD:0:3}*****"
290-
291289
# Make sure we have all environment variables
292290
if [ -z "$SUPABASE_URL" ] || [ -z "$SUPABASE_KEY" ] || [ -z "$SUPABASE_DB_PASSWORD" ] || [ -z "$SUPABASE_ACCESS_TOKEN" ]; then
293291
if [ -f .env ]; then
@@ -315,10 +313,25 @@ run_migrations() {
315313
echo -e "${YELLOW}Using hardcoded project reference as fallback: $PROJECT_REF${NC}"
316314
fi
317315

318-
# Use the Supabase CLI to push migrations without direct database connection
319-
echo -e "${YELLOW}Running migrations using Supabase CLI...${NC}"
316+
# Step 1: Sync local migration history with remote database
317+
echo -e "${YELLOW}Step 1: Syncing local migration history with remote database...${NC}"
318+
echo -e "${YELLOW}Running: supabase db pull${NC}"
319+
320+
# Pull remote migration history to sync local state
321+
supabase db pull --schema public,auth,storage,graphql_public,supabase_functions,extensions
322+
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}"
326+
else
327+
echo -e "${GREEN}Migration history synced successfully!${NC}"
328+
fi
329+
330+
# Step 2: Apply any pending migrations
331+
echo -e "${YELLOW}Step 2: Applying pending migrations to database...${NC}"
332+
echo "Using database password: ${SUPABASE_DB_PASSWORD:0:3}*****"
320333

321-
# Run the command without direct database URL to use the linked project
334+
# Use the Supabase CLI to push migrations
322335
echo -e "${YELLOW}Running: supabase db push${NC}"
323336
supabase db push
324337

@@ -365,6 +378,44 @@ create_migration() {
365378
echo -e "${GREEN}$0 migrate${NC}"
366379
}
367380

381+
# Function to sync with remote database
382+
sync_with_remote() {
383+
echo -e "${YELLOW}Syncing local migration history with remote database...${NC}"
384+
385+
# Check if Supabase CLI is installed
386+
if ! command -v supabase &> /dev/null; then
387+
install_cli
388+
export PATH="$HOME/.local/bin:$PATH"
389+
fi
390+
391+
# Check if Supabase project is initialized
392+
if [ ! -d "supabase" ]; then
393+
echo -e "${YELLOW}Supabase project not initialized. Setting up...${NC}"
394+
setup_project
395+
fi
396+
397+
# Make sure we have all environment variables
398+
if [ -z "$SUPABASE_URL" ] || [ -z "$SUPABASE_KEY" ] || [ -z "$SUPABASE_DB_PASSWORD" ] || [ -z "$SUPABASE_ACCESS_TOKEN" ]; then
399+
if [ -f .env ]; then
400+
echo -e "${YELLOW}Reloading environment variables from .env file...${NC}"
401+
source .env
402+
fi
403+
fi
404+
405+
# Set environment variables for Supabase CLI
406+
export SUPABASE_DB_PASSWORD="$SUPABASE_DB_PASSWORD"
407+
408+
# Pull remote migration history to sync local state
409+
echo -e "${YELLOW}Running: supabase db pull${NC}"
410+
supabase db pull --schema public,auth,storage,graphql_public,supabase_functions,extensions
411+
412+
if [ $? -eq 0 ]; then
413+
echo -e "${GREEN}Migration history synced successfully!${NC}"
414+
else
415+
echo -e "${YELLOW}Warning: Could not sync migration history. This might be expected for new projects.${NC}"
416+
fi
417+
}
418+
368419
# Function to check for updates and upgrade if needed
369420
check_for_updates() {
370421
echo -e "${YELLOW}Checking for Supabase CLI updates...${NC}"

0 commit comments

Comments
 (0)