Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: enable sdl style check (#18012)

rebelice2dff5ff46c4a
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.go
index 0163a1b922..c78dbdfe0d 100644
--- a/backend/api/v1/release_service_check.go
+++ b/backend/api/v1/release_service_check.go
@@ -381,13 +381,33 @@ func (s *ReleaseService) checkReleaseDeclarative(ctx context.Context, files []*v
}
}
- // Perform SDL integrity checks for PostgreSQL (handles cross-file validation)
+ // Perform SDL style and integrity checks for PostgreSQL
+ var sdlStyleAdvices map[string][]*storepb.Advice
var sdlIntegrityAdvices map[string][]*storepb.Advice
if engine == storepb.Engine_POSTGRES {
fileContents := make(map[string]string)
for _, file := range files {
fileContents[file.Path] = string(file.Statement)
}
+
+ // Run SDL style checks (schema name requirements, index naming, etc.)
+ sdlStyleAdvices = make(map[string][]*storepb.Advice)
+ for filePath, content := range fileContents {
+ advices, err := advisorpg.CheckSDLStyle(content)
+ if err != nil {
+ // Continue with other checks even if style check fails
+ sdlStyleAdvices[filePath] = []*storepb.Advice{{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.Internal.Int32(),
+ Title: "Failed to check SDL style",
+ Content: err.Error(),
+ }}
+ } else {
+ sdlStyleAdvices[filePath] = advices
+ }
+ }
+
+ // Run SDL integrity checks (handles cross-file validation)
var err error
sdlIntegrityAdvices, err = advisorpg.CheckSDLIntegrity(fileContents)
if err != nil {
@@ -455,8 +475,15 @@ func (s *ReleaseService) checkReleaseDeclarative(ctx context.Context, files []*v
}
}
- // Add SDL integrity check results for this file (PostgreSQL only)
+ // Add SDL style and integrity check results for this file (PostgreSQL only)
if engine == storepb.Engine_POSTGRES && len(checkResult.Advices) == 0 {
+ // Add SDL style check results
+ if advices, exists := sdlStyleAdvices[file.Path]; exists {
+ for _, advice := range advices {
+ checkResult.Advices = append(checkResult.Advices, convertToV1Advice(advice))
+ }
+ }
+ // Add SDL integrity check results
if advices, exists := sdlIntegrityAdvices[file.Path]; exists {
for _, advice := range advices {
checkResult.Advices = append(checkResult.Advices, convertToV1Advice(advice))
diff --git a/backend/plugin/advisor/pg/sdl_check.go b/backend/plugin/advisor/pg/sdl_check.go
index ccde78d1ef..12214cb347 100644
--- a/backend/plugin/advisor/pg/sdl_check.go
+++ b/backend/plugin/advisor/pg/sdl_check.go
@@ -11,47 +11,29 @@ import (
pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
)
-// CheckSDL performs SDL-specific checks on PostgreSQL declarative schema files.
-// It runs both style/convention checks and integrity checks:
+// CheckSDLStyle performs SDL style/convention checks on PostgreSQL declarative schema files.
//
-// Style Checks (sdl_check.go):
+// Style Checks:
// 1. Disallowed column-level constraints (PRIMARY KEY, UNIQUE, CHECK, FOREIGN KEY must be table-level; NOT NULL, DEFAULT, GENERATED are allowed)
// 2. Table constraints without explicit names
// 3. Objects without explicit schema names (tables, indexes, views, sequences, functions)
// 4. Foreign key references without explicit schema names
// 5. Indexes without explicit names (unnamed indexes are not allowed)
//
-// Integrity Checks (sdl_integrity_check.go):
-// 1. Foreign key references to non-existent tables/columns
-// 2. Foreign key type mismatches
-// 3. CHECK constraints referencing non-existent columns
-// 4. CHECK constraints referencing other tables (not allowed)
-// 5. Duplicate object names (tables, indexes, constraints, columns)
-// 6. Multiple primary keys on same table
-// 7. View dependencies on non-existent tables
-func CheckSDL(statement string) ([]*storepb.Advice, error) {
+// For integrity checks (foreign key validation, duplicate detection, etc.), use CheckSDLIntegrity from sdl_integrity_check.go.
+func CheckSDLStyle(statement string) ([]*storepb.Advice, error) {
tree, err := pgparser.ParsePostgreSQL(statement)
if err != nil {
return nil, err
}
- var allAdvices []*storepb.Advice
-
// Run style/convention checks
styleChecker := &sdlChecker{
BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
}
antlr.ParseTreeWalkerDefault.Walk(styleChecker, tree.Tree)
- allAdvices = append(allAdvices, styleChecker.adviceList...)
-
- // Run integrity checks
- integrityAdvices, err := checkSingleStatement(statement)
- if err != nil {
- return allAdvices, err // Return style checks even if integrity check fails
- }
- allAdvices = append(allAdvices, integrityAdvices...)
- return allAdvices, nil
+ return styleChecker.adviceList, nil
}
type sdlChecker struct {
diff --git a/backend/plugin/advisor/pg/sdl_check_test.go b/backend/plugin/advisor/pg/sdl_check_test.go
index 76c2a122d4..b056f8c5b8 100644
--- a/backend/plugin/advisor/pg/sdl_check_test.go
+++ b/backend/plugin/advisor/pg/sdl_check_test.go
@@ -9,7 +9,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/advisor"
)
-func TestCheckSDL(t *testing.T) {
+func TestCheckSDLStyle(t *testing.T) {
tests := []struct {
name string
statement string
@@ -208,6 +208,12 @@ func TestCheckSDL(t *testing.T) {
wantCount: 1,
wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
},
+ {
+ name: "Error - CREATE INDEX without schema name (quoted table)",
+ statement: `CREATE INDEX "idx_departments_budget" ON ONLY "departments" (budget);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
+ },
{
name: "Valid SDL - CREATE UNIQUE INDEX with schema name",
statement: `CREATE UNIQUE INDEX idx_users_email ON public.users (email);`,
@@ -427,8 +433,8 @@ CREATE VIEW active_users AS SELECT * FROM public.users WHERE active = true;`,
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
- advices, err := CheckSDL(tc.statement)
- require.NoError(t, err, "CheckSDL should not return error for valid SQL")
+ advices, err := CheckSDLStyle(tc.statement)
+ require.NoError(t, err, "CheckSDLStyle should not return error for valid SQL")
require.Equal(t, tc.wantCount, len(advices), "Unexpected number of advices")
if tc.wantCount > 0 {
@@ -449,7 +455,7 @@ CREATE VIEW active_users AS SELECT * FROM public.users WHERE active = true;`,
}
}
-func TestCheckSDL_InvalidSQL(t *testing.T) {
+func TestCheckSDLStyle_InvalidSQL(t *testing.T) {
tests := []struct {
name string
statement string
@@ -466,20 +472,20 @@ func TestCheckSDL_InvalidSQL(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
- _, err := CheckSDL(tc.statement)
- require.Error(t, err, "CheckSDL should return error for invalid SQL")
+ _, err := CheckSDLStyle(tc.statement)
+ require.Error(t, err, "CheckSDLStyle should return error for invalid SQL")
})
}
}
-func TestCheckSDL_EmptyStatement(t *testing.T) {
- advices, err := CheckSDL("")
+func TestCheckSDLStyle_EmptyStatement(t *testing.T) {
+ advices, err := CheckSDLStyle("")
require.NoError(t, err)
require.Empty(t, advices, "Empty statement should return no advices")
}
-func TestCheckSDL_CommentOnly(t *testing.T) {
- advices, err := CheckSDL("-- This is just a comment")
+func TestCheckSDLStyle_CommentOnly(t *testing.T) {
+ advices, err := CheckSDLStyle("-- This is just a comment")
require.NoError(t, err)
require.Empty(t, advices, "Comment-only statement should return no advices")
}