Skip to content

Commit 2619252

Browse files
committed
Add PostgreSQL client and improve database connection handling in deploy scripts
1 parent 140280a commit 2619252

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
- name: Install dependencies
6262
run: |
6363
sudo apt-get update
64-
sudo apt-get install -y rsync zsh curl file golang
64+
sudo apt-get install -y rsync zsh curl file golang postgresql-client
6565
6666
- name: Create .env file from secret
6767
run: |

bin/deploy-with-migrations.sh

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,33 @@ echo -e "${YELLOW}Running deployment script...${NC}"
1515

1616
echo -e "${GREEN}Deployment successful!${NC}"
1717

18+
# Debug: Print environment variables (masked for security)
19+
echo -e "${YELLOW}Checking environment variables...${NC}"
20+
echo "SUPABASE_URL is ${SUPABASE_URL:+set}${SUPABASE_URL:-not set}"
21+
echo "SUPABASE_KEY is ${SUPABASE_KEY:+set}${SUPABASE_KEY:-not set}"
22+
echo "SUPABASE_DB_PASSWORD is ${SUPABASE_DB_PASSWORD:+set}${SUPABASE_DB_PASSWORD:-not set}"
23+
echo "SUPABASE_ACCESS_TOKEN is ${SUPABASE_ACCESS_TOKEN:+set}${SUPABASE_ACCESS_TOKEN:-not set}"
24+
1825
# Run migrations using Supabase CLI
1926
echo -e "${YELLOW}Running database migrations with Supabase CLI...${NC}"
20-
./bin/supabase-db.sh migrate
27+
28+
# Create a temporary .env file with the required credentials
29+
echo "Creating temporary .env file for migrations..."
30+
cat > .env.migration << EOF
31+
SUPABASE_URL=${SUPABASE_URL}
32+
SUPABASE_KEY=${SUPABASE_KEY}
33+
SUPABASE_DB_PASSWORD=${SUPABASE_DB_PASSWORD}
34+
SUPABASE_ACCESS_TOKEN=${SUPABASE_ACCESS_TOKEN}
35+
EOF
36+
37+
# Debug: Check if the temporary .env file was created correctly
38+
echo "Temporary .env file created with $(grep -c '' .env.migration) lines"
39+
40+
# Run migrations with the temporary .env file
41+
SUPABASE_ENV_FILE=.env.migration ./bin/supabase-db.sh migrate
42+
43+
# Remove the temporary .env file
44+
rm -f .env.migration
2145

2246
echo -e "${GREEN}Migrations successful!${NC}"
2347

bin/supabase-db.sh

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

Comments
 (0)