diff --git a/.superset/setup.sh b/.superset/setup.sh index 863f3d03999..128059ac4e5 100755 --- a/.superset/setup.sh +++ b/.superset/setup.sh @@ -10,6 +10,13 @@ success() { echo -e "${GREEN}โœ“${NC} $1"; } echo "๐Ÿš€ Setting up Superset workspace..." +# Load root .env for this script (provides NEON_PROJECT_ID, etc.) +if [ -n "$SUPERSET_ROOT_PATH" ] && [ -f "$SUPERSET_ROOT_PATH/.env" ]; then + set -a + source "$SUPERSET_ROOT_PATH/.env" + set +a +fi + # Check dependencies command -v bun &> /dev/null || error "Bun not installed. Install from https://bun.sh" command -v neonctl &> /dev/null || error "Neon CLI not installed. Run: npm install -g neonctl" @@ -24,38 +31,48 @@ echo "๐Ÿ“ฅ Installing dependencies..." bun install success "Dependencies installed" -# Link direnv config from root repo if it exists -if [ -n "$SUPERSET_ROOT_PATH" ] && [ -f "$SUPERSET_ROOT_PATH/.envrc" ]; then - echo "๐Ÿ”ง Linking .envrc..." - ln -sf "$SUPERSET_ROOT_PATH/.envrc" .envrc +# Create .envrc for direnv +if [ ! -f .envrc ]; then + echo "๐Ÿ”ง Creating .envrc..." + cat > .envrc << 'ENVRC' +#!/usr/bin/env bash +dotenv .env +ENVRC if command -v direnv &> /dev/null; then direnv allow fi success "direnv configured" fi -# Create Neon branch for this workspace -echo "๐Ÿ—„๏ธ Creating Neon branch..." +# Create or get Neon branch for this workspace WORKSPACE_NAME="${SUPERSET_WORKSPACE_NAME:-$(basename "$PWD")}" -NEON_OUTPUT=$(neonctl branches create \ - --project-id "$NEON_PROJECT_ID" \ - --name "$WORKSPACE_NAME" \ - --output json) - -# Parse connection strings from create output -BRANCH_ID=$(echo "$NEON_OUTPUT" | jq -r '.branch.id') -DIRECT_URL=$(echo "$NEON_OUTPUT" | jq -r '.connection_uris[0].connection_uri') -POOLER_HOST=$(echo "$NEON_OUTPUT" | jq -r '.connection_uris[0].connection_parameters.pooler_host') -PASSWORD=$(echo "$NEON_OUTPUT" | jq -r '.connection_uris[0].connection_parameters.password') -ROLE=$(echo "$NEON_OUTPUT" | jq -r '.connection_uris[0].connection_parameters.role') -DATABASE=$(echo "$NEON_OUTPUT" | jq -r '.connection_uris[0].connection_parameters.database') -POOLED_URL="postgresql://${ROLE}:${PASSWORD}@${POOLER_HOST}/${DATABASE}?sslmode=require" - -cat >> .env << EOF -NEON_BRANCH_ID=$BRANCH_ID -DATABASE_URL=$POOLED_URL -DATABASE_URL_UNPOOLED=$DIRECT_URL -EOF + +# Check if branch already exists +EXISTING_BRANCH=$(neonctl branches list --project-id "$NEON_PROJECT_ID" --output json | jq -r ".[] | select(.name == \"$WORKSPACE_NAME\") | .id") + +if [ -n "$EXISTING_BRANCH" ]; then + echo "๐Ÿ—„๏ธ Using existing Neon branch..." + BRANCH_ID="$EXISTING_BRANCH" + # Get connection strings for existing branch + DIRECT_URL=$(neonctl connection-string "$EXISTING_BRANCH" --project-id "$NEON_PROJECT_ID") + POOLED_URL=$(neonctl connection-string "$EXISTING_BRANCH" --project-id "$NEON_PROJECT_ID" --pooled) +else + echo "๐Ÿ—„๏ธ Creating Neon branch..." + NEON_OUTPUT=$(neonctl branches create \ + --project-id "$NEON_PROJECT_ID" \ + --name "$WORKSPACE_NAME" \ + --output json) + BRANCH_ID=$(echo "$NEON_OUTPUT" | jq -r '.branch.id') + # Get connection strings for new branch + DIRECT_URL=$(neonctl connection-string "$BRANCH_ID" --project-id "$NEON_PROJECT_ID") + POOLED_URL=$(neonctl connection-string "$BRANCH_ID" --project-id "$NEON_PROJECT_ID" --pooled) +fi + +# Copy root .env and override with branch-specific values +cp "$SUPERSET_ROOT_PATH/.env" .env +sed -i '' "s|^DATABASE_URL=.*|DATABASE_URL=$POOLED_URL|" .env +sed -i '' "s|^DATABASE_URL_UNPOOLED=.*|DATABASE_URL_UNPOOLED=$DIRECT_URL|" .env +echo "NEON_BRANCH_ID=$BRANCH_ID" >> .env success "Neon branch created: $WORKSPACE_NAME" echo "โœจ Done!" diff --git a/.superset/teardown.sh b/.superset/teardown.sh index 2b4bf203171..b22fb117b13 100755 --- a/.superset/teardown.sh +++ b/.superset/teardown.sh @@ -10,6 +10,14 @@ success() { echo -e "${GREEN}โœ“${NC} $1"; } echo "๐Ÿงน Tearing down Superset workspace..." +# Load local .env +if [ -f ".env" ]; then + # shellcheck disable=SC1091 + set -a + source .env + set +a +fi + # Check dependencies command -v neonctl &> /dev/null || error "Neon CLI not installed. Run: npm install -g neonctl" @@ -17,17 +25,14 @@ command -v neonctl &> /dev/null || error "Neon CLI not installed. Run: npm insta NEON_PROJECT_ID="${NEON_PROJECT_ID:-}" [ -z "$NEON_PROJECT_ID" ] && error "NEON_PROJECT_ID environment variable is required" -# Delete Neon branch for this workspace -WORKSPACE_NAME="${SUPERSET_WORKSPACE_NAME:-$(basename "$PWD")}" -if [ -f ".env" ]; then - # shellcheck disable=SC1091 - source .env -fi BRANCH_ID="${NEON_BRANCH_ID:-}" if [ -z "$BRANCH_ID" ]; then error "No NEON_BRANCH_ID found in .env; cannot delete branch" fi +# Delete Neon branch for this workspace +WORKSPACE_NAME="${SUPERSET_WORKSPACE_NAME:-$(basename "$PWD")}" + echo "๐Ÿ—„๏ธ Deleting Neon branch: $WORKSPACE_NAME ($BRANCH_ID)" if neonctl branches delete "$BRANCH_ID" --project-id "$NEON_PROJECT_ID" --force 2>/dev/null; then success "Neon branch deleted: $WORKSPACE_NAME"