SQL Review with GitHub Actions

Estimated: 20 mins

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, 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.

This tutorial is part of the SQL Review series:

  1. SQL Review with Bytebase GUI
  2. SQL Review with CI
    • SQL Review with GitHub Actions (this one)
  3. SQL Review with Bytebase API
  4. Codify SQL Review Policies with Bytebase API

This tutorial will show you how to integrate SQL Review with GitHub Actions to improve your database schema change process.

Prerequisites

Configure ngrok and create service account same as Database Release CI/CD with GitHub Actions.

GitHub Action Workflow

This 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.

Configuration snippet:

env:
BYTEBASE_URL: 'https://demo.bytebase.com'
BYTEBASE_SERVICE_ACCOUNT: 'ci@service.bytebase.com'
BYTEBASE_PROJECT: 'projects/project-sample'
BYTEBASE_TARGETS: 'instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod'
FILE_PATTERN: 'migrations/*.sql'

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 to see the SQL Review Sample Policy for the Prod environment.

bb-sql-review-prod

Example PR

Example PR contains four sql files in the migrations folder:

  • 001__drop_dept_emp.sql
  • 002__add_employee_phone.sql
  • 003__update_employee_gender.sql
  • 004__create_book.sql

Upon PR creation, the GitHub Action triggers SQL Review. A summary, including affected rows, risk level, and advices, is posted as a PR comment. With four files and two stages, there are eight lines of review results.

gh-sql-review-summary

Click the Files changed tab for detailed SQL review results posted inlined in the PR.

gh-file-changed

You may correct the SQL files and push again. The SQL Review will be triggered again and the results will be updated.

Summary

Now you have learned how to trigger SQL Review in GitHub Actions, you may also refer to the SQL Review document for more details.

Edit this page on GitHub