Manage SQL Review Rules with Terraform
This tutorial is part of the Manage Bytebase with Terraform series:
- Part 1: Manage Databases with Terraform - Set up instances and environments
- Part 2: Manage Projects with Terraform - Organize databases into projects
- Part 3: Manage Bytebase Settings with Terraform - Configure workspace settings, environment policies, approval flows, and risk management
- Part 4: Manage SQL Review Rules with Terraform (This one) - Set up SQL review policies
- Part 5: Manage Database Access Control with Terraform - Set up access controls and permissions
- Part 6: Manage Data Masking with Terraform - Configure data masking policies
π Complete tutorial terraform files on GitHub
File Structure: This tutorial series uses separate Terraform files for better organization. Files are numbered by tutorial part (e.g., 1-instances.tf
for Part 1, 2-projects.tf
for Part 2, etc.). Terraform automatically handles dependencies between files.
Learn how to configure SQL review rules to enforce database schema standards using Terraform and the Terraform Bytebase Provider.
Bytebase Terraform Provider handles control plane configuration such as settings, policies, access controls. It does not handle data plane operations such as database creation, schema migrations, DML execution, query.
What Youβll Learn
- Configure SQL review rules for schema standards
- Enforce naming conventions and structural requirements
- Apply different severity levels (ERROR, WARNING)
- Target specific environments with review policies
Prerequisites
Before starting this tutorial, ensure you have:
- Completed Part 3: Manage Bytebase Settings with Terraform
- Bytebase running with ngrok and service account configured
- Your Terraform files from the previous tutorials
Setup
From the previous tutorials, you should have:
- Bytebase instances and projects configured
- Environments (test and prod) set up
- Workspace settings and approval flows configured
Configure SQL Review Rules
Step 1 - Create Review Configuration
Create 4-sql-review.tf
with the SQL review configuration:
Step 2 - Apply Configuration
Step 3 - Verify in Bytebase
- Go to CI/CD > SQL Review in the left sidebar
- You should see Sample SQL Review Config listed
- Click on it to view the configured rules
- Note that itβs applied to the production environment
Rule Types Explained
The example demonstrates key SQL review rule categories:
1. Column Rules
- column.no-null: Warns about nullable columns
- column.required: Enforces required columns (id, timestamps, audit fields)
2. Table Rules
- table.require-pk: Every table must have a primary key
3. Naming Rules
- naming.column: Enforces lowercase snake_case column names
4. Statement Rules
- statement.maximum-limit-value: Prevents SELECT queries without proper limits
Step 4 - Test SQL Review
Create a test SQL statement to see the review in action:
- Go to Project Two > Database > hr_prod
- Click Edit Schema
- Try creating a table that violates the rules:
Expected violations:
- β Column naming convention (should be first_name)
- β Missing required columns (id, created_ts, etc.)
- β No primary key defined
- Try a compliant table:
Expected result:
- β οΈ Warning about nullable columns (if any)
- β All required rules pass
Advanced Configuration
Targeting Multiple Environments
To apply rules to multiple environments:
Engine-Specific Rules
Different rules for different database engines:
Key Points
- Rule Levels: ERROR blocks changes, WARNING allows but notifies
- Engine Specific: Rules can target specific database engines
- Environment Scoped: Apply different rules to different environments
- Payload Format: Complex rules use JSON payloads for configuration
Next Steps
Continue the series: Manage Database Access Control with Terraform - set up access controls and permissions.
Resources: