Skip to content

Commit 455980d

Browse files
authored
chore: support configure the payload for sql review rules (#42)
* feat: support policy data source * chore: support CRUD policy resource * chore: support config sql review rules
1 parent 303577d commit 455980d

File tree

7 files changed

+509
-32
lines changed

7 files changed

+509
-32
lines changed

api/policy.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ type ApprovalGroup string
1212
// BackupPlanSchedule is schedule for backup plan policy.
1313
type BackupPlanSchedule string
1414

15-
// SQLReviewRuleLevel is the error level for SQL review rule.
16-
type SQLReviewRuleLevel string
17-
1815
// SensitiveDataMaskType is the mask type for sensitive data.
1916
type SensitiveDataMaskType string
2017

@@ -50,13 +47,6 @@ const (
5047
// SensitiveDataMaskTypeDefault is the sensitive data type to hide data with a default method.
5148
// The default method is subject to change.
5249
SensitiveDataMaskTypeDefault SensitiveDataMaskType = "DEFAULT"
53-
54-
// SQLReviewRuleLevelError is the error level for SQL review rule.
55-
SQLReviewRuleLevelError SQLReviewRuleLevel = "ERROR"
56-
// SQLReviewRuleLevelWarning is the warning level for SQL review rule.
57-
SQLReviewRuleLevelWarning SQLReviewRuleLevel = "WARNING"
58-
// SQLReviewRuleLevelDisabled is the disabled level for SQL review rule.
59-
SQLReviewRuleLevelDisabled SQLReviewRuleLevel = "DISABLED"
6050
)
6151

6252
// DeploymentApprovalPolicy is the policy configuration for deployment approval.
@@ -104,15 +94,8 @@ type AccessControlRule struct {
10494

10595
// SQLReviewPolicy is the API message for SQL review policy.
10696
type SQLReviewPolicy struct {
107-
Title string `type:"title"`
108-
Rules []*SQLReviewRule `type:"rules"`
109-
}
110-
111-
// SQLReviewRule is the API message for SQL review rule.
112-
type SQLReviewRule struct {
113-
Type string `json:"type"`
114-
Level SQLReviewRuleLevel `json:"level"`
115-
Payload string `json:"payload"`
97+
Title string `json:"title"`
98+
Rules []*SQLReviewRule `json:"rules"`
11699
}
117100

118101
// PolicyFindMessage is the API message for finding policies.

api/sql_review.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package api
2+
3+
// SQLReviewRuleType is the type of schema rule.
4+
type SQLReviewRuleType string
5+
6+
// SQLReviewRuleLevel is the error level for SQL review rule.
7+
type SQLReviewRuleLevel string
8+
9+
const (
10+
// SchemaRuleTableNaming enforce the table name format.
11+
SchemaRuleTableNaming SQLReviewRuleType = "naming.table"
12+
// SchemaRuleColumnNaming enforce the column name format.
13+
SchemaRuleColumnNaming SQLReviewRuleType = "naming.column"
14+
// SchemaRulePKNaming enforce the primary key name format.
15+
SchemaRulePKNaming SQLReviewRuleType = "naming.index.pk"
16+
// SchemaRuleUKNaming enforce the unique key name format.
17+
SchemaRuleUKNaming SQLReviewRuleType = "naming.index.uk"
18+
// SchemaRuleFKNaming enforce the foreign key name format.
19+
SchemaRuleFKNaming SQLReviewRuleType = "naming.index.fk"
20+
// SchemaRuleIDXNaming enforce the index name format.
21+
SchemaRuleIDXNaming SQLReviewRuleType = "naming.index.idx"
22+
// SchemaRuleAutoIncrementColumnNaming enforce the auto_increment column name format.
23+
SchemaRuleAutoIncrementColumnNaming SQLReviewRuleType = "naming.column.auto-increment"
24+
25+
// SchemaRuleStatementNoSelectAll disallow 'SELECT *'.
26+
SchemaRuleStatementNoSelectAll SQLReviewRuleType = "statement.select.no-select-all"
27+
// SchemaRuleStatementRequireWhere require 'WHERE' clause.
28+
SchemaRuleStatementRequireWhere SQLReviewRuleType = "statement.where.require"
29+
// SchemaRuleStatementNoLeadingWildcardLike disallow leading '%' in LIKE, e.g. LIKE foo = '%x' is not allowed.
30+
SchemaRuleStatementNoLeadingWildcardLike SQLReviewRuleType = "statement.where.no-leading-wildcard-like"
31+
// SchemaRuleStatementDisallowCommit disallow using commit in the issue.
32+
SchemaRuleStatementDisallowCommit SQLReviewRuleType = "statement.disallow-commit"
33+
// SchemaRuleStatementDisallowLimit disallow the LIMIT clause in INSERT, DELETE and UPDATE statements.
34+
SchemaRuleStatementDisallowLimit SQLReviewRuleType = "statement.disallow-limit"
35+
// SchemaRuleStatementDisallowOrderBy disallow the ORDER BY clause in DELETE and UPDATE statements.
36+
SchemaRuleStatementDisallowOrderBy SQLReviewRuleType = "statement.disallow-order-by"
37+
// SchemaRuleStatementMergeAlterTable disallow redundant ALTER TABLE statements.
38+
SchemaRuleStatementMergeAlterTable SQLReviewRuleType = "statement.merge-alter-table"
39+
// SchemaRuleStatementInsertRowLimit enforce the insert row limit.
40+
SchemaRuleStatementInsertRowLimit SQLReviewRuleType = "statement.insert.row-limit"
41+
// SchemaRuleStatementInsertMustSpecifyColumn enforce the insert column specified.
42+
SchemaRuleStatementInsertMustSpecifyColumn SQLReviewRuleType = "statement.insert.must-specify-column"
43+
// SchemaRuleStatementInsertDisallowOrderByRand disallow the order by rand in the INSERT statement.
44+
SchemaRuleStatementInsertDisallowOrderByRand SQLReviewRuleType = "statement.insert.disallow-order-by-rand"
45+
// SchemaRuleStatementAffectedRowLimit enforce the UPDATE/DELETE affected row limit.
46+
SchemaRuleStatementAffectedRowLimit SQLReviewRuleType = "statement.affected-row-limit"
47+
// SchemaRuleStatementDMLDryRun dry run the dml.
48+
SchemaRuleStatementDMLDryRun SQLReviewRuleType = "statement.dml-dry-run"
49+
50+
// SchemaRuleTableRequirePK require the table to have a primary key.
51+
SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"
52+
// SchemaRuleTableNoFK require the table disallow the foreign key.
53+
SchemaRuleTableNoFK SQLReviewRuleType = "table.no-foreign-key"
54+
// SchemaRuleTableDropNamingConvention require only the table following the naming convention can be deleted.
55+
SchemaRuleTableDropNamingConvention SQLReviewRuleType = "table.drop-naming-convention"
56+
// SchemaRuleTableCommentConvention enforce the table comment convention.
57+
SchemaRuleTableCommentConvention SQLReviewRuleType = "table.comment"
58+
// SchemaRuleTableDisallowPartition disallow the table partition.
59+
SchemaRuleTableDisallowPartition SQLReviewRuleType = "table.disallow-partition"
60+
61+
// SchemaRuleRequiredColumn enforce the required columns in each table.
62+
SchemaRuleRequiredColumn SQLReviewRuleType = "column.required"
63+
// SchemaRuleColumnNotNull enforce the columns cannot have NULL value.
64+
SchemaRuleColumnNotNull SQLReviewRuleType = "column.no-null"
65+
// SchemaRuleColumnDisallowChangeType disallow change column type.
66+
SchemaRuleColumnDisallowChangeType SQLReviewRuleType = "column.disallow-change-type"
67+
// SchemaRuleColumnSetDefaultForNotNull require the not null column to set default value.
68+
SchemaRuleColumnSetDefaultForNotNull SQLReviewRuleType = "column.set-default-for-not-null"
69+
// SchemaRuleColumnDisallowChange disallow CHANGE COLUMN statement.
70+
SchemaRuleColumnDisallowChange SQLReviewRuleType = "column.disallow-change"
71+
// SchemaRuleColumnDisallowChangingOrder disallow changing column order.
72+
SchemaRuleColumnDisallowChangingOrder SQLReviewRuleType = "column.disallow-changing-order"
73+
// SchemaRuleColumnCommentConvention enforce the column comment convention.
74+
SchemaRuleColumnCommentConvention SQLReviewRuleType = "column.comment"
75+
// SchemaRuleColumnAutoIncrementMustInteger require the auto-increment column to be integer.
76+
SchemaRuleColumnAutoIncrementMustInteger SQLReviewRuleType = "column.auto-increment-must-integer"
77+
// SchemaRuleColumnTypeDisallowList enforce the column type disallow list.
78+
SchemaRuleColumnTypeDisallowList SQLReviewRuleType = "column.type-disallow-list"
79+
// SchemaRuleColumnDisallowSetCharset disallow set column charset.
80+
SchemaRuleColumnDisallowSetCharset SQLReviewRuleType = "column.disallow-set-charset"
81+
// SchemaRuleColumnMaximumCharacterLength enforce the maximum character length.
82+
SchemaRuleColumnMaximumCharacterLength SQLReviewRuleType = "column.maximum-character-length"
83+
// SchemaRuleColumnAutoIncrementInitialValue enforce the initial auto-increment value.
84+
SchemaRuleColumnAutoIncrementInitialValue SQLReviewRuleType = "column.auto-increment-initial-value"
85+
// SchemaRuleColumnAutoIncrementMustUnsigned enforce the auto-increment column to be unsigned.
86+
SchemaRuleColumnAutoIncrementMustUnsigned SQLReviewRuleType = "column.auto-increment-must-unsigned"
87+
// SchemaRuleCurrentTimeColumnCountLimit enforce the current column count limit.
88+
SchemaRuleCurrentTimeColumnCountLimit SQLReviewRuleType = "column.current-time-count-limit"
89+
// SchemaRuleColumnRequireDefault enforce the column default.
90+
SchemaRuleColumnRequireDefault SQLReviewRuleType = "column.require-default"
91+
92+
// SchemaRuleSchemaBackwardCompatibility enforce the MySQL and TiDB support check whether the schema change is backward compatible.
93+
SchemaRuleSchemaBackwardCompatibility SQLReviewRuleType = "schema.backward-compatibility"
94+
95+
// SchemaRuleDropEmptyDatabase enforce the MySQL and TiDB support check if the database is empty before users drop it.
96+
SchemaRuleDropEmptyDatabase SQLReviewRuleType = "database.drop-empty-database"
97+
98+
// SchemaRuleIndexNoDuplicateColumn require the index no duplicate column.
99+
SchemaRuleIndexNoDuplicateColumn SQLReviewRuleType = "index.no-duplicate-column"
100+
// SchemaRuleIndexKeyNumberLimit enforce the index key number limit.
101+
SchemaRuleIndexKeyNumberLimit SQLReviewRuleType = "index.key-number-limit"
102+
// SchemaRuleIndexPKTypeLimit enforce the type restriction of columns in primary key.
103+
SchemaRuleIndexPKTypeLimit SQLReviewRuleType = "index.pk-type-limit"
104+
// SchemaRuleIndexTypeNoBlob enforce the type restriction of columns in index.
105+
SchemaRuleIndexTypeNoBlob SQLReviewRuleType = "index.type-no-blob"
106+
// SchemaRuleIndexTotalNumberLimit enforce the index total number limit.
107+
SchemaRuleIndexTotalNumberLimit SQLReviewRuleType = "index.total-number-limit"
108+
// SchemaRuleIndexPrimaryKeyTypeAllowlist enforce the primary key type allowlist.
109+
SchemaRuleIndexPrimaryKeyTypeAllowlist SQLReviewRuleType = "index.primary-key-type-allowlist"
110+
111+
// SchemaRuleCharsetAllowlist enforce the charset allowlist.
112+
SchemaRuleCharsetAllowlist SQLReviewRuleType = "system.charset.allowlist"
113+
114+
// SchemaRuleCollationAllowlist enforce the collation allowlist.
115+
SchemaRuleCollationAllowlist SQLReviewRuleType = "system.collation.allowlist"
116+
117+
// SchemaRuleCommentLength limit comment length.
118+
SchemaRuleCommentLength SQLReviewRuleType = "comment.length"
119+
120+
// SQLReviewRuleLevelError is the error level for SQL review rule.
121+
SQLReviewRuleLevelError SQLReviewRuleLevel = "ERROR"
122+
// SQLReviewRuleLevelWarning is the warning level for SQL review rule.
123+
SQLReviewRuleLevelWarning SQLReviewRuleLevel = "WARNING"
124+
// SQLReviewRuleLevelDisabled is the disabled level for SQL review rule.
125+
SQLReviewRuleLevelDisabled SQLReviewRuleLevel = "DISABLED"
126+
)
127+
128+
// NamingRulePayload is the payload for naming rule.
129+
type NamingRulePayload struct {
130+
MaxLength int `json:"maxLength"`
131+
Format string `json:"format"`
132+
}
133+
134+
// StringArrayTypeRulePayload is the payload for rules with string array value.
135+
type StringArrayTypeRulePayload struct {
136+
List []string `json:"list"`
137+
}
138+
139+
// RequiredColumnRulePayload is the payload for required column rule.
140+
type RequiredColumnRulePayload struct {
141+
ColumnList []string `json:"columnList"`
142+
}
143+
144+
// CommentConventionRulePayload is the payload for comment convention rule.
145+
type CommentConventionRulePayload struct {
146+
Required bool `json:"required"`
147+
MaxLength int `json:"maxLength"`
148+
}
149+
150+
// NumberTypeRulePayload is the number type payload.
151+
type NumberTypeRulePayload struct {
152+
Number int `json:"number"`
153+
}
154+
155+
// SQLReviewRule is the API message for SQL review rule.
156+
type SQLReviewRule struct {
157+
Type SQLReviewRuleType `json:"type"`
158+
Level SQLReviewRuleLevel `json:"level"`
159+
Payload string `json:"payload"`
160+
}

examples/policies/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ output "deployment_approval_policy" {
3232
value = data.bytebase_policy.deployment_approval
3333
}
3434

35+
# Find SQL review policy in test environment.
36+
data "bytebase_policy" "sql_review" {
37+
environment = local.environment_id_test
38+
type = "SQL_REVIEW"
39+
}
40+
41+
output "sql_review_policy" {
42+
value = data.bytebase_policy.sql_review
43+
}
44+
3545
# List policies in test environment.
3646
data "bytebase_policy_list" "test_env_policies" {
3747
environment = local.environment_id_test

examples/setup/main.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,35 @@ resource "bytebase_policy" "backup_plan" {
140140
retention_duration = 86400
141141
}
142142
}
143+
144+
# Create SQL review policy for test env.
145+
resource "bytebase_policy" "sql_review" {
146+
environment = bytebase_instance.test.environment
147+
type = "SQL_REVIEW"
148+
149+
sql_review_policy {
150+
title = "SQL Review Policy for Test environment"
151+
rules {
152+
type = "naming.table"
153+
level = "ERROR"
154+
payload {
155+
max_length = 99
156+
format = "^[a-z]+$"
157+
}
158+
}
159+
rules {
160+
type = "column.required"
161+
level = "WARNING"
162+
payload {
163+
list = ["id", "created_ts", "updated_ts"]
164+
}
165+
}
166+
rules {
167+
type = "column.auto-increment-initial-value"
168+
level = "DISABLED"
169+
payload {
170+
number = 1
171+
}
172+
}
173+
}
174+
}

0 commit comments

Comments
 (0)