|
| 1 | +--- |
| 2 | +title: State-based Schema Management with GitHub Actions and AI Review |
| 3 | +author: Adela |
| 4 | +updated_at: 2025/12/12 18:00 |
| 5 | +tags: Tutorial |
| 6 | +integrations: GitHub |
| 7 | +category: Tutorial |
| 8 | +level: Advanced |
| 9 | +estimated_time: '45 mins' |
| 10 | +--- |
| 11 | + |
| 12 | +import CreateServiceAccountGitOps from '/snippets/tutorials/create-service-account-gitops.mdx'; |
| 13 | +import ConfigSQLReview from '/snippets/tutorials/config-sql-review.mdx'; |
| 14 | + |
| 15 | +This is part of our database deployment series with Bytebase: |
| 16 | + |
| 17 | +- [Database GitOps with GitHub Actions](/tutorials/gitops-github-workflow) |
| 18 | +- [Database GitOps with Azure DevOps Pipeline](/tutorials/gitops-azure-devops-workflow) |
| 19 | +- [Database GitOps with GitLab CI](/tutorials/gitops-gitlab-workflow) |
| 20 | +- [Database GitOps with Bitbucket Pipelines](/tutorials/gitops-bitbucket-workflow) |
| 21 | +- State-based Database Schema Management with GitHub Actions (this one) |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +This tutorial shows you how to build a database state-based workflow using GitHub Actions and Bytebase API for PostgreSQL databases. Unlike migration-based approaches that track incremental changes, state-based workflows declare the desired final state of your database schema and let the system determine the necessary migrations. You'll learn to: |
| 26 | + |
| 27 | +1. Create a state-based database workflow where you can: |
| 28 | + |
| 29 | + - Define your desired database schema state in declarative SQL files |
| 30 | + - Automatically generate migration scripts based on state differences |
| 31 | + - Run SQL reviews on pull requests for generated migrations |
| 32 | + - Auto-deploy changes when merging to `main` |
| 33 | + |
| 34 | +1. Handle schema drift detection and remediation |
| 35 | + |
| 36 | +1. Deploy state changes via ChatOps-style PR comments |
| 37 | + |
| 38 | +<Info> |
| 39 | + |
| 40 | +**Important:** State-based workflow currently only supports PostgreSQL. |
| 41 | + |
| 42 | +</Info> |
| 43 | + |
| 44 | +## Repository |
| 45 | + |
| 46 | +[https://github.com/bytebase/example-gitops-github-flow](https://github.com/bytebase/example-gitops-github-flow) |
| 47 | + |
| 48 | +## Prerequisites |
| 49 | + |
| 50 | +- A Bytebase instance (Bytebase Cloud or self-hosted) |
| 51 | +- For self-hosted version, you need [Docker](https://www.docker.com/) to run Bytebase. |
| 52 | + |
| 53 | +## State-based vs Migration-based |
| 54 | + |
| 55 | +**Migration-based**: Write incremental scripts that transform the database step-by-step. You track which migrations have run. |
| 56 | + |
| 57 | +**State-based**: Declare the desired schema state. The system automatically generates migrations by comparing current and desired states. |
| 58 | + |
| 59 | +## Prepare the Environment |
| 60 | + |
| 61 | +### Step 1 - Set up Bytebase |
| 62 | + |
| 63 | +import BytebaseSetupOptions from '/snippets/tutorials/bytebase-setup-options.mdx'; |
| 64 | + |
| 65 | +<BytebaseSetupOptions /> |
| 66 | + |
| 67 | +### Step 2 - Create Service Account |
| 68 | + |
| 69 | +<CreateServiceAccountGitOps /> |
| 70 | + |
| 71 | +### Step 3 - Fork the Example Repository and Configure Variables |
| 72 | + |
| 73 | +1. Fork [https://github.com/bytebase/example-gitops-github-flow](https://github.com/bytebase/example-gitops-github-flow). There are two workflows in this repository for this tutorial: |
| 74 | + |
| 75 | + - `.github/workflows/declarative-release-action.yml`: Deploy release in Bytebase after the PR is merged to the `main` branch. |
| 76 | + - `.github/workflows/declarative-sql-review-action.yml`: [Lint the generated SQL](/sql-review/review-policy/) migration after the PR is created with AI configured. |
| 77 | + |
| 78 | +1. Go into the workflow files and update the `env` section with your own values: |
| 79 | + |
| 80 | + - **BYTEBASE_URL**: Your Bytebase instance URL (e.g., `https://bytebase.your-company.com` or your Bytebase Cloud URL) |
| 81 | + - **BYTEBASE_SERVICE_ACCOUNT **: `[email protected]` (the service account you created in the previous step) |
| 82 | + - **BYTEBASE_PROJECT**: `projects/project-sample` (the sample project in Bytebase) |
| 83 | + - **BYTEBASE_TARGETS**: `instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod` (the two default databases in the sample project) |
| 84 | + - **STATE_FILE_PATTERN**: `schemas/*.sql` (the pattern for state definition files) |
| 85 | + |
| 86 | +1. Add the service account password as a secret named **BYTEBASE_SERVICE_ACCOUNT_SECRET** in **Settings > Secrets and Variables > Actions**. |
| 87 | + |
| 88 | +1. The **`GITHUB_TOKEN`** is automatically provided by GitHub during workflow execution. |
| 89 | + |
| 90 | +1. Go to **Actions** tab and enable workflow runs. |
| 91 | + |
| 92 | +### Step 4 - Configure AI Review |
| 93 | + |
| 94 | +1. Go to Bytebase console, click **Settings > General > AI Assistant**. |
| 95 | +1. Enable AI and choose your provider (OpenAI, Azure OpenAI, Gemini, or Claude). |
| 96 | +1. Enter your API credentials and test the connection. |
| 97 | +1. Create a `.bytebase/sql-review.md` file in your repository and write your team's SQL standards in natural language - no special syntax required. Here is an example: |
| 98 | + |
| 99 | +```markdown |
| 100 | +# .bytebase/sql-review.md |
| 101 | +# SQL Review Standards |
| 102 | + |
| 103 | +## 1. Table Naming Convention |
| 104 | +- All table names must be in snake_case |
| 105 | +``` |
| 106 | + |
| 107 | +1. Update `.github/workflows/declarative-sql-review-action.yml` to use the `.bytebase/sql-review.md` file. |
| 108 | + |
| 109 | +```yaml |
| 110 | +... |
| 111 | +run: | |
| 112 | + bytebase-action check --url=${{ env.BYTEBASE_URL }} --project=${{ env.BYTEBASE_PROJECT }} --targets=${{ env.BYTEBASE_TARGETS }} --file-pattern=${{ env.FILE_PATTERN }} --declarative --custom-rules "$(cat .bytebase/sql-review.md)" |
| 113 | +
|
| 114 | +``` |
| 115 | +
|
| 116 | +## Deploy the State Changes |
| 117 | +
|
| 118 | +### Step 1 - Export the Schema Definition File |
| 119 | +
|
| 120 | +State-based workflows use declarative SQL files that describe the complete desired state of your database schema. |
| 121 | +
|
| 122 | +1. Create a state definition file under `schemas/` directory, if you want to manage your existing database schema. You may go to Bytebase console, choose a database and then click **Export Schema** to get the schema definition file. |
| 123 | + |
| 124 | +1. In this tutorial, we go to `Sample Project` and then click **Database > Databases**. Choose `hr_prod` database and click **Export Schema > Single File** to get the schema definition file as follows: |
| 125 | + |
| 126 | + ```sql |
| 127 | + COMMENT ON SCHEMA "public" IS 'standard public schema'; |
| 128 | +
|
| 129 | + CREATE TABLE "public"."audit" ( |
| 130 | + "id" serial, |
| 131 | + "operation" text NOT NULL, |
| 132 | + "query" text, |
| 133 | + "user_name" text NOT NULL, |
| 134 | + "changed_at" timestamp(6) with time zone DEFAULT CURRENT_TIMESTAMP, |
| 135 | + CONSTRAINT "audit_pkey" PRIMARY KEY (id) |
| 136 | + ); |
| 137 | +
|
| 138 | + ... |
| 139 | + ``` |
| 140 | + |
| 141 | +1. Add a new table before `audit` table. Rename the file to `schema.sql` and move it to the `schemas/` directory. |
| 142 | + |
| 143 | + ```sql |
| 144 | + CREATE TABLE "public"."fakeTable" ( |
| 145 | + "id" serial, |
| 146 | + "name" text NOT NULL |
| 147 | + ); |
| 148 | + ``` |
| 149 | + |
| 150 | +### Step 2 - Create a Pull Request |
| 151 | + |
| 152 | +1. Commit to a new branch and create a pull request. Wait for a while, `declarative-sql-review-action.yml` workflow will be triggered and you can see the review results in the PR comments and file changes tab. |
| 153 | + |
| 154 | +  |
| 155 | +  |
| 156 | + |
| 157 | +1. There is a warning regarding the `fakeTable` table, we can fix by updating the schema definition file. |
| 158 | + |
| 159 | + ```sql |
| 160 | + CREATE TABLE "public"."fake_table" ( |
| 161 | + "id" serial, |
| 162 | + "name" text NOT NULL |
| 163 | + ); |
| 164 | + ``` |
| 165 | + |
| 166 | +### Step 3 - Merge the Pull Request |
| 167 | + |
| 168 | +1. Commit to the branch and push the changes. After the AI SQL review is passed, we can merge the pull request. |
| 169 | + |
| 170 | +1. Go to the **Actions** tab, you can see the workflow is running and the test stage is passed because it's automatically deployed. While the prod stage is waiting for approval. |
| 171 | + |
| 172 | +  |
| 173 | + |
| 174 | +1. Go to Bytebase console, you can see the release is created and the schema change is applied to the `hr_test` database. |
| 175 | + |
| 176 | +  |
| 177 | + |
| 178 | +  |
| 179 | + |
| 180 | +  |
| 181 | + |
| 182 | +1. Go back to GitHub **Actions** tab and approve the prod stage and wait for it to be deployed |
| 183 | + |
| 184 | +1. Go to Bytebase console, you can see the schema change is applied to the `hr_prod` database. |
| 185 | + |
| 186 | +  |
| 187 | + |
| 188 | +## Summary |
| 189 | + |
| 190 | +In this tutorial, we have learned how to build a database state-based workflow using GitHub Actions and Bytebase API for PostgreSQL databases. We have also learned how to use AI review to review the schema definition file. |
0 commit comments