Use case: configuring CI/CD (using GitHub actions) to create temporary branch on push → confirm migrations apply to branch (using Prisma migrate) → apply migrations to root branch if successful.
(Actual implementation more complex with dev + production environments, but that’s the basic idea)
Inspired by the example at GitHub - kelvich/branching_demo_ts (as demoed in Nikita’s Prisma Day talk)
Things I discovered so far:
-
V1 API (as used in demo repo) creates branches that appear as projects rather than branches (I’m guessing this is the older behaviour prior to the most recent branching beta release last week?)
-
→ tried the V2 API, which successfully creates branches as per web interface.
-
→ V2 API requires additional work to achieve the same result vs. V1 API:
# V1
curl -o - -X POST https://console.stage.neon.tech/api/v1/projects/$PROJECT/branches \
-H "Authorization: Bearer $API_TOKEN" |
jq -r '.id'
# V2
curl -o - -X POST https://console.neon.tech/api/v2/projects/$PROJECT/branches \
-H "Authorization: Bearer $NEON_API_TOKEN" \
-H 'Content-Type: application/json' \
# POST body now required
#
# i. calling from shell: need to wrap string in double quotes for env vars to be parsed
# and therefore escape double quotes within JSON string
#
# ii. need to specify parent_id - doesn't default to root like v1 API
# -> need to obtain root branch ID via API GET projects/.../branches
# OR from URL for 'main' branch in web interface (can't see it elsewhere)
#
# iii. need to specify branch name - doesn't auto-create like v1 API
# -> must not exist already
# -> user now needs to find a method to create unique ID
# (here using $GITHUB_RUN_* env vars within GitHub Actions)
-d "{\"branch\":{\"parent_id\":\"$PARENT_BRANCH_ID\", \"name\": \"ci-actions/runs/$GITHUB_RUN_ID#$GITHUB_RUN_ATTEMPT\"}}"
Now working - but would have required less work if there was an API that creates new-style branches with simplicity of V1 API (defaulting parent_id to root and auto-creating branch name if not specified - for “throwaway” test branches).
-
Wish: Branch rebase feature [maybe - not sure whether I’d actually have a need for use this yet - still figuring how best to implement] - so I can update a branch’s data in-place to a newer version of the original parent branch without having to create a new branch. (New branches require updated host name in connection string, which would require redeploying test environments with updated env variable).
-
Endpoint for connecting to newly-created branch:
V1 API demo used${{ branch.id }}.cloud.stage.neon.tech
. → removed.stage.
to make it work via${{ branch.id }}.cloud.neon.tech
.
Doesn’t work with V2 branches → connection errors with “Console request failed” (doesn’t explain cause of error/my mistake).
Realised I need to construct different endpoint for V2:"${{ branch.endpoints[0].id }}.${{ branch.endpoints[0].proxy_host }}"
Would be helpful/less work if: 1) full endpoint host name returned in V2 API result; 2) easy/predictable way to get working host name just using branch ID/single toplevel response field without having to drill into array of multiple potential endpoints, as with V1.
Hope helpful to share!