From 2669f1976fc11cbb65db95715bf70c58427e9dd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Fri, 22 May 2026 16:25:28 +0200 Subject: [PATCH 1/6] Enable previews --- .github/workflows/cleanup-preview.yml | 31 +++++++++++++++++++++ package.json | 1 + scripts/vercel-build.sh | 39 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 .github/workflows/cleanup-preview.yml create mode 100755 scripts/vercel-build.sh diff --git a/.github/workflows/cleanup-preview.yml b/.github/workflows/cleanup-preview.yml new file mode 100644 index 0000000..598cae5 --- /dev/null +++ b/.github/workflows/cleanup-preview.yml @@ -0,0 +1,31 @@ +name: Cleanup Preview Branch + +on: + pull_request: + types: [closed] + +permissions: + contents: read + +jobs: + cleanup: + name: Delete Xata preview branch + runs-on: ubuntu-latest + steps: + - name: Install Xata CLI + run: | + curl -fsSL https://xata.io/install.sh | bash + echo "$HOME/.config/xata/bin" >> $GITHUB_PATH + + - name: Delete preview branch + env: + XATA_API_KEY: ${{ secrets.XATA_API_KEY }} + XATA_ORGANIZATIONID: ${{ secrets.XATA_ORG_ID }} + XATA_PROJECTID: ${{ secrets.XATA_PROJECT_ID }} + REF: ${{ github.event.pull_request.head.ref }} + run: | + set -euo pipefail + SAFE_REF=$(echo -n "$REF" | tr -c 'a-zA-Z0-9-_' '-' | cut -c1-50) + BRANCH_NAME="preview-${SAFE_REF}" + echo "Deleting Xata branch $BRANCH_NAME" + xata branch delete "$BRANCH_NAME" --yes || true diff --git a/package.json b/package.json index 019d893..9293b6c 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "scripts": { "dev": "next dev", "build": "next build", + "vercel-build": "bash scripts/vercel-build.sh", "start": "next start", "lint": "eslint", "db:generate": "drizzle-kit generate", diff --git a/scripts/vercel-build.sh b/scripts/vercel-build.sh new file mode 100755 index 0000000..b2bcade --- /dev/null +++ b/scripts/vercel-build.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# Vercel build entry point. For preview deploys, fork a Xata branch off `main` +# and point DATABASE_URL at it so each PR gets isolated metadata storage. +# Production builds (main) fall through to a plain `next build` that reads the +# DATABASE_URL configured in the Vercel project. + +set -euo pipefail + +# Re-export the runtime env var names under the names the Xata CLI expects, +# so the same secrets configured for the app also drive the CLI here. +export XATA_ORGANIZATIONID="${XATA_ORGANIZATIONID:-${XATA_ORG_ID:-}}" +export XATA_PROJECTID="${XATA_PROJECTID:-${XATA_PROJECT_ID:-}}" + +if ! command -v xata >/dev/null 2>&1; then + curl -fsSL https://xata.io/install.sh | bash + export PATH="$HOME/.config/xata/bin:$PATH" +fi + +xata version + +if [[ "${VERCEL_ENV:-}" == "preview" && -n "${VERCEL_GIT_COMMIT_REF:-}" ]]; then + # Sanitize the git ref into a legal Xata branch name (cap at 50 chars so + # the "preview-" prefix still leaves room under Xata's 63-char limit). + SAFE_REF=$(echo -n "$VERCEL_GIT_COMMIT_REF" | tr -c 'a-zA-Z0-9-_' '-' | cut -c1-50) + BRANCH_NAME="preview-${SAFE_REF}" + + # Recreate from scratch on every push so the preview reflects the PR's + # current schema + seed without drift across rebuilds. + xata branch delete "$BRANCH_NAME" --yes || true + xata branch create --name "$BRANCH_NAME" --parent-branch main + xata branch wait-ready "$BRANCH_NAME" + + DSN=$(xata branch url "$BRANCH_NAME" --type primary) + echo "DATABASE_URL=$DSN" >> .env.production + + DATABASE_URL="$DSN" npm run db:migrate +fi + +npm run build From 5bd3c6e365060dffa0d7ca6a6329f7342b2cf9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Fri, 22 May 2026 16:39:22 +0200 Subject: [PATCH 2/6] update env vars --- scripts/vercel-build.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/vercel-build.sh b/scripts/vercel-build.sh index b2bcade..f2e2a22 100755 --- a/scripts/vercel-build.sh +++ b/scripts/vercel-build.sh @@ -8,8 +8,9 @@ set -euo pipefail # Re-export the runtime env var names under the names the Xata CLI expects, # so the same secrets configured for the app also drive the CLI here. -export XATA_ORGANIZATIONID="${XATA_ORGANIZATIONID:-${XATA_ORG_ID:-}}" -export XATA_PROJECTID="${XATA_PROJECTID:-${XATA_PROJECT_ID:-}}" +export XATA_ORGANIZATIONID="${XATA_ORG_ID}" +export XATA_PROJECTID="${XATA_MAIN_PROJECT_ID}" +export XATA_API_KEY="${XATA_VERCEL_API_KEY}" if ! command -v xata >/dev/null 2>&1; then curl -fsSL https://xata.io/install.sh | bash From 0090fa689da91b37214ff93903a230c845ddf38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Fri, 22 May 2026 16:59:03 +0200 Subject: [PATCH 3/6] fix --- scripts/vercel-build.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/vercel-build.sh b/scripts/vercel-build.sh index f2e2a22..2c630e1 100755 --- a/scripts/vercel-build.sh +++ b/scripts/vercel-build.sh @@ -8,9 +8,9 @@ set -euo pipefail # Re-export the runtime env var names under the names the Xata CLI expects, # so the same secrets configured for the app also drive the CLI here. -export XATA_ORGANIZATIONID="${XATA_ORG_ID}" -export XATA_PROJECTID="${XATA_MAIN_PROJECT_ID}" -export XATA_API_KEY="${XATA_VERCEL_API_KEY}" +export XATA_ORGANIZATIONID="$XATA_ORG_ID" +export XATA_PROJECTID="$XATA_MAIN_PROJECT_ID" +export XATA_API_KEY="$XATA_VERCEL_API_KEY" if ! command -v xata >/dev/null 2>&1; then curl -fsSL https://xata.io/install.sh | bash From 732fb8b52620fc4711c6ef357f01c4f840a2c894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Fri, 22 May 2026 17:04:08 +0200 Subject: [PATCH 4/6] explicit flags --- scripts/vercel-build.sh | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/scripts/vercel-build.sh b/scripts/vercel-build.sh index 2c630e1..eb0aed9 100755 --- a/scripts/vercel-build.sh +++ b/scripts/vercel-build.sh @@ -6,10 +6,7 @@ set -euo pipefail -# Re-export the runtime env var names under the names the Xata CLI expects, -# so the same secrets configured for the app also drive the CLI here. -export XATA_ORGANIZATIONID="$XATA_ORG_ID" -export XATA_PROJECTID="$XATA_MAIN_PROJECT_ID" +# The CLI reads XATA_API_KEY directly from the env; org/project come in via flags. export XATA_API_KEY="$XATA_VERCEL_API_KEY" if ! command -v xata >/dev/null 2>&1; then @@ -25,13 +22,15 @@ if [[ "${VERCEL_ENV:-}" == "preview" && -n "${VERCEL_GIT_COMMIT_REF:-}" ]]; then SAFE_REF=$(echo -n "$VERCEL_GIT_COMMIT_REF" | tr -c 'a-zA-Z0-9-_' '-' | cut -c1-50) BRANCH_NAME="preview-${SAFE_REF}" + XATA_FLAGS=(--organization "$XATA_ORG_ID" --project "$XATA_MAIN_PROJECT_ID") + # Recreate from scratch on every push so the preview reflects the PR's # current schema + seed without drift across rebuilds. - xata branch delete "$BRANCH_NAME" --yes || true - xata branch create --name "$BRANCH_NAME" --parent-branch main - xata branch wait-ready "$BRANCH_NAME" + xata branch delete "$BRANCH_NAME" "${XATA_FLAGS[@]}" --yes || true + xata branch create --name "$BRANCH_NAME" --parent-branch main "${XATA_FLAGS[@]}" + xata branch wait-ready "$BRANCH_NAME" "${XATA_FLAGS[@]}" - DSN=$(xata branch url "$BRANCH_NAME" --type primary) + DSN=$(xata branch url "$BRANCH_NAME" --type primary "${XATA_FLAGS[@]}") echo "DATABASE_URL=$DSN" >> .env.production DATABASE_URL="$DSN" npm run db:migrate From cc0d90004e47214edc56e645716f8e1ecb83ec52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Mon, 25 May 2026 10:34:03 +0200 Subject: [PATCH 5/6] debug --- .github/workflows/cleanup-preview.yml | 9 ++++++--- scripts/vercel-build.sh | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cleanup-preview.yml b/.github/workflows/cleanup-preview.yml index 598cae5..7b0e388 100644 --- a/.github/workflows/cleanup-preview.yml +++ b/.github/workflows/cleanup-preview.yml @@ -20,12 +20,15 @@ jobs: - name: Delete preview branch env: XATA_API_KEY: ${{ secrets.XATA_API_KEY }} - XATA_ORGANIZATIONID: ${{ secrets.XATA_ORG_ID }} - XATA_PROJECTID: ${{ secrets.XATA_PROJECT_ID }} + XATA_ORG_ID: ${{ secrets.XATA_ORG_ID }} + XATA_MAIN_PROJECT_ID: ${{ secrets.XATA_MAIN_PROJECT_ID }} REF: ${{ github.event.pull_request.head.ref }} run: | set -euo pipefail SAFE_REF=$(echo -n "$REF" | tr -c 'a-zA-Z0-9-_' '-' | cut -c1-50) BRANCH_NAME="preview-${SAFE_REF}" echo "Deleting Xata branch $BRANCH_NAME" - xata branch delete "$BRANCH_NAME" --yes || true + xata branch delete "$BRANCH_NAME" \ + --organization "$XATA_ORG_ID" \ + --project "$XATA_MAIN_PROJECT_ID" \ + --yes || true diff --git a/scripts/vercel-build.sh b/scripts/vercel-build.sh index eb0aed9..0a0cf6b 100755 --- a/scripts/vercel-build.sh +++ b/scripts/vercel-build.sh @@ -27,6 +27,7 @@ if [[ "${VERCEL_ENV:-}" == "preview" && -n "${VERCEL_GIT_COMMIT_REF:-}" ]]; then # Recreate from scratch on every push so the preview reflects the PR's # current schema + seed without drift across rebuilds. xata branch delete "$BRANCH_NAME" "${XATA_FLAGS[@]}" --yes || true + echo xata branch create --name "$BRANCH_NAME" --parent-branch main "${XATA_FLAGS[@]}" xata branch create --name "$BRANCH_NAME" --parent-branch main "${XATA_FLAGS[@]}" xata branch wait-ready "$BRANCH_NAME" "${XATA_FLAGS[@]}" From 647258ee147051d945c689878d162122dda78210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Mon, 25 May 2026 10:38:14 +0200 Subject: [PATCH 6/6] remove debug --- scripts/vercel-build.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/vercel-build.sh b/scripts/vercel-build.sh index 0a0cf6b..0334367 100755 --- a/scripts/vercel-build.sh +++ b/scripts/vercel-build.sh @@ -27,8 +27,7 @@ if [[ "${VERCEL_ENV:-}" == "preview" && -n "${VERCEL_GIT_COMMIT_REF:-}" ]]; then # Recreate from scratch on every push so the preview reflects the PR's # current schema + seed without drift across rebuilds. xata branch delete "$BRANCH_NAME" "${XATA_FLAGS[@]}" --yes || true - echo xata branch create --name "$BRANCH_NAME" --parent-branch main "${XATA_FLAGS[@]}" - xata branch create --name "$BRANCH_NAME" --parent-branch main "${XATA_FLAGS[@]}" + xata branch create --name "$BRANCH_NAME" "${XATA_FLAGS[@]}" xata branch wait-ready "$BRANCH_NAME" "${XATA_FLAGS[@]}" DSN=$(xata branch url "$BRANCH_NAME" --type primary "${XATA_FLAGS[@]}")