# Test each pull request against its own Gregale PostgreSQL branch

> Use a disposable app and PostgreSQL branch per reviewed PR, then prove writes stay isolated.

Run each reviewed pull request in a separate, disposable Gregale app
connected to its own PostgreSQL branch. A write in PR 17 should not show
up in PR 28, and neither app should write to the source database.

A [built-in GitHub PR preview](https://gregale.dev/docs/preview-environments.md) cannot own a
managed PostgreSQL binding. Gregale rejects the attachment with
`409 pr_preview_resource_unsupported`. Use the built-in preview for
checks that do not need a managed database. For a database-backed
rehearsal, follow the separate app workflow below. It does not use the
built-in preview URL or react to GitHub events.

## Start with data that is safe to copy

A [PostgreSQL branch](https://gregale.dev/docs/postgres.md#branches) starts with the source
database's schema and data. Use a disposable source with synthetic rows,
not production or a clone containing customer data. Review the PR code
before deploying it with any credential. Keep the review apps private.

[Clone the example on GitHub](https://github.com/gregale-dev/postgres-pr-branches)
or [download the ZIP](https://gregale.dev/examples/postgres-pr-branches.zip). Run from the example
directory. You'll need the Gregale CLI,
a paid workspace with room for one managed database and two short-lived
branches, and Python 3 for the smoke test. The fixture uses three disposable
apps, so check your app quota too.

Choose unique names. Use a managed database region available to your
workspace; this example uses `us-east-2`.

```sh
SOURCE=review-fixture-source
APP_A=review-fixture-pr-17
APP_B=review-fixture-pr-28
BRANCH_A="$APP_A-db"
BRANCH_B="$APP_B-db"

gregale postgres create "$SOURCE" --region us-east-2 --postgres-major 17 --class development --scale-to-zero
gregale postgres get "$SOURCE"
```

Once the database reports `ready`, use this helper to reserve the app,
apply separate serving and migration bindings, and deploy the example.
Run it from the downloaded directory. Check each command before proceeding
if you adapt this to your own repository.

```sh
deploy_fixture() (
  set -e
  app="$1"
  db="$2"
  gregale deploy --name "$app" --create-only --require-authn
  gregale postgres attach "$db" "$app" --env DATABASE_URL --access read_write
  gregale postgres attach "$db" "$app" --env MIGRATION_DATABASE_URL --access migration
  gregale deploy --name "$app" --require-authn --wait
)

deploy_fixture "$SOURCE" "$SOURCE"
```

The release command creates a table and inserts a row named `seed`.
Only the release task sees `MIGRATION_DATABASE_URL`. The serving app can
read and write rows through `DATABASE_URL` but does not need a
schema-changing credential. Use the URL printed by the deploy and check
that `GET /markers/seed` returns `"present":true` with a key from the
owning account before branching.

## Make one branch per review app

Create the branches after the seed release succeeds. Each branch has a
two-hour TTL. Check that both report `ready` before deploying.

```sh
gregale postgres branch "$SOURCE" "$BRANCH_A" --ttl 2h
gregale postgres branch "$SOURCE" "$BRANCH_B" --ttl 2h
gregale postgres get "$BRANCH_A"
gregale postgres get "$BRANCH_B"

deploy_fixture "$APP_A" "$BRANCH_A"
deploy_fixture "$APP_B" "$BRANCH_B"
```

Use the PR number in each app and database name. For your own app, deploy the
reviewed PR commit from a separate checkout to that PR's disposable app.
Use your repository's migration and smoke tests in place of the example's
seed and marker routes. Never copy the production app's environment or
override the built-in PR preview's resource policy.

## Check that writes do not cross branches

Use the HTTPS `*.gregale.app` URLs printed by the three deployments. Make
a [key from the fixture apps' owning account](https://gregale.dev/docs/auth.md) available to the
local smoke script in `GREGALE_SMOKE_TOKEN`. Do not commit the key or pass
it to the containers.

```sh
python3 smoke.py "https://SOURCE.gregale.app" "https://APP-A.gregale.app" "https://APP-B.gregale.app"
```

The test checks anonymous calls are refused and all three apps can read the
seed. It then makes a distinct write in each review app and asks the other
review app and the source to read it. Only the app that made the write
should find it. The response also confirms the running app cannot see
the migration credential. A green health check alone proves none of this.

**Verified on Gregale:** We deployed the source app and two review apps on
Gregale with CLI v0.1.18-rc.378. Each review app read its own write and
neither could read the other app's write. The source saw neither write.
All three apps used managed PostgreSQL 17 and rejected anonymous access.
This test used ordinary apps; it did not exercise GitHub webhooks or
automatic pairing.

## Clean up when review is done

Close the review loop by deleting the two disposable apps and their
branches. Closing the GitHub PR alone does not delete anything created by
this manual workflow. The TTL is a backstop for the branch, not for the app.
Branch deletion is permanent; use only the disposable names.

```sh
gregale app delete "$APP_A" --delete-bindings --yes
gregale app delete "$APP_B" --delete-bindings --yes
gregale postgres branch delete "$SOURCE" "$BRANCH_A" --yes
gregale postgres branch delete "$SOURCE" "$BRANCH_B" --yes
gregale postgres branch list "$SOURCE"

gregale app delete "$SOURCE" --delete-bindings --yes
gregale postgres delete "$SOURCE" --yes
```

A short-lived branch uses a [separate branch allowance](https://gregale.dev/docs/postgres.md#branches),
but compute and written data still count toward usage.

Source: https://gregale.dev/docs/postgres-branch-per-pr
