Skip to content

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

chore: add sdl check (#17946)

rebelicebddf7f892018
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.go
index 8a8d2a4419..395cc408c8 100644
--- a/backend/api/v1/release_service_check.go
+++ b/backend/api/v1/release_service_check.go
@@ -12,6 +12,7 @@ import (
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/catalog"
+ advisorpg "github.com/bytebase/bytebase/backend/plugin/advisor/pg"
"github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/plugin/parser/pg"
"github.com/bytebase/bytebase/backend/runner/plancheck"
@@ -439,6 +440,23 @@ func (s *ReleaseService) checkReleaseDeclarative(ctx context.Context, files []*v
}
}
}
+
+ // Run SDL-specific checks (only for PostgreSQL)
+ if engine == storepb.Engine_POSTGRES && len(checkResult.Advices) == 0 {
+ sdlAdvices, err := s.runSDLChecksForPostgreSQL(ctx, statement)
+ if err != nil {
+ checkResult.Advices = append(checkResult.Advices, &v1pb.Advice{
+ Status: v1pb.Advice_ERROR,
+ Code: advisor.Internal.Int32(),
+ Title: "Failed to run SDL checks",
+ Content: err.Error(),
+ })
+ } else {
+ for _, advice := range sdlAdvices {
+ checkResult.Advices = append(checkResult.Advices, convertToV1Advice(advice))
+ }
+ }
+ }
}
}
@@ -594,6 +612,7 @@ func convertRiskLevel(riskLevel storepb.RiskLevel) (v1pb.RiskLevel, error) {
// allowedSDLStatementTypes defines the whitelist of statement types allowed in SDL files.
// SDL files should only contain CREATE and COMMENT statements to declare the desired schema.
+// ALTER SEQUENCE is allowed for setting ownership (OWNED BY).
var allowedSDLStatementTypes = map[string]bool{
// CREATE statements - declare new objects
"CREATE_TABLE": true,
@@ -604,6 +623,9 @@ var allowedSDLStatementTypes = map[string]bool{
"CREATE_PROCEDURE": true,
"CREATE_SCHEMA": true,
+ // ALTER statements - limited to specific cases
+ "ALTER_SEQUENCE": true, // Allowed for OWNED BY and sequence options
+
// COMMENT - metadata annotations
"COMMENT": true,
}
@@ -646,3 +668,12 @@ func getStatementTypesWithPositionsForEngine(engine storepb.Engine, asts any) ([
return []statementTypeWithPosition{}, nil
}
}
+
+// runSDLChecksForPostgreSQL runs SDL-specific checks for PostgreSQL declarative files.
+// It checks for:
+// 1. Column-level constraints (not allowed, only table-level)
+// 2. Table constraints without explicit names
+// 3. Objects without explicit schema names
+func (*ReleaseService) runSDLChecksForPostgreSQL(_ context.Context, statement string) ([]*storepb.Advice, error) {
+ return advisorpg.CheckSDL(statement)
+}
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index f3e9ea74d7..931f44bbca 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -64,6 +64,10 @@ const (
StatementNoLockOption Code = 237
StatementObjectOwnerCheck Code = 238
StatementDisallowedInSDL Code = 239
+ SDLDisallowColumnConstraint Code = 240
+ SDLRequireConstraintName Code = 241
+ SDLRequireSchemaName Code = 242
+ SDLRequireIndexName Code = 243
// 301 ~ 399 naming error code
// 301 table naming advisor error code.
diff --git a/backend/plugin/advisor/pg/sdl_check.go b/backend/plugin/advisor/pg/sdl_check.go
new file mode 100644
index 0000000000..f7c2287c68
--- /dev/null
+++ b/backend/plugin/advisor/pg/sdl_check.go
@@ -0,0 +1,635 @@
+package pg
+
+import (
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/parser/postgresql"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
+)
+
+// CheckSDL performs SDL-specific checks on PostgreSQL declarative schema files.
+// It checks for:
+// 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)
+func CheckSDL(statement string) ([]*storepb.Advice, error) {
+ tree, err := pgparser.ParsePostgreSQL(statement)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &sdlChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type sdlChecker struct {
+ *parser.BasePostgreSQLParserListener
+ adviceList []*storepb.Advice
+}
+
+// EnterCreatestmt handles CREATE TABLE statements.
+func (c *sdlChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ var tableName, schemaName string
+ allQualifiedNames := ctx.AllQualified_name()
+ if len(allQualifiedNames) > 0 {
+ schemaName = extractSchemaName(allQualifiedNames[0])
+ tableName = extractTableName(allQualifiedNames[0])
+ }
+
+ // Check 3: Require explicit schema name
+ if schemaName == "" {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireSchemaName.Int32(),
+ Title: "sdl.require-schema-name",
+ Content: fmt.Sprintf(
+ "Table '%s' is created without explicit schema name.\n\n"+
+ "SDL requires all objects to specify their schema explicitly.\n\n"+
+ "Example - Instead of:\n"+
+ " CREATE TABLE %s (...);\n\n"+
+ "Use:\n"+
+ " CREATE TABLE schema_name.%s (...);",
+ tableName, tableName, tableName,
+ ),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+
+ // Check all column definitions and table constraints
+ if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {
+ allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
+ for _, elem := range allElements {
+ // Check 1: Disallow column-level constraints
+ if elem.ColumnDef() != nil {
+ c.checkColumnConstraints(elem.ColumnDef(), tableName)
+ }
+ // Check 2: Require constraint names for table constraints
+ if elem.Tableconstraint() != nil {
+ c.checkTableConstraintName(elem.Tableconstraint(), tableName)
+ // Check 3: Check foreign key reference tables have schema names
+ c.checkForeignKeyReferenceSchema(elem.Tableconstraint(), tableName)
+ }
+ }
+ }
+}
+
+// EnterIndexstmt handles CREATE INDEX
+func (c *sdlChecker) EnterIndexstmt(ctx *parser.IndexstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Get index name if available
+ var indexName string
+ if ctx.Name() != nil && ctx.Name().Colid() != nil {
+ indexName = pgparser.NormalizePostgreSQLColid(ctx.Name().Colid())
+ }
+
+ // Check 1: Disallow unnamed indexes
+ if indexName == "" {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireIndexName.Int32(),
+ Title: "sdl.require-index-name",
+ Content: "Index is created without an explicit name.\n\n" +
+ "SDL requires all indexes to have explicit names.\n\n" +
+ "Example - Instead of:\n" +
+ " CREATE INDEX ON table_name (column);\n\n" +
+ "Use:\n" +
+ " CREATE INDEX idx_table_column ON table_name (column);",
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+
+ // Check 2: Require schema name on table
+ if ctx.Relation_expr() != nil && ctx.Relation_expr().Qualified_name() != nil {
+ schemaName := extractSchemaName(ctx.Relation_expr().Qualified_name())
+ tableName := extractTableName(ctx.Relation_expr().Qualified_name())
+
+ if schemaName == "" {
+ var content string
+ if indexName != "" {
+ content = fmt.Sprintf(
+ "Index '%s' is created on table '%s' without explicit schema name.\n\n"+
+ "SDL requires all objects to specify their schema explicitly.\n\n"+
+ "Example - Instead of:\n"+
+ " CREATE INDEX %s ON %s (...);\n\n"+
+ "Use:\n"+
+ " CREATE INDEX %s ON schema_name.%s (...);",
+ indexName, tableName,
+ indexName, tableName,
+ indexName, tableName,
+ )
+ } else {
+ // Unnamed index (already reported above, but still check table schema)
+ content = fmt.Sprintf(
+ "Index is created on table '%s' without explicit schema name.\n\n"+
+ "SDL requires all objects to specify their schema explicitly.\n\n"+
+ "Example - Use:\n"+
+ " CREATE INDEX idx_name ON schema_name.%s (...);",
+ tableName,
+ tableName,
+ )
+ }
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireSchemaName.Int32(),
+ Title: "sdl.require-schema-name",
+ Content: content,
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+// EnterViewstmt handles CREATE VIEW
+func (c *sdlChecker) EnterViewstmt(ctx *parser.ViewstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ if ctx.Qualified_name() != nil {
+ schemaName := extractSchemaName(ctx.Qualified_name())
+ viewName := extractTableName(ctx.Qualified_name())
+ if schemaName == "" {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireSchemaName.Int32(),
+ Title: "sdl.require-schema-name",
+ Content: fmt.Sprintf(
+ "View '%s' is created without explicit schema name.\n\n"+
+ "SDL requires all objects to specify their schema explicitly.\n\n"+
+ "Example - Instead of:\n"+
+ " CREATE VIEW %s AS ...;\n\n"+
+ "Use:\n"+
+ " CREATE VIEW schema_name.%s AS ...;",
+ viewName, viewName, viewName,
+ ),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+// EnterCreateseqstmt handles CREATE SEQUENCE
+func (c *sdlChecker) EnterCreateseqstmt(ctx *parser.CreateseqstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ if ctx.Qualified_name() != nil {
+ schemaName := extractSchemaName(ctx.Qualified_name())
+ sequenceName := extractTableName(ctx.Qualified_name())
+ if schemaName == "" {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireSchemaName.Int32(),
+ Title: "sdl.require-schema-name",
+ Content: fmt.Sprintf(
+ "Sequence '%s' is created without explicit schema name.\n\n"+
+ "SDL requires all objects to specify their schema explicitly.\n\n"+
+ "Example - Instead of:\n"+
+ " CREATE SEQUENCE %s ...;\n\n"+
+ "Use:\n"+
+ " CREATE SEQUENCE schema_name.%s ...;",
+ sequenceName, sequenceName, sequenceName,
+ ),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+// EnterCreatefunctionstmt handles CREATE FUNCTION
+func (c *sdlChecker) EnterCreatefunctionstmt(ctx *parser.CreatefunctionstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ if ctx.Func_name() != nil {
+ schemaName, functionName := c.extractSchemaAndNameFromFuncName(ctx.Func_name())
+ if schemaName == "" {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireSchemaName.Int32(),
+ Title: "sdl.require-schema-name",
+ Content: fmt.Sprintf(
+ "Function '%s' is created without explicit schema name.\n\n"+
+ "SDL requires all objects to specify their schema explicitly.\n\n"+
+ "Example - Instead of:\n"+
+ " CREATE FUNCTION %s(...) ...;\n\n"+
+ "Use:\n"+
+ " CREATE FUNCTION schema_name.%s(...) ...;",
+ functionName, functionName, functionName,
+ ),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+// EnterAlterseqstmt handles ALTER SEQUENCE
+func (c *sdlChecker) EnterAlterseqstmt(ctx *parser.AlterseqstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ if ctx.Qualified_name() != nil {
+ schemaName := extractSchemaName(ctx.Qualified_name())
+ sequenceName := extractTableName(ctx.Qualified_name())
+ if schemaName == "" {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireSchemaName.Int32(),
+ Title: "sdl.require-schema-name",
+ Content: fmt.Sprintf(
+ "Sequence '%s' is altered without explicit schema name.\n\n"+
+ "SDL requires all objects to specify their schema explicitly.\n\n"+
+ "Example - Instead of:\n"+
+ " ALTER SEQUENCE %s ...;\n\n"+
+ "Use:\n"+
+ " ALTER SEQUENCE schema_name.%s ...;",
+ sequenceName, sequenceName, sequenceName,
+ ),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+// checkColumnConstraints checks a column definition for disallowed constraints.
+// SDL allows NOT NULL, DEFAULT, GENERATED at column level.
+// SDL disallows PRIMARY KEY, UNIQUE, CHECK, FOREIGN KEY at column level.
+func (c *sdlChecker) checkColumnConstraints(columnDef parser.IColumnDefContext, tableName string) {
+ if columnDef == nil {
+ return
+ }
+
+ var columnName string
+ if columnDef.Colid() != nil {
+ columnName = pgparser.NormalizePostgreSQLColid(columnDef.Colid())
+ }
+
+ // Check if column has any disallowed constraints
+ if columnDef.Colquallist() != nil {
+ allConstraints := columnDef.Colquallist().AllColconstraint()
+ var disallowedConstraints []parser.IColconstraintContext
+ var disallowedConstraintTypes []string
+
+ for _, constraint := range allConstraints {
+ if constraint.Colconstraintelem() != nil {
+ elem := constraint.Colconstraintelem()
+ // Check if this is a disallowed constraint type
+ if c.isDisallowedColumnConstraint(elem) {
+ disallowedConstraints = append(disallowedConstraints, constraint)
+ disallowedConstraintTypes = append(disallowedConstraintTypes, c.getConstraintTypeName(elem))
+ }
+ // Also check FK reference schema even though FK is disallowed at column level
+ // This provides a more specific error message
+ c.checkColumnForeignKeyReferenceSchema(elem, columnName, tableName)
+ }
+ }
+
+ if len(disallowedConstraints) > 0 {
+ firstConstraint := disallowedConstraints[0]
+ exampleConstraint := disallowedConstraintTypes[0]
+
+ content := fmt.Sprintf(
+ "Column '%s' in table '%s' has disallowed column-level constraint(s): %v.\n\n"+
+ "SDL only allows NOT NULL, DEFAULT, and GENERATED constraints at the column level.\n"+
+ "PRIMARY KEY, UNIQUE, CHECK, and FOREIGN KEY constraints must be defined at the table level.\n\n"+
+ "Example - Instead of:\n"+
+ " CREATE TABLE %s (\n"+
+ " %s INTEGER %s\n"+
+ " );\n\n"+
+ "Use table-level constraint:\n"+
+ " CREATE TABLE %s (\n"+
+ " %s INTEGER,\n"+
+ " CONSTRAINT constraint_name %s (%s)\n"+
+ " );",
+ columnName, tableName, disallowedConstraintTypes,
+ tableName, columnName, exampleConstraint,
+ tableName, columnName, exampleConstraint, columnName,
+ )
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLDisallowColumnConstraint.Int32(),
+ Title: "sdl.disallow-column-constraint",
+ Content: content,
+ StartPosition: &storepb.Position{
+ Line: int32(firstConstraint.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+// isDisallowedColumnConstraint returns true if the constraint is not allowed at column level in SDL.
+// Allowed: NOT NULL, NULL, DEFAULT, GENERATED
+// Disallowed: PRIMARY KEY, UNIQUE, CHECK, FOREIGN KEY
+func (*sdlChecker) isDisallowedColumnConstraint(elem parser.IColconstraintelemContext) bool {
+ if elem == nil {
+ return false
+ }
+ // Disallowed constraints
+ if elem.PRIMARY() != nil && elem.KEY() != nil {
+ return true // PRIMARY KEY
+ }
+ if elem.UNIQUE() != nil {
+ return true // UNIQUE
+ }
+ if elem.CHECK() != nil {
+ return true // CHECK
+ }
+ if elem.REFERENCES() != nil {
+ return true // FOREIGN KEY
+ }
+ // Allowed constraints: NOT NULL, NULL, DEFAULT, GENERATED
+ return false
+}
+
+// checkTableConstraintName checks if a table constraint has an explicit name.
+func (c *sdlChecker) checkTableConstraintName(constraint parser.ITableconstraintContext, tableName string) {
+ if constraint == nil {
+ return
+ }
+
+ // Check if constraint has a name using CONSTRAINT keyword
+ hasConstraintName := false
+ if constraint.CONSTRAINT() != nil {
+ if constraint.Name() != nil && constraint.Name().Colid() != nil {
+ hasConstraintName = true
+ }
+ }
+
+ if !hasConstraintName {
+ constraintType := c.getTableConstraintType(constraint)
+
+ content := fmt.Sprintf(
+ "Table '%s' has a %s constraint without an explicit name.\n\n"+
+ "SDL requires all table constraints to have explicit names using the CONSTRAINT keyword.\n\n"+
+ "Example - Instead of:\n"+
+ " CREATE TABLE %s (\n"+
+ " id INTEGER,\n"+
+ " %s\n"+
+ " );\n\n"+
+ "Use named constraint:\n"+
+ " CREATE TABLE %s (\n"+
+ " id INTEGER,\n"+
+ " CONSTRAINT constraint_name %s\n"+
+ " );",
+ tableName, constraintType,
+ tableName, c.getConstraintExample(constraint, false),
+ tableName, c.getConstraintExample(constraint, true),
+ )
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireConstraintName.Int32(),
+ Title: "sdl.require-constraint-name",
+ Content: content,
+ StartPosition: &storepb.Position{
+ Line: int32(constraint.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+}
+
+// checkForeignKeyReferenceSchema checks if a FOREIGN KEY constraint references a table with explicit schema name.
+func (c *sdlChecker) checkForeignKeyReferenceSchema(constraint parser.ITableconstraintContext, tableName string) {
+ if constraint == nil || constraint.Constraintelem() == nil {
+ return
+ }
+
+ elem := constraint.Constraintelem()
+ // Check if this is a FOREIGN KEY constraint
+ if elem.FOREIGN() == nil || elem.KEY() == nil {
+ return
+ }
+
+ // Check the referenced table (REFERENCES clause)
+ if elem.Qualified_name() != nil {
+ refSchemaName := extractSchemaName(elem.Qualified_name())
+ refTableName := extractTableName(elem.Qualified_name())
+
+ if refSchemaName == "" {
+ content := fmt.Sprintf(
+ "Foreign key constraint in table '%s' references table '%s' without explicit schema name.\n\n"+
+ "SDL requires all table references in foreign keys to specify their schema explicitly.\n\n"+
+ "Example - Instead of:\n"+
+ " CREATE TABLE %s (\n"+
+ " ...\n"+
+ " CONSTRAINT fk_name FOREIGN KEY (column) REFERENCES %s(id)\n"+
+ " );\n\n"+
+ "Use:\n"+
+ " CREATE TABLE %s (\n"+
+ " ...\n"+
+ " CONSTRAINT fk_name FOREIGN KEY (column) REFERENCES schema_name.%s(id)\n"+
+ " );",
+ tableName, refTableName,
+ tableName, refTableName,
+ tableName, refTableName,
+ )
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireSchemaName.Int32(),
+ Title: "sdl.require-schema-name",
+ Content: content,
+ StartPosition: &storepb.Position{
+ Line: int32(constraint.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+// checkColumnForeignKeyReferenceSchema checks if a column-level FOREIGN KEY references a table with explicit schema name.
+func (c *sdlChecker) checkColumnForeignKeyReferenceSchema(elem parser.IColconstraintelemContext, columnName, tableName string) {
+ if elem == nil {
+ return
+ }
+
+ // Check if this is a FOREIGN KEY constraint (REFERENCES)
+ if elem.REFERENCES() == nil {
+ return
+ }
+
+ // Check the referenced table
+ if elem.Qualified_name() != nil {
+ refSchemaName := extractSchemaName(elem.Qualified_name())
+ refTableName := extractTableName(elem.Qualified_name())
+
+ if refSchemaName == "" {
+ content := fmt.Sprintf(
+ "Column '%s' in table '%s' has a foreign key reference to table '%s' without explicit schema name.\n\n"+
+ "SDL requires all table references in foreign keys to specify their schema explicitly.\n"+
+ "Note: Foreign keys should be defined at table level, not column level.\n\n"+
+ "Example - Instead of:\n"+
+ " CREATE TABLE %s (\n"+
+ " %s INTEGER REFERENCES %s(id)\n"+
+ " );\n\n"+
+ "Use table-level constraint with schema:\n"+
+ " CREATE TABLE %s (\n"+
+ " %s INTEGER,\n"+
+ " CONSTRAINT fk_name FOREIGN KEY (%s) REFERENCES schema_name.%s(id)\n"+
+ " );",
+ columnName, tableName, refTableName,
+ tableName, columnName, refTableName,
+ tableName, columnName, columnName, refTableName,
+ )
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLRequireSchemaName.Int32(),
+ Title: "sdl.require-schema-name",
+ Content: content,
+ StartPosition: &storepb.Position{
+ Line: int32(elem.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+// Helper functions
+
+func (*sdlChecker) getConstraintTypeName(elem parser.IColconstraintelemContext) string {
+ if elem == nil {
+ return "UNKNOWN"
+ }
+ if elem.PRIMARY() != nil && elem.KEY() != nil {
+ return "PRIMARY KEY"
+ }
+ if elem.UNIQUE() != nil {
+ return "UNIQUE"
+ }
+ if elem.NOT() != nil && elem.NULL_P() != nil {
+ return "NOT NULL"
+ }
+ if elem.NULL_P() != nil {
+ return "NULL"
+ }
+ if elem.CHECK() != nil {
+ return "CHECK"
+ }
+ if elem.DEFAULT() != nil {
+ return "DEFAULT"
+ }
+ if elem.REFERENCES() != nil {
+ return "FOREIGN KEY"
+ }
+ if elem.GENERATED() != nil {
+ return "GENERATED"
+ }
+ return "CONSTRAINT"
+}
+
+func (*sdlChecker) getTableConstraintType(constraint parser.ITableconstraintContext) string {
+ if constraint.Constraintelem() == nil {
+ return "CONSTRAINT"
+ }
+ elem := constraint.Constraintelem()
+ if elem.PRIMARY() != nil && elem.KEY() != nil {
+ return "PRIMARY KEY"
+ }
+ if elem.UNIQUE() != nil {
+ return "UNIQUE"
+ }
+ if elem.CHECK() != nil {
+ return "CHECK"
+ }
+ if elem.FOREIGN() != nil && elem.KEY() != nil {
+ return "FOREIGN KEY"
+ }
+ if elem.EXCLUDE() != nil {
+ return "EXCLUDE"
+ }
+ return "CONSTRAINT"
+}
+
+func (*sdlChecker) getConstraintExample(constraint parser.ITableconstraintContext, _ bool) string {
+ if constraint.Constraintelem() == nil {
+ return "..."
+ }
+ elem := constraint.Constraintelem()
+ if elem.PRIMARY() != nil && elem.KEY() != nil {
+ return "PRIMARY KEY (id)"
+ }
+ if elem.UNIQUE() != nil {
+ return "UNIQUE (column)"
+ }
+ if elem.CHECK() != nil {
+ return "CHECK (condition)"
+ }
+ if elem.FOREIGN() != nil && elem.KEY() != nil {
+ return "FOREIGN KEY (column) REFERENCES other_table(id)"
+ }
+ return "..."
+}
+
+func (*sdlChecker) extractSchemaAndNameFromFuncName(funcName parser.IFunc_nameContext) (string, string) {
+ if funcName.Type_function_name() != nil {
+ return "", funcName.Type_function_name().GetText()
+ }
+ if funcName.Indirection() != nil {
+ parts := []string{}
+ if funcName.Colid() != nil {
+ parts = append(parts, funcName.Colid().GetText())
+ }
+ for _, attr := range funcName.Indirection().AllIndirection_el() {
+ if attr.Attr_name() != nil {
+ parts = append(parts, attr.Attr_name().GetText())
+ }
+ }
+ if len(parts) == 1 {
+ return "", parts[0]
+ }
+ if len(parts) >= 2 {
+ return parts[0], parts[1]
+ }
+ }
+ return "", ""
+}
diff --git a/backend/plugin/advisor/pg/sdl_check_test.go b/backend/plugin/advisor/pg/sdl_check_test.go
new file mode 100644
index 0000000000..7ffbc349a8
--- /dev/null
+++ b/backend/plugin/advisor/pg/sdl_check_test.go
@@ -0,0 +1,451 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+)
+
+func TestCheckSDL(t *testing.T) {
+ tests := []struct {
+ name string
+ statement string
+ wantCount int
+ wantCodes []advisor.Code
+ }{
+ {
+ name: "Valid SDL - table with schema name, table-level named constraints",
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ email TEXT,
+ CONSTRAINT pk_users PRIMARY KEY (id),
+ CONSTRAINT uk_users_email UNIQUE (email)
+ );`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Valid SDL - table with allowed column-level constraints (NOT NULL, DEFAULT)",
+ statement: `CREATE TABLE public.users (
+ id INTEGER NOT NULL DEFAULT 0,
+ email TEXT NOT NULL,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Valid SDL - table with GENERATED column constraint",
+ statement: `CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ email_lower TEXT GENERATED ALWAYS AS (LOWER(email)) STORED,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - Missing schema name for table",
+ statement: `CREATE TABLE users (
+ id INTEGER,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
+ },
+ {
+ name: "Error - Column-level PRIMARY KEY constraint",
+ statement: `CREATE TABLE public.users (
+ id INTEGER PRIMARY KEY,
+ email TEXT
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDisallowColumnConstraint},
+ },
+ {
+ name: "Error - Column-level UNIQUE constraint",
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ email TEXT UNIQUE
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDisallowColumnConstraint},
+ },
+ {
+ name: "Error - Column-level CHECK constraint",
+ statement: `CREATE TABLE public.users (
+ id INTEGER CHECK (id > 0),
+ email TEXT
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDisallowColumnConstraint},
+ },
+ {
+ name: "Error - Column-level FOREIGN KEY constraint",
+ statement: `CREATE TABLE public.orders (
+ id INTEGER,
+ user_id INTEGER REFERENCES public.users(id)
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDisallowColumnConstraint},
+ },
+ {
+ name: "Error - Table-level constraint without name",
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ PRIMARY KEY (id)
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireConstraintName},
+ },
+ {
+ name: "Error - Table-level UNIQUE without name",
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ email TEXT,
+ UNIQUE (email)
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireConstraintName},
+ },
+ {
+ name: "Error - Table-level CHECK without name",
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ CHECK (id > 0)
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireConstraintName},
+ },
+ {
+ name: "Error - Table-level FOREIGN KEY without name",
+ statement: `CREATE TABLE public.orders (
+ id INTEGER,
+ user_id INTEGER,
+ FOREIGN KEY (user_id) REFERENCES public.users(id)
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireConstraintName},
+ },
+ {
+ name: "Error - Multiple disallowed column-level constraints on one column",
+ statement: `CREATE TABLE public.users (
+ id INTEGER PRIMARY KEY UNIQUE CHECK (id > 0),
+ email TEXT
+ );`,
+ wantCount: 1, // One column with multiple disallowed constraints
+ wantCodes: []advisor.Code{
+ advisor.SDLDisallowColumnConstraint,
+ },
+ },
+ {
+ name: "Error - Multiple columns with disallowed constraints",
+ statement: `CREATE TABLE public.users (
+ id INTEGER PRIMARY KEY,
+ email TEXT UNIQUE
+ );`,
+ wantCount: 2, // Two columns with disallowed constraints
+ wantCodes: []advisor.Code{
+ advisor.SDLDisallowColumnConstraint,
+ advisor.SDLDisallowColumnConstraint,
+ },
+ },
+ {
+ name: "Valid SDL - Mix of allowed column constraints with table-level constraints",
+ statement: `CREATE TABLE public.users (
+ id INTEGER NOT NULL DEFAULT 0,
+ email TEXT NOT NULL,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ CONSTRAINT pk_users PRIMARY KEY (id),
+ CONSTRAINT uk_users_email UNIQUE (email)
+ );`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - Missing schema name and unnamed constraint",
+ statement: `CREATE TABLE users (
+ id INTEGER,
+ PRIMARY KEY (id)
+ );`,
+ wantCount: 2,
+ wantCodes: []advisor.Code{
+ advisor.SDLRequireSchemaName,
+ advisor.SDLRequireConstraintName,
+ },
+ },
+ {
+ name: "Error - All three types of errors",
+ statement: `CREATE TABLE users (
+ id INTEGER PRIMARY KEY,
+ UNIQUE (id)
+ );`,
+ wantCount: 3,
+ wantCodes: []advisor.Code{
+ advisor.SDLRequireSchemaName,
+ advisor.SDLDisallowColumnConstraint,
+ advisor.SDLRequireConstraintName,
+ },
+ },
+ {
+ name: "Valid SDL - CREATE INDEX with schema name",
+ statement: `CREATE INDEX idx_users_email ON public.users (email);`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - CREATE INDEX without schema name",
+ statement: `CREATE INDEX idx_users_email ON users (email);`,
+ 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);`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - CREATE UNIQUE INDEX without schema name",
+ statement: `CREATE UNIQUE INDEX idx_users_email ON users (email);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
+ },
+ {
+ name: "Error - CREATE INDEX (unnamed) without schema name",
+ statement: `CREATE INDEX ON users (email);`,
+ wantCount: 2, // Both: missing index name AND missing schema name
+ wantCodes: []advisor.Code{
+ advisor.SDLRequireIndexName,
+ advisor.SDLRequireSchemaName,
+ },
+ },
+ {
+ name: "Error - CREATE INDEX (unnamed) with schema name",
+ statement: `CREATE INDEX ON public.users (email);`,
+ wantCount: 1, // Only missing index name
+ wantCodes: []advisor.Code{advisor.SDLRequireIndexName},
+ },
+ {
+ name: "Valid SDL - CREATE VIEW with schema name",
+ statement: `CREATE VIEW public.active_users AS SELECT * FROM public.users WHERE active = true;`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - CREATE VIEW without schema name",
+ statement: `CREATE VIEW active_users AS SELECT * FROM public.users WHERE active = true;`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
+ },
+ {
+ name: "Valid SDL - CREATE SEQUENCE with schema name",
+ statement: `CREATE SEQUENCE public.user_id_seq START 1;`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - CREATE SEQUENCE without schema name",
+ statement: `CREATE SEQUENCE user_id_seq START 1;`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
+ },
+ {
+ name: "Valid SDL - CREATE FUNCTION with schema name",
+ statement: `CREATE FUNCTION public.get_user_count() RETURNS INTEGER AS $$
+ SELECT COUNT(*) FROM public.users;
+ $$ LANGUAGE SQL;`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - CREATE FUNCTION without schema name",
+ statement: `CREATE FUNCTION get_user_count() RETURNS INTEGER AS $$
+ SELECT COUNT(*) FROM public.users;
+ $$ LANGUAGE SQL;`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
+ },
+ {
+ name: "Valid SDL - ALTER SEQUENCE with schema name",
+ statement: `ALTER SEQUENCE public.user_id_seq OWNED BY public.users.id;`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - ALTER SEQUENCE without schema name",
+ statement: `ALTER SEQUENCE user_id_seq OWNED BY public.users.id;`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
+ },
+ {
+ name: "Valid SDL - ALTER SEQUENCE with other options",
+ statement: `ALTER SEQUENCE public.user_id_seq INCREMENT BY 5 MINVALUE 1 MAXVALUE 1000;`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Valid SDL - Multiple statements all valid",
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ email TEXT,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );
+
+ CREATE INDEX idx_users_email ON public.users (email);
+
+ CREATE SEQUENCE public.user_id_seq START 1;`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - Multiple statements with multiple errors",
+ statement: `CREATE TABLE users (
+ id INTEGER PRIMARY KEY
+ );
+
+ CREATE INDEX idx_users_email ON users (email);
+
+ CREATE VIEW active_users AS SELECT * FROM users;`,
+ wantCount: 4, // schema name for table + column constraint + schema name for index + schema name for view
+ wantCodes: []advisor.Code{
+ advisor.SDLRequireSchemaName, // table
+ advisor.SDLDisallowColumnConstraint, // column constraint
+ advisor.SDLRequireSchemaName, // index
+ advisor.SDLRequireSchemaName, // view
+ },
+ },
+ {
+ name: "Valid SDL - Complex table with multiple named constraints",
+ statement: `CREATE TABLE public.orders (
+ id INTEGER,
+ user_id INTEGER,
+ product_id INTEGER,
+ quantity INTEGER,
+ total_price NUMERIC,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES public.users(id),
+ CONSTRAINT fk_orders_product FOREIGN KEY (product_id) REFERENCES public.products(id),
+ CONSTRAINT uk_orders_composite UNIQUE (user_id, product_id),
+ CONSTRAINT chk_orders_quantity CHECK (quantity > 0),
+ CONSTRAINT chk_orders_price CHECK (total_price >= 0)
+ );`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ {
+ name: "Error - FK reference without schema name (table-level)",
+ statement: `CREATE TABLE public.orders (
+ id INTEGER,
+ user_id INTEGER,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES users(id)
+ );`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
+ },
+ {
+ name: "Error - FK reference without schema name (column-level)",
+ statement: `CREATE TABLE public.orders (
+ id INTEGER,
+ user_id INTEGER REFERENCES users(id)
+ );`,
+ wantCount: 2, // Both: disallowed column constraint AND missing schema
+ wantCodes: []advisor.Code{
+ advisor.SDLRequireSchemaName,
+ advisor.SDLDisallowColumnConstraint,
+ },
+ },
+ {
+ name: "Error - Multiple FK references without schema",
+ statement: `CREATE TABLE public.orders (
+ id INTEGER,
+ user_id INTEGER,
+ product_id INTEGER,
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES users(id),
+ CONSTRAINT fk_orders_product FOREIGN KEY (product_id) REFERENCES products(id)
+ );`,
+ wantCount: 2, // Two FK references without schema
+ wantCodes: []advisor.Code{
+ advisor.SDLRequireSchemaName,
+ advisor.SDLRequireSchemaName,
+ },
+ },
+ {
+ name: "Valid SDL - Mix of FK with and without errors",
+ statement: `CREATE TABLE public.orders (
+ id INTEGER,
+ user_id INTEGER,
+ product_id INTEGER,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES public.users(id),
+ CONSTRAINT fk_orders_product FOREIGN KEY (product_id) REFERENCES public.products(id)
+ );`,
+ wantCount: 0,
+ wantCodes: nil,
+ },
+ }
+
+ 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")
+ require.Equal(t, tc.wantCount, len(advices), "Unexpected number of advices")
+
+ if tc.wantCount > 0 {
+ require.NotNil(t, tc.wantCodes, "wantCodes should be specified when wantCount > 0")
+ require.Equal(t, len(tc.wantCodes), len(advices), "Number of advices should match wantCodes")
+
+ // Verify each advice has the expected error code and ERROR status
+ for i, advice := range advices {
+ require.Equal(t, storepb.Advice_ERROR, advice.Status, "All SDL check advices should have ERROR status")
+ require.Equal(t, tc.wantCodes[i].Int32(), advice.Code, "Advice %d should have expected error code", i)
+ require.NotEmpty(t, advice.Title, "Advice should have a title")
+ require.NotEmpty(t, advice.Content, "Advice should have content")
+ require.NotNil(t, advice.StartPosition, "Advice should have start position")
+ require.Greater(t, advice.StartPosition.Line, int32(0), "Line number should be positive")
+ }
+ }
+ })
+ }
+}
+
+func TestCheckSDL_InvalidSQL(t *testing.T) {
+ tests := []struct {
+ name string
+ statement string
+ }{
+ {
+ name: "Syntax error - missing parenthesis",
+ statement: `CREATE TABLE public.users (id INTEGER;`,
+ },
+ {
+ name: "Syntax error - invalid keyword",
+ statement: `CREATE TABLEX public.users (id INTEGER);`,
+ },
+ }
+
+ 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")
+ })
+ }
+}
+
+func TestCheckSDL_EmptyStatement(t *testing.T) {
+ advices, err := CheckSDL("")
+ 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")
+ require.NoError(t, err)
+ require.Empty(t, advices, "Comment-only statement should return no advices")
+}
diff --git a/backend/tests/action_test.go b/backend/tests/action_test.go
index 748411d89d..6be1991f93 100644
--- a/backend/tests/action_test.go
+++ b/backend/tests/action_test.go
@@ -298,10 +298,12 @@ func TestActionCheckCommand(t *testing.T) {
// Create test data directory and schema.sql file
testDataDir := t.TempDir()
- schemaContent := `CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(255) NOT NULL UNIQUE,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+ schemaContent := `CREATE TABLE public.users (
+ id SERIAL NOT NULL,
+ username VARCHAR(255) NOT NULL,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ CONSTRAINT pk_users PRIMARY KEY (id),
+ CONSTRAINT uk_users_username UNIQUE (username)
);`
schemaFile := filepath.Join(testDataDir, "schema.sql")
err = os.WriteFile(schemaFile, []byte(schemaContent), 0644)
@@ -341,20 +343,23 @@ func TestActionCheckCommand(t *testing.T) {
testDataDir := t.TempDir()
// Create users.sql
- usersContent := `CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- username VARCHAR(255) NOT NULL UNIQUE,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+ usersContent := `CREATE TABLE public.users (
+ id SERIAL NOT NULL,
+ username VARCHAR(255) NOT NULL,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ CONSTRAINT pk_users PRIMARY KEY (id),
+ CONSTRAINT uk_users_username UNIQUE (username)
);`
usersFile := filepath.Join(testDataDir, "users.sql")
err = os.WriteFile(usersFile, []byte(usersContent), 0644)
a.NoError(err)
// Create products.sql
- productsContent := `CREATE TABLE products (
- id SERIAL PRIMARY KEY,
+ productsContent := `CREATE TABLE public.products (
+ id SERIAL NOT NULL,
name VARCHAR(255) NOT NULL,
- price DECIMAL(10,2)
+ price DECIMAL(10,2),
+ CONSTRAINT pk_products PRIMARY KEY (id)
);`
productsFile := filepath.Join(testDataDir, "products.sql")
err = os.WriteFile(productsFile, []byte(productsContent), 0644)