@@ -42,7 +42,7 @@ install_cli() {
4242 return 0
4343 fi
4444
45- SUPABASE_VERSION=" 2.20.12" # Known working version
45+ SUPABASE_VERSION=" 2.20.12" # Latest stable version that works with cloud
4646
4747 # Compile from source using ZIP file
4848 echo " Compiling Supabase CLI from source..."
@@ -185,9 +185,6 @@ setup_project() {
185185
186186 echo -e " ${YELLOW} Project reference: ${PROJECT_REF}${NC} "
187187
188- # Skip direct database connection test and proceed with Supabase CLI
189- echo -e " ${YELLOW} Using Supabase CLI for database operations...${NC} "
190-
191188 # Initialize Supabase project if not already initialized
192189 if [ ! -d " supabase" ]; then
193190 echo -e " ${YELLOW} Initializing Supabase project...${NC} "
@@ -202,42 +199,21 @@ setup_project() {
202199
203200 # Set environment variables for Supabase CLI
204201 export SUPABASE_DB_PASSWORD=" $SUPABASE_DB_PASSWORD "
202+ export SUPABASE_ACCESS_TOKEN=" $SUPABASE_ACCESS_TOKEN "
205203
206- # Run the command with retry logic
207- echo " Running supabase link with retry logic..."
208- MAX_RETRIES=3
209- RETRY_COUNT=0
210- SUCCESS=false
204+ # Set up environment variables for Supabase CLI
205+ export PGPASSWORD=" $SUPABASE_DB_PASSWORD "
211206
212- while [ $RETRY_COUNT -lt $MAX_RETRIES ] && [ " $SUCCESS " = " false" ]; do
213- echo " Attempt $(( $RETRY_COUNT + 1 )) of $MAX_RETRIES ..."
214-
215- # Add a delay between retries
216- if [ $RETRY_COUNT -gt 0 ]; then
217- SLEEP_TIME=$(( $RETRY_COUNT * 5 ))
218- echo " Waiting $SLEEP_TIME seconds before retry..."
219- sleep $SLEEP_TIME
220- fi
221-
222- # Run the command with timeout to prevent hanging
223- # Set up PGPASSWORD environment variable
224- export PGPASSWORD=" $SUPABASE_DB_PASSWORD "
225-
226- # Run the command without --password flag
227- timeout 60 supabase link --project-ref " $PROJECT_REF " --debug
228-
229- if [ $? -eq 0 ]; then
230- SUCCESS=true
231- echo -e " ${GREEN} Link successful!${NC} "
232- else
233- RETRY_COUNT=$(( $RETRY_COUNT + 1 ))
234- echo -e " ${YELLOW} Link failed. Retrying...${NC} "
235- fi
236- done
207+ # Run the link command directly - no retries to avoid confusion
208+ echo -e " ${YELLOW} Running: supabase link --project-ref \" $PROJECT_REF \" --password \" $SUPABASE_DB_PASSWORD \" --debug${NC} "
209+ supabase link --project-ref " $PROJECT_REF " --password " $SUPABASE_DB_PASSWORD " --debug
237210
238- if [ " $SUCCESS " = " false" ]; then
239- echo -e " ${RED} Link failed after $MAX_RETRIES attempts.${NC} "
211+ if [ $? -eq 0 ]; then
212+ echo -e " ${GREEN} Link successful!${NC} "
213+ else
214+ echo -e " ${RED} Link failed.${NC} "
240215 echo -e " ${RED} Please check your Supabase configuration and try again.${NC} "
216+ echo -e " ${YELLOW} Make sure your SUPABASE_ACCESS_TOKEN and SUPABASE_DB_PASSWORD are correct.${NC} "
241217 exit 1
242218 fi
243219
@@ -267,41 +243,38 @@ run_migrations() {
267243 # Set environment variables for Supabase CLI
268244 export SUPABASE_DB_PASSWORD=" $SUPABASE_DB_PASSWORD "
269245
270- # Run the command with retry logic
271- echo " Running supabase migration up with retry logic..."
272- MAX_RETRIES=3
273- RETRY_COUNT=0
274- SUCCESS=false
246+ # Extract project reference from URL if not already done
247+ if [ -z " $PROJECT_REF " ]; then
248+ PROJECT_REF=$( echo $SUPABASE_URL | sed -E ' s/.*\/\/([^.]+).*/\1/' )
249+ fi
275250
276- while [ $RETRY_COUNT -lt $MAX_RETRIES ] && [ " $SUCCESS " = " false" ]; do
277- echo " Attempt $(( $RETRY_COUNT + 1 )) of $MAX_RETRIES ..."
278-
279- # Add a delay between retries
280- if [ $RETRY_COUNT -gt 0 ]; then
281- SLEEP_TIME=$(( $RETRY_COUNT * 5 ))
282- echo " Waiting $SLEEP_TIME seconds before retry..."
283- sleep $SLEEP_TIME
284- fi
285-
286- # Run the command with timeout to prevent hanging
287- # Set up PGPASSWORD environment variable
288- export PGPASSWORD=" $SUPABASE_DB_PASSWORD "
289-
290- # Run the command without --password flag
291- timeout 60 supabase migration up --debug
292-
293- if [ $? -eq 0 ]; then
294- SUCCESS=true
295- echo -e " ${GREEN} Migration successful!${NC} "
296- else
297- RETRY_COUNT=$(( $RETRY_COUNT + 1 ))
298- echo -e " ${YELLOW} Migration failed. Retrying...${NC} "
299- fi
300- done
251+ # Ensure we have the project reference
252+ if [ -z " $PROJECT_REF " ]; then
253+ echo -e " ${RED} Error: Could not extract project reference from SUPABASE_URL.${NC} "
254+ echo -e " ${YELLOW} SUPABASE_URL should be in the format: https://your-project-ref.supabase.co${NC} "
255+ exit 1
256+ fi
257+
258+ # Set up the database URL for direct cloud connection
259+ DB_URL=" postgresql://postgres:${SUPABASE_DB_PASSWORD} @db.${PROJECT_REF} .supabase.co:5432/postgres"
260+ echo -e " ${YELLOW} Using cloud database URL for migrations...${NC} "
261+
262+ # Run the migration command directly with the cloud database URL
263+ echo -e " ${YELLOW} Running migrations on cloud database...${NC} "
301264
302- if [ " $SUCCESS " = " false" ]; then
303- echo -e " ${RED} Migration failed after $MAX_RETRIES attempts.${NC} "
265+ # Set up PGPASSWORD environment variable
266+ export PGPASSWORD=" $SUPABASE_DB_PASSWORD "
267+
268+ # Run the command with the cloud database URL - no retries to avoid confusion
269+ echo -e " ${YELLOW} Running: supabase db push --db-url <DB_URL> --debug${NC} "
270+ supabase db push --db-url " $DB_URL " --debug
271+
272+ if [ $? -eq 0 ]; then
273+ echo -e " ${GREEN} Migration successful!${NC} "
274+ else
275+ echo -e " ${RED} Migration failed.${NC} "
304276 echo -e " ${RED} Please check your Supabase configuration and try again.${NC} "
277+ echo -e " ${YELLOW} Make sure your SUPABASE_DB_PASSWORD is correct and your IP is allowed to access the database.${NC} "
305278 exit 1
306279 fi
307280
0 commit comments