You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorials/state-based-schema-management-github.mdx
+19-24Lines changed: 19 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,18 +22,13 @@ This is part of our database deployment series with Bytebase:
22
22
23
23
---
24
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:
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
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
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
37
32
38
33
<Info>
39
34
@@ -56,7 +51,7 @@ This tutorial shows you how to build a database state-based workflow using GitHu
56
51
57
52
**State-based**: Declare the desired schema state. The system automatically generates migrations by comparing current and desired states.
58
53
59
-
## Prepare the Environment
54
+
## Setup
60
55
61
56
### Step 1 - Set up Bytebase
62
57
@@ -117,11 +112,11 @@ run: |
117
112
118
113
### Step 1 - Export the Schema Definition File
119
114
120
-
State-based workflows use declarative SQL files that describe the complete desired state of your database schema.
115
+
State-based workflows use SQL files that define the complete desired schema.
121
116
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.
117
+
1. To manage existing schemas, export them from Bytebase console: navigate to your database and click **Export Schema**.
123
118
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:
119
+
1. Navigate to `Sample Project` > **Database > Databases**. Select `hr_prod` and export the schema:
125
120
126
121
```sql
127
122
COMMENT ON SCHEMA "public" IS 'standard public schema';
@@ -138,7 +133,7 @@ State-based workflows use declarative SQL files that describe the complete desir
138
133
...
139
134
```
140
135
141
-
1. Add a new table before `audit` table. Rename the file to `schema.sql` and move it to the `schemas/` directory.
136
+
1. Add a new table, save as `schema.sql` in the `schemas/` directory:
142
137
143
138
```sql
144
139
CREATE TABLE "public"."fakeTable" (
@@ -149,12 +144,12 @@ State-based workflows use declarative SQL files that describe the complete desir
149
144
150
145
### Step 2 - Create a Pull Request
151
146
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.
147
+
1. Create a pull request from your branch. The workflow triggers automatically, posting review results as PR comments.
1. There is a warning regarding the `fakeTable` table, we can fix by updating the schema definition file.
152
+
1. Fix the naming convention warning by updating the table name:
158
153
159
154
```sql
160
155
CREATE TABLE "public"."fake_table" (
@@ -165,26 +160,26 @@ State-based workflows use declarative SQL files that describe the complete desir
165
160
166
161
### Step 3 - Merge the Pull Request
167
162
168
-
1. Commit to the branch and push the changes. After the AI SQL review is passed, we can merge the pull request.
163
+
1. Push the fix. Once the review passes, merge the PR.
169
164
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.
165
+
1. Check the **Actions** tab - test deploys automatically, prod awaits approval.
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.
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