处理分支
Learn how to develop and manage your Supabase branches
本指南讲解了如何高效使用 Supabase 分支,包括迁移管理、数据填充行为和开发工作流程。
🌐 This guide covers how to work with Supabase branches effectively, including migration management, seeding behavior, and development workflows.
订阅通知 #
🌐 Subscribing to notifications
当一个操作在持久分支上执行完成时,你可以订阅 webhook 通知。负载格式遵循 webhook 标准。
🌐 You can subscribe to webhook notifications when an action run completes on a persistent branch. The payload format follows the webhook standards.
1{2 "type": "run.completed",3 "timestamp": "2025-10-17T02:27:18.705861793Z",4 "data": {5 "project_ref": "xuqpsshjxdecrwdyuxvs",6 "details_url": "https://supabase.com/dashboard/project/xuqpsshjxdecrwdyuxvs/branches",7 "action_run": {8 "id": "d5f8b4298d0a4d37b99e255c7837e7af",9 "created_at": "2025-10-17T02:27:10.133329324Z",10 "steps": [11 {12 "name": "clone",13 "status": "exited",14 "updated_at": "2025-10-17T02:27:10.788435466Z"15 },16 {17 "name": "pull",18 "status": "exited",19 "updated_at": "2025-10-17T02:27:11.701742857Z"20 },21 {22 "name": "health",23 "status": "exited",24 "updated_at": "2025-10-17T02:27:12.79205717Z"25 },26 {27 "name": "configure",28 "status": "exited",29 "updated_at": "2025-10-17T02:27:13.726839657Z"30 },31 {32 "name": "migrate",33 "status": "exited",34 "updated_at": "2025-10-17T02:27:14.97017507Z"35 },36 {37 "name": "seed",38 "status": "exited",39 "updated_at": "2025-10-17T02:27:15.637684921Z"40 },41 {42 "name": "deploy",43 "status": "exited",44 "updated_at": "2025-10-17T02:27:18.604193114Z"45 }46 ]47 }48 }49}我们建议注册一个单一的 webhooks 处理器,根据负载类型将事件分发到下游服务。最简单的方法是部署一个 Edge Function。例如,下面的 Edge Function 会监听运行完成事件,以通知 Slack 通道。
🌐 We recommend registering a single webhooks processor that dispatches events to downstream services based on the payload type. The easiest way to do that is by deploying an Edge Function. For example, the following Edge Function listens for run completed events to notify a Slack channel.
1// Setup type definitions for built-in Supabase Runtime APIs2import 'jsr:@supabase/functions-js/edge-runtime.d.ts'34console.log('Branching notification booted!')5const slack = Deno.env.get('SLACK_WEBHOOK_URL') ?? ''67Deno.serve(async (request) => {8 const body = await request.json()9 const blocks = [10 {11 type: 'header',12 text: {13 type: 'plain_text',14 text: `Action run ${body.data.action_run.failure ? 'failed' : 'completed'}`,15 emoji: true,16 },17 },18 {19 type: 'section',20 fields: [21 {22 type: 'mrkdwn',23 text: `*Branch ref:*\n${body.data.project_ref}`,24 },25 {26 type: 'mrkdwn',27 text: `*Run ID:*\n${body.data.action_run.id}`,28 },29 ],30 },31 {32 type: 'section',33 fields: [34 {35 type: 'mrkdwn',36 text: `*Started at:*\n${body.data.action_run.created_at}`,37 },38 {39 type: 'mrkdwn',40 text: `*Completed at:*\n${body.timestamp}`,41 },42 ],43 },44 {45 type: 'section',46 text: {47 type: 'mrkdwn',48 text: `<${body.data.details_url}|View logs>`,49 },50 },51 ]52 const resp = await fetch(slack, {53 method: 'POST',54 body: JSON.stringify({55 blocks,56 }),57 })58 const message = await resp.text()59 return new Response(60 JSON.stringify({61 message,62 }),63 {64 status: 200,65 }66 )67})创建一个 Slack webhook URL 并将其设置为函数密钥。
1supabase secrets set --project-ref <branch-ref> SLACK_WEBHOOK_URL=<your-webhook-url>创建并部署一个边缘函数来处理 webhooks。
1supabase functions deploy --project-ref <branch-ref> --use-api notify-slack把目标分支的通知 URL 更新为指向你的 Edge Function。
1supabase branches update <branch-ref> --notify-url https://<branch-ref>.supabase.co/functions/v1/notify-slack完成以上步骤后,每当目标分支上的操作运行完成时,你应该会收到一条 Slack 消息。
🌐 After completing the steps above, you should receive a Slack message whenever an action run completes on your target branch.
迁移和播种行为 #
🌐 Migration and seeding behavior
迁移是按顺序进行的。每次迁移都是在上一次的基础上进行的。
🌐 Migrations are run in sequential order. Each migration builds upon the previous one.
预览分支会记录哪些迁移已经应用过,并且每次提交时只应用新的迁移。这在回滚迁移时可能会造成问题。
🌐 The preview branch has a record of which migrations have been applied, and only applies new migrations for each commit. This can create an issue when rolling back migrations.
使用 ORM 或自定义种子脚本 #
🌐 Using ORM or custom seed scripts
如果你想使用自己的 ORM 来管理迁移和种子脚本,你需要在预览分支准备好之后在 GitHub Actions 中运行它们。可以使用以下示例 GHA 工作流来获取分支凭证。
🌐 If you want to use your own ORM for managing migrations and seed scripts, you will need to run them in GitHub Actions after the preview branch is ready. The branch credentials can be fetched using the following example GHA workflow.
1name: Custom ORM23on:4 pull_request:5 types:6 - opened7 - reopened8 - synchronize9 branches:10 - main11 paths:12 - 'supabase/**'1314jobs:15 wait:16 runs-on: ubuntu-latest17 outputs:18 status: ${{ steps.check.outputs.conclusion }}19 steps:20 - uses: fountainhead/action-wait-for-check@v1.2.021 id: check22 with:23 checkName: Supabase Preview24 ref: ${{ github.event.pull_request.head.sha || github.sha }}25 token: ${{ secrets.GITHUB_TOKEN }}2627 migrate:28 needs:29 - wait30 if: ${{ needs.wait.outputs.status == 'success' }}31 runs-on: ubuntu-latest32 env:33 SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}34 SUPABASE_PROJECT_ID: ${{ secrets.SUPABASE_PROJECT_ID }}35 steps:36 - uses: supabase/setup-cli@v137 with:38 version: latest39 - run: supabase --experimental branches get "$GITHUB_HEAD_REF" -o env >> $GITHUB_ENV40 - name: Custom ORM migration41 run: psql "$POSTGRES_URL_NON_POOLING" -c 'select 1'回滚迁移 #
🌐 Rolling back migrations
你可能想回滚之前迁移中所做的更改。例如,你可能已经推送了一个包含你不再需要的模式更改的迁移文件。
🌐 You might want to roll back changes you've made in an earlier migration change. For example, you may have pushed a migration file containing schema changes you no longer want.
要解决这个问题,先推送最新的更改,然后在 Supabase 中删除预览分支,再重新打开它。
🌐 To fix this, push the latest changes, then delete the preview branch in Supabase and reopen it.
新的预览分支默认是从 ./supabase/seed.sql 文件重新生成的。旧预览分支上做的任何额外数据更改都会丢失。这相当于在本地运行 supabase db reset。所有迁移都会按顺序重新执行。
🌐 The new preview branch is reseeded from the ./supabase/seed.sql file by default. Any additional data changes made on the old preview branch are lost. This is equivalent to running supabase db reset locally. All migrations are rerun in sequential order.
种子行为 #
🌐 Seeding behavior
你的预览分支已使用与本地播种行为相同的示例数据进行初始化。
🌐 Your Preview Branches are seeded with sample data using the same as local seeding behavior.
数据库只会在创建预览分支时初始化一次。要重新执行初始化,只需删除预览分支,然后通过关闭并重新打开你的 Pull Request 来重新创建它。
🌐 The database is only seeded once, when the preview branch is created. To rerun seeding, delete the preview branch and recreate it by closing, and reopening your pull request.
使用分支进行开发 #
🌐 Developing with branches
你可以使用本地或远程开发流程来用分支进行开发。
🌐 You can develop with branches using either local or remote development workflows.
本地开发工作流程 #
🌐 Local development workflow
- 为你的功能创建一个新的 Git 分支
- 使用 Supabase CLI 进行模式更改
- 使用
supabase db diff生成迁移文件 - 在本地测试你的更改
- 提交并推送到 GitHub
- 创建一个拉取请求来生成预览分支
远程开发工作流程 #
🌐 Remote development workflow
- 在 Supabase 仪表板里创建一个预览分支
- 使用分支下拉菜单切换分支
- 在仪表板中进行模式更改
- 使用
supabase db pull在本地拉取更改 - 提交生成的迁移文件
- 推送到你的 Git 仓库
管理分支环境 #
🌐 Managing branch environments
在分支之间切换 #
🌐 Switching between branches
在 Supabase 仪表板中使用分支下拉菜单在不同分支之间切换。每个分支都有自己的:
🌐 Use the branch dropdown in the Supabase dashboard to switch between different branches. Each branch has its own:
- 数据库实例
- API端点
- 身份验证设置
- 存储桶
正在访问分支凭证 #
🌐 Accessing branch credentials
每个分支都有独特的凭证,你可以在仪表板上找到:
🌐 Each branch has unique credentials that you can find in the dashboard:
- 切换到你想要的分支
- 导航到 设置 > API
- 复制分支专用的 URL 和密钥
分支隔离 #
🌐 Branch isolation
各个分支是完全隔离的。在一个分支上做的修改不会影响其他分支,包括:
🌐 Branches are completely isolated from each other. Changes made in one branch don't affect others, including:
- 数据库模式和数据
- 存储对象
- 边缘函数
- 认证配置
下一步 #
🌐 Next steps