SQL Review with Bytebase GUI
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, CI or API.
This is the 3rd part of tutorial series of SQL Review:
- SQL Review with Bytebase GUI (this one)
- SQL Review with CI (TBD)
- SQL Review with Bytebase API
- Codify SQL Review Policies with Bytebase API
This tutorial will walk you through using SQL Review in Bytebase with Bytebase GUI to improve your database schema change process. The Community Plan is sufficient for completing this tutorial.
Prerequisites
-
Make sure you have Docker installed, and if you don’t have important existing Bytebase data locally, you can start over from scratch by
rm -rf ~/.bytebase/data
. -
Copy and paste the commands to start one Bytebase via Docker.
docker run --rm --init \ --name bytebase \ --publish 8080:8080 --pull always \ --volume ~/.bytebase/data:/var/opt/bytebase \ bytebase/bytebase:3.4.1
Configure SQL Review Policies
-
Register an admin account and it will be granted the
workspace admin
role automatically. -
Click CI/CD > SQL Review on the left side menu. You can find there is already one default SQL Review policy named
SQL Review Sample Policy
. -
Click Edit, you'll be redirected to the SQL Review policy edit page. Here you may specify different rules for different database engines, and here our sample database is PostgreSQL, so we'll focus on the PostgreSQL rules.
-
The default policy is attached to Environment
Prod
, you may click Change attached resources to attach it to other Environments or Projects. Here we'll keep it as is. You may also click Change the template to change the rules. -
Click Change the template, keep the default template
Default SQL Review Template
and click Next. Add one ruleEnforce setting default value on columns
, set Error Level toError
and click Confirm and update.
Trigger SQL Review
-
Go to
Sample Project
, since the default SQL Review policy is attached to theProd
environment, we'll create a new table on it. Click Database > Databases on the left side menu, selecthr_prod
and click Edit Schema. -
Click Add Table while choosing
Prod hr_prod > public > Tables
on the left bar. Create a new tablet1
with one columnid
andname
, intentionly unclick Not Null forname
and click Preview issue. -
The SQL Review will run automatically before the issue preview, and you can see the violations against our defined rules. Click Continue anyway.
-
After redirecting to the issue page, click Create and Continue anyway, the issue is created with SQL review red marked as there is some
Error
. -
You may resolve this by changing the SQL as follows:
CREATE TABLE "public"."t1" ( "id" integer NOT NULL DEFAULT 0, "name" text NOT NULL DEFAULT '' );
If you find that you can't edit the SQL, you can click Setting on the left side menu, and then check Allow modify statement or otherwise the SQL is not editable.
Summary
Now you have learned how to trigger SQL Review in Bytebase GUI, you may also refer to the SQL Review document for more details. Next, let's learn how to trigger SQL Review from your CI API.