|
| 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 | +Learn to implement state-based database schema management using GitHub Actions and Bytebase. This approach declares the desired schema state and automatically generates necessary migrations, eliminating manual script writing. |
| 26 | + |
| 27 | +**What you'll build:** |
| 28 | +- Declarative schema definitions in SQL files |
| 29 | +- AI-powered SQL reviews on pull requests |
| 30 | +- Auto-deployment of state changes on merge to main |
| 31 | +- Schema drift detection and correction |
| 32 | + |
| 33 | +<Info> |
| 34 | + |
| 35 | +**Important:** State-based workflow currently only supports PostgreSQL. |
| 36 | + |
| 37 | +</Info> |
| 38 | + |
| 39 | +## Repository |
| 40 | + |
| 41 | +[https://github.com/bytebase/example-gitops-github-flow](https://github.com/bytebase/example-gitops-github-flow) |
| 42 | + |
| 43 | +## Prerequisites |
| 44 | + |
| 45 | +- A Bytebase instance (Bytebase Cloud or self-hosted) |
| 46 | +- For self-hosted version, you need [Docker](https://www.docker.com/) to run Bytebase. |
| 47 | + |
| 48 | +## State-based vs Migration-based |
| 49 | + |
| 50 | +**Migration-based**: Write incremental scripts that transform the database step-by-step. You track which migrations have run. |
| 51 | + |
| 52 | +**State-based**: Declare the desired schema state. The system automatically generates migrations by comparing current and desired states. |
| 53 | + |
| 54 | +## Setup |
| 55 | + |
| 56 | +### Step 1 - Set up Bytebase |
| 57 | + |
| 58 | +import BytebaseSetupOptions from '/snippets/tutorials/bytebase-setup-options.mdx'; |
| 59 | + |
| 60 | +<BytebaseSetupOptions /> |
| 61 | + |
| 62 | +### Step 2 - Create Service Account |
| 63 | + |
| 64 | +<CreateServiceAccountGitOps /> |
| 65 | + |
| 66 | +### Step 3 - Fork the Example Repository and Configure Variables |
| 67 | + |
| 68 | +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: |
| 69 | + |
| 70 | + - `.github/workflows/declarative-release-action.yml`: Deploy release in Bytebase after the PR is merged to the `main` branch. |
| 71 | + - `.github/workflows/declarative-sql-review-action.yml`: [Lint the generated SQL](/sql-review/review-policy/) migration after the PR is created with AI configured. |
| 72 | + |
| 73 | +1. Go into the workflow files and update the `env` section with your own values: |
| 74 | + |
| 75 | + - **BYTEBASE_URL**: Your Bytebase instance URL (e.g., `https://bytebase.your-company.com` or your Bytebase Cloud URL) |
| 76 | + - **BYTEBASE_SERVICE_ACCOUNT **: `[email protected]` (the service account you created in the previous step) |
| 77 | + - **BYTEBASE_PROJECT**: `projects/project-sample` (the sample project in Bytebase) |
| 78 | + - **BYTEBASE_TARGETS**: `instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod` (the two default databases in the sample project) |
| 79 | + - **STATE_FILE_PATTERN**: `schemas/*.sql` (the pattern for state definition files) |
| 80 | + |
| 81 | +1. Add the service account password as a secret named **BYTEBASE_SERVICE_ACCOUNT_SECRET** in **Settings > Secrets and Variables > Actions**. |
| 82 | + |
| 83 | +1. The **`GITHUB_TOKEN`** is automatically provided by GitHub during workflow execution. |
| 84 | + |
| 85 | +1. Go to **Actions** tab and enable workflow runs. |
| 86 | + |
| 87 | +### Step 4 - Configure AI Review |
| 88 | + |
| 89 | +1. Go to Bytebase console, click **Settings > General > AI Assistant**. |
| 90 | +1. Enable AI and choose your provider (OpenAI, Azure OpenAI, Gemini, or Claude). |
| 91 | +1. Enter your API credentials and test the connection. |
| 92 | +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: |
| 93 | + |
| 94 | +```markdown |
| 95 | +# .bytebase/sql-review.md |
| 96 | +# SQL Review Standards |
| 97 | + |
| 98 | +## 1. Table Naming Convention |
| 99 | +- All table names must be in snake_case |
| 100 | +``` |
| 101 | + |
| 102 | +1. Update `.github/workflows/declarative-sql-review-action.yml` to use the `.bytebase/sql-review.md` file. |
| 103 | + |
| 104 | +```yaml |
| 105 | +... |
| 106 | +run: | |
| 107 | + 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)" |
| 108 | +
|
| 109 | +``` |
| 110 | +
|
| 111 | +## Deploy the State Changes |
| 112 | +
|
| 113 | +### Step 1 - Export the Schema Definition File |
| 114 | +
|
| 115 | +State-based workflows use SQL files that define the complete desired schema. |
| 116 | +
|
| 117 | +1. To manage existing schemas, export them from Bytebase console: navigate to your database and click **Export Schema**. |
| 118 | +
|
| 119 | +1. Navigate to `Sample Project` > **Database > Databases**. Select `hr_prod` and export the schema: |
| 120 | + |
| 121 | + ```sql |
| 122 | + COMMENT ON SCHEMA "public" IS 'standard public schema'; |
| 123 | +
|
| 124 | + CREATE TABLE "public"."audit" ( |
| 125 | + "id" serial, |
| 126 | + "operation" text NOT NULL, |
| 127 | + "query" text, |
| 128 | + "user_name" text NOT NULL, |
| 129 | + "changed_at" timestamp(6) with time zone DEFAULT CURRENT_TIMESTAMP, |
| 130 | + CONSTRAINT "audit_pkey" PRIMARY KEY (id) |
| 131 | + ); |
| 132 | +
|
| 133 | + ... |
| 134 | + ``` |
| 135 | + |
| 136 | +1. Add a new table, save as `schema.sql` in the `schemas/` directory: |
| 137 | + |
| 138 | + ```sql |
| 139 | + CREATE TABLE "public"."fakeTable" ( |
| 140 | + "id" serial, |
| 141 | + "name" text NOT NULL |
| 142 | + ); |
| 143 | + ``` |
| 144 | + |
| 145 | +### Step 2 - Create a Pull Request |
| 146 | + |
| 147 | +1. Create a pull request from your branch. The workflow triggers automatically, posting review results as PR comments. |
| 148 | + |
| 149 | +  |
| 150 | +  |
| 151 | + |
| 152 | +1. Fix the naming convention warning by updating the table name: |
| 153 | + |
| 154 | + ```sql |
| 155 | + CREATE TABLE "public"."fake_table" ( |
| 156 | + "id" serial, |
| 157 | + "name" text NOT NULL |
| 158 | + ); |
| 159 | + ``` |
| 160 | + |
| 161 | +### Step 3 - Merge the Pull Request |
| 162 | + |
| 163 | +1. Push the fix. Once the review passes, merge the PR. |
| 164 | + |
| 165 | +1. Check the **Actions** tab - test deploys automatically, prod awaits approval. |
| 166 | + |
| 167 | +  |
| 168 | + |
| 169 | +1. In Bytebase console, verify the release was created and applied to `hr_test`. |
| 170 | + |
| 171 | +  |
| 172 | + |
| 173 | +  |
| 174 | + |
| 175 | +  |
| 176 | + |
| 177 | +1. Return to GitHub **Actions** and approve the production deployment. |
| 178 | + |
| 179 | +1. Confirm the changes are applied to `hr_prod` in Bytebase. |
| 180 | + |
| 181 | +  |
| 182 | + |
| 183 | +## Summary |
| 184 | + |
| 185 | +You've successfully implemented state-based schema management with GitHub Actions, enabling declarative database changes with AI-powered reviews and automated deployments. |
0 commit comments