Skip to content

Commit b2a0f3e

Browse files
committed
Improve Supabase deployment with .env support and simplified db migrations
1 parent 0a068a8 commit b2a0f3e

File tree

3 files changed

+89
-75
lines changed

3 files changed

+89
-75
lines changed

bin/deploy-with-migrations.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,30 @@ ssh $SSH_OPTS $REMOTE_USER@$REMOTE_HOST "cd $REMOTE_DIR && chmod +x ./bin/instal
3838

3939
# Run Supabase setup and migrations on the remote server
4040
echo -e "${YELLOW}Running Supabase setup and migrations on remote server...${NC}"
41-
ssh $SSH_OPTS $REMOTE_USER@$REMOTE_HOST "cd $REMOTE_DIR && chmod +x ./bin/supabase-db.sh && ./bin/supabase-db.sh setup && ./bin/supabase-db.sh migrate"
41+
42+
# First, ensure the .env file is properly set up on the remote server
43+
if [ -f .env ]; then
44+
echo -e "${YELLOW}Copying .env file to remote server...${NC}"
45+
scp $SSH_OPTS .env $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/.env
46+
47+
if [ $? -ne 0 ]; then
48+
echo -e "${RED}Failed to copy .env file to remote server. Aborting.${NC}"
49+
exit 1
50+
fi
51+
fi
52+
53+
# Run the Supabase setup
54+
echo -e "${YELLOW}Running Supabase setup on remote server...${NC}"
55+
ssh $SSH_OPTS $REMOTE_USER@$REMOTE_HOST "cd $REMOTE_DIR && chmod +x ./bin/supabase-db.sh && ./bin/supabase-db.sh setup"
56+
57+
if [ $? -ne 0 ]; then
58+
echo -e "${RED}Supabase setup failed. Aborting deployment.${NC}"
59+
exit 1
60+
fi
61+
62+
# Run the Supabase migrations
63+
echo -e "${YELLOW}Running Supabase migrations on remote server...${NC}"
64+
ssh $SSH_OPTS $REMOTE_USER@$REMOTE_HOST "cd $REMOTE_DIR && ./bin/supabase-db.sh migrate"
4265

4366
if [ $? -eq 0 ]; then
4467
echo -e "${GREEN}Migrations successful!${NC}"

bin/install-service.sh

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,18 @@ if [ "$EUID" -eq 0 ]; then
118118
echo 'export PATH="$HOME/.local/bin:$PATH"' | sudo -u "$ORIGINAL_USER" tee -a "/home/$ORIGINAL_USER/.zshrc" > /dev/null
119119
fi
120120

121-
# Run the setup script as the original user
122-
echo -e "${YELLOW}Running Supabase setup as $ORIGINAL_USER...${NC}"
123-
sudo -u "$ORIGINAL_USER" zsh -c "cd \"$PROJECT_DIR\" && chmod +x ./bin/supabase-db.sh && ./bin/supabase-db.sh setup"
121+
# Check if .env file exists
122+
if [ -f "$PROJECT_DIR/.env" ]; then
123+
echo -e "${YELLOW}Found .env file, using for Supabase setup...${NC}"
124+
125+
# Run the setup script as the original user with environment variables from .env
126+
echo -e "${YELLOW}Running Supabase setup as $ORIGINAL_USER...${NC}"
127+
sudo -u "$ORIGINAL_USER" zsh -c "cd \"$PROJECT_DIR\" && source .env && chmod +x ./bin/supabase-db.sh && ./bin/supabase-db.sh setup"
128+
else
129+
echo -e "${RED}No .env file found. Supabase setup may fail without proper credentials.${NC}"
130+
echo -e "${YELLOW}Running Supabase setup as $ORIGINAL_USER...${NC}"
131+
sudo -u "$ORIGINAL_USER" zsh -c "cd \"$PROJECT_DIR\" && chmod +x ./bin/supabase-db.sh && ./bin/supabase-db.sh setup"
132+
fi
124133
else
125134
mkdir -p "$HOME/.local/bin"
126135

@@ -129,9 +138,18 @@ else
129138
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.zshrc"
130139
fi
131140

132-
# Run the setup script
133-
echo -e "${YELLOW}Running Supabase setup...${NC}"
134-
cd "$PROJECT_DIR" && chmod +x ./bin/supabase-db.sh && ./bin/supabase-db.sh setup
141+
# Check if .env file exists
142+
if [ -f "$PROJECT_DIR/.env" ]; then
143+
echo -e "${YELLOW}Found .env file, using for Supabase setup...${NC}"
144+
145+
# Run the setup script with environment variables from .env
146+
echo -e "${YELLOW}Running Supabase setup...${NC}"
147+
cd "$PROJECT_DIR" && source .env && chmod +x ./bin/supabase-db.sh && ./bin/supabase-db.sh setup
148+
else
149+
echo -e "${RED}No .env file found. Supabase setup may fail without proper credentials.${NC}"
150+
echo -e "${YELLOW}Running Supabase setup...${NC}"
151+
cd "$PROJECT_DIR" && chmod +x ./bin/supabase-db.sh && ./bin/supabase-db.sh setup
152+
fi
135153
fi
136154

137155
echo -e "${GREEN}Supabase setup complete.${NC}"

bin/supabase-db.sh

Lines changed: 41 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)