@@ -173,11 +173,14 @@ setup_project() {
173173
174174 # Load environment variables from .env file if they're not already set
175175 if [ -z " $SUPABASE_URL " ] || [ -z " $SUPABASE_KEY " ] || [ -z " $SUPABASE_DB_PASSWORD " ] || [ -z " $SUPABASE_ACCESS_TOKEN " ]; then
176- if [ -f .env ]; then
177- echo -e " ${YELLOW} Loading environment variables from .env file...${NC} "
178- source .env
176+ # Check for custom env file path
177+ ENV_FILE=" ${SUPABASE_ENV_FILE:- .env} "
178+
179+ if [ -f " $ENV_FILE " ]; then
180+ echo -e " ${YELLOW} Loading environment variables from $ENV_FILE file...${NC} "
181+ source " $ENV_FILE "
179182 else
180- echo -e " ${YELLOW} No .env file found. Checking for environment variables...${NC} "
183+ echo -e " ${YELLOW} No $ENV_FILE file found. Checking for environment variables...${NC} "
181184 fi
182185 fi
183186
@@ -198,6 +201,33 @@ setup_project() {
198201
199202 echo -e " ${YELLOW} Project reference: ${PROJECT_REF}${NC} "
200203
204+ # Test database connection
205+ echo -e " ${YELLOW} Testing database connection...${NC} "
206+
207+ # Create a temporary .pgpass file for the test
208+ PGPASS_FILE=" $HOME /.pgpass"
209+ HOST_PATTERN=" db.${PROJECT_REF} .supabase.co"
210+
211+ # Create .pgpass file with both possible usernames
212+ echo " ${HOST_PATTERN} :5432:postgres:postgres:${SUPABASE_DB_PASSWORD} " > " $PGPASS_FILE "
213+ echo " ${HOST_PATTERN} :5432:postgres:postgres.${PROJECT_REF} :${SUPABASE_DB_PASSWORD} " >> " $PGPASS_FILE "
214+ chmod 600 " $PGPASS_FILE "
215+
216+ # Check if psql is installed
217+ if command -v psql & > /dev/null; then
218+ # Try connection with postgres.{project_ref} user
219+ if psql -h " db.${PROJECT_REF} .supabase.co" -p 5432 -d postgres -U " postgres.${PROJECT_REF} " -c " SELECT 1" > /dev/null 2>&1 ; then
220+ echo -e " ${GREEN} Database connection successful with postgres.${PROJECT_REF} user!${NC} "
221+ # Try connection with postgres user
222+ elif psql -h " db.${PROJECT_REF} .supabase.co" -p 5432 -d postgres -U postgres -c " SELECT 1" > /dev/null 2>&1 ; then
223+ echo -e " ${GREEN} Database connection successful with postgres user!${NC} "
224+ else
225+ echo -e " ${YELLOW} Could not connect to database directly with psql. Continuing with Supabase CLI...${NC} "
226+ fi
227+ else
228+ echo -e " ${YELLOW} psql not installed, skipping direct connection test...${NC} "
229+ fi
230+
201231 # Initialize Supabase project if not already initialized
202232 if [ ! -d " supabase" ]; then
203233 echo -e " ${YELLOW} Initializing Supabase project...${NC} "
@@ -209,7 +239,24 @@ setup_project() {
209239 # Link to existing Supabase project
210240 echo -e " ${YELLOW} Linking to Supabase cloud project...${NC} "
211241 echo " Using database password: ${SUPABASE_DB_PASSWORD: 0: 3} *****"
212- supabase link --project-ref " $PROJECT_REF " --password " $SUPABASE_DB_PASSWORD " --no-keyring --debug
242+
243+ # Create a temporary .pgpass file to avoid password prompt
244+ echo " Creating temporary .pgpass file..."
245+ PGPASS_FILE=" $HOME /.pgpass"
246+
247+ # Extract project reference from URL for the hostname pattern
248+ HOST_PATTERN=" db.${PROJECT_REF} .supabase.co"
249+
250+ # Create or append to .pgpass file
251+ echo " ${HOST_PATTERN} :5432:postgres:postgres:${SUPABASE_DB_PASSWORD} " > " $PGPASS_FILE "
252+ echo " ${HOST_PATTERN} :5432:postgres:postgres.${PROJECT_REF} :${SUPABASE_DB_PASSWORD} " >> " $PGPASS_FILE "
253+
254+ # Set proper permissions
255+ chmod 600 " $PGPASS_FILE "
256+
257+ # Run the command with debug flag
258+ echo " Running supabase link with debug flag..."
259+ supabase link --project-ref " $PROJECT_REF " --no-keyring --debug
213260
214261 echo -e " ${GREEN} Supabase project setup complete!${NC} "
215262}
@@ -233,7 +280,27 @@ run_migrations() {
233280 # Run migrations
234281 echo -e " ${YELLOW} Pushing migrations to database...${NC} "
235282 echo " Using database password: ${SUPABASE_DB_PASSWORD: 0: 3} *****"
236- supabase db push --no-keyring --password " $SUPABASE_DB_PASSWORD " --debug
283+
284+ # Create a temporary .pgpass file to avoid password prompt
285+ echo " Creating temporary .pgpass file..."
286+ PGPASS_FILE=" $HOME /.pgpass"
287+
288+ # Extract project reference from URL for the hostname pattern
289+ HOST_PATTERN=" db.${PROJECT_REF} .supabase.co"
290+
291+ # Create or append to .pgpass file
292+ echo " ${HOST_PATTERN} :5432:postgres:postgres:${SUPABASE_DB_PASSWORD} " > " $PGPASS_FILE "
293+ echo " ${HOST_PATTERN} :5432:postgres:postgres.${PROJECT_REF} :${SUPABASE_DB_PASSWORD} " >> " $PGPASS_FILE "
294+
295+ # Set proper permissions
296+ chmod 600 " $PGPASS_FILE "
297+
298+ # Run the command with debug flag
299+ echo " Running supabase db push with debug flag..."
300+ supabase db push --no-keyring --debug
301+
302+ # Remove the temporary .pgpass file
303+ rm -f " $PGPASS_FILE "
237304
238305 echo -e " ${GREEN} Migrations applied successfully!${NC} "
239306}
0 commit comments