|
| 1 | +--- |
| 2 | +title: 'SQL Review with GitHub Actions' |
| 3 | +author: Adela |
| 4 | +updated_at: 2025/03/03 18:15 |
| 5 | +tags: Tutorial |
| 6 | +level: Intermediate |
| 7 | +estimated_time: '20 mins' |
| 8 | +description: Learn how to use SQL Review via GitHub CI to enhance your database schema change process. |
| 9 | +--- |
| 10 | + |
| 11 | +When modifying data in the database, it's crucial to ensure that the changes are both safe and accurate. Bytebase offers a feature called [SQL Review](/docs/sql-review/overview/), which allows you to evaluate your SQL changes before they are applied to the database. SQL Review can be invoked from the Bytebase GUI, API or CI. |
| 12 | + |
| 13 | +This tutorial is part of the SQL Review series: |
| 14 | + |
| 15 | +1. [SQL Review with Bytebase GUI](/docs/tutorials/sql-review-gui/) |
| 16 | +1. SQL Review with CI |
| 17 | + - SQL Review with GitHub Actions (this one) |
| 18 | +1. [SQL Review with Bytebase API](/docs/tutorials/sql-review-api/) |
| 19 | +1. [Codify SQL Review Policies with Bytebase API](/docs/tutorials/api-sql-review-policy/) |
| 20 | + |
| 21 | +This tutorial will show you how to integrate SQL Review with GitHub Actions to improve your database schema change process. |
| 22 | + |
| 23 | +Example repository: [release-cicd-workflows-example](https://github.com/bytebase/release-cicd-workflows-example). |
| 24 | + |
| 25 | +For detailed steps on setting up SQL Review in conjunction with database release CI/CD using GitHub Actions, refer to the [Database Release CI/CD with GitHub Actions](/docs/tutorials/github-release-cicd-workflow/) tutorial. |
| 26 | + |
| 27 | +## GitHub Action Workflow |
| 28 | + |
| 29 | +This [bytebase-check-release.yml](https://github.com/bytebase/release-cicd-workflows-example/blob/main/.github/workflows/bytebase-check-release.yml) workflow is the GitHub Action that will be triggered when a PR is created. It will check the SQL files in the PR and review them with Bytebase. |
| 30 | + |
| 31 | +Configuration snippet: |
| 32 | + |
| 33 | +```yaml |
| 34 | + env: |
| 35 | + BYTEBASE_URL: "https://demo.bytebase.com" |
| 36 | + BYTEBASE_SERVICE_ACCOUNT: "ci@service.bytebase.com" |
| 37 | + BYTEBASE_PROJECT: "projects/project-sample" |
| 38 | + BYTEBASE_TARGETS: "instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod" |
| 39 | + FILE_PATTERN: "migrations/*.sql" |
| 40 | +``` |
| 41 | +
|
| 42 | +The deployment target is the database that the SQL files will be applied to. So the SQL Review policy is fetched based on the target database. Visit the [demo sql review policy page](https://demo.bytebase.com/sql-review) to see the `SQL Review Sample Policy` for the `Prod` environment. |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +## Example PR |
| 47 | + |
| 48 | +[Example PR](https://github.com/bytebase/release-cicd-workflows-example/pull/4)cointains four sql files in the `migrations` folder: |
| 49 | + |
| 50 | +- `001__drop_dept_emp.sql` |
| 51 | +- `002__add_employee_phone.sql` |
| 52 | +- `003__update_employee_gender.sql` |
| 53 | +- `004__create_book.sql` |
| 54 | + |
| 55 | +Upon PR creation, the GitHub Action triggers SQL Review. A summary, including affected rows, risk level, and advice, is posted as a PR comment. With four files and two stages, there are eight lines of review results. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +Click the **Files changed** tab for detailed SQL review results per file. |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +You may correct the SQL files and push again. The SQL Review will be performed again and the results will be updated. |
| 64 | + |
| 65 | +## Summary |
| 66 | + |
| 67 | +Now you have learned how to trigger SQL Review in GitHub Actions, you may also refer to the [SQL Review](/docs/sql-review/overview) document for more details. |
0 commit comments