advisor

package
v1.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package advisor provides a simplified wrapper around Bytebase's SQL advisor functionality.

Package advisor provides SQL review rules definitions.

Index

Constants

View Source
const (
	EngineMySQL     = storepb.Engine_MYSQL
	EnginePostgres  = storepb.Engine_POSTGRES
	EngineTiDB      = storepb.Engine_TIDB
	EngineOracle    = storepb.Engine_ORACLE
	EngineMSSQL     = storepb.Engine_MSSQL
	EngineMariaDB   = storepb.Engine_MARIADB
	EngineSnowflake = storepb.Engine_SNOWFLAKE
	EngineOceanBase = storepb.Engine_OCEANBASE
)

Engine constants for convenience.

View Source
const (
	RuleLevelError   = storepb.SQLReviewRuleLevel_ERROR
	RuleLevelWarning = storepb.SQLReviewRuleLevel_WARNING
	// RuleLevelDisabled uses LEVEL_UNSPECIFIED to indicate a disabled rule
	RuleLevelDisabled = storepb.SQLReviewRuleLevel_LEVEL_UNSPECIFIED
)

Rule level constants.

View Source
const (
	AdviceStatusSuccess = storepb.Advice_SUCCESS
	AdviceStatusWarning = storepb.Advice_WARNING
	AdviceStatusError   = storepb.Advice_ERROR
)

Advice status constants.

View Source
const (
	// Engine rules
	RuleMySQLEngine = string(advisor.SchemaRuleMySQLEngine)

	// Naming rules
	RuleFullyQualifiedObjectName  = string(advisor.SchemaRuleFullyQualifiedObjectName)
	RuleTableNaming               = string(advisor.SchemaRuleTableNaming)
	RuleColumnNaming              = string(advisor.SchemaRuleColumnNaming)
	RulePKNaming                  = string(advisor.SchemaRulePKNaming)
	RuleUKNaming                  = string(advisor.SchemaRuleUKNaming)
	RuleFKNaming                  = string(advisor.SchemaRuleFKNaming)
	RuleIDXNaming                 = string(advisor.SchemaRuleIDXNaming)
	RuleAutoIncrementColumnNaming = string(advisor.SchemaRuleAutoIncrementColumnNaming)
	RuleTableNameNoKeyword        = string(advisor.SchemaRuleTableNameNoKeyword)
	RuleIdentifierNoKeyword       = string(advisor.SchemaRuleIdentifierNoKeyword)
	RuleIdentifierCase            = string(advisor.SchemaRuleIdentifierCase)

	// Statement rules
	RuleStatementNoSelectAll                  = string(advisor.SchemaRuleStatementNoSelectAll)
	RuleStatementRequireWhereForSelect        = string(advisor.SchemaRuleStatementRequireWhereForSelect)
	RuleStatementRequireWhereForUpdateDelete  = string(advisor.SchemaRuleStatementRequireWhereForUpdateDelete)
	RuleStatementNoLeadingWildcardLike        = string(advisor.SchemaRuleStatementNoLeadingWildcardLike)
	RuleStatementDisallowOnDelCascade         = string(advisor.SchemaRuleStatementDisallowOnDelCascade)
	RuleStatementDisallowRemoveTblCascade     = string(advisor.SchemaRuleStatementDisallowRemoveTblCascade)
	RuleStatementDisallowCommit               = string(advisor.SchemaRuleStatementDisallowCommit)
	RuleStatementDisallowLimit                = string(advisor.SchemaRuleStatementDisallowLimit)
	RuleStatementDisallowOrderBy              = string(advisor.SchemaRuleStatementDisallowOrderBy)
	RuleStatementMergeAlterTable              = string(advisor.SchemaRuleStatementMergeAlterTable)
	RuleStatementInsertRowLimit               = string(advisor.SchemaRuleStatementInsertRowLimit)
	RuleStatementInsertMustSpecifyColumn      = string(advisor.SchemaRuleStatementInsertMustSpecifyColumn)
	RuleStatementInsertDisallowOrderByRand    = string(advisor.SchemaRuleStatementInsertDisallowOrderByRand)
	RuleStatementAffectedRowLimit             = string(advisor.SchemaRuleStatementAffectedRowLimit)
	RuleStatementDMLDryRun                    = string(advisor.SchemaRuleStatementDMLDryRun)
	RuleStatementDisallowAddColumnWithDefault = string(advisor.SchemaRuleStatementDisallowAddColumnWithDefault)
	RuleStatementAddCheckNotValid             = string(advisor.SchemaRuleStatementAddCheckNotValid)
	RuleStatementAddFKNotValid                = string(advisor.SchemaRuleStatementAddFKNotValid)
	RuleStatementDisallowAddNotNull           = string(advisor.SchemaRuleStatementDisallowAddNotNull)
	RuleStatementSelectFullTableScan          = string(advisor.SchemaRuleStatementSelectFullTableScan)
	RuleStatementCreateSpecifySchema          = string(advisor.SchemaRuleStatementCreateSpecifySchema)
	RuleStatementCheckSetRoleVariable         = string(advisor.SchemaRuleStatementCheckSetRoleVariable)
	RuleStatementDisallowUsingFilesort        = string(advisor.SchemaRuleStatementDisallowUsingFilesort)
	RuleStatementDisallowUsingTemporary       = string(advisor.SchemaRuleStatementDisallowUsingTemporary)
	RuleStatementWhereNoEqualNull             = string(advisor.SchemaRuleStatementWhereNoEqualNull)
	RuleStatementWhereDisallowFunctions       = string(advisor.SchemaRuleStatementWhereDisallowFunctionsAndCalculations)
	RuleStatementQueryMinimumPlanLevel        = string(advisor.SchemaRuleStatementQueryMinumumPlanLevel)
	RuleStatementWhereMaxLogicalOperatorCount = string(advisor.SchemaRuleStatementWhereMaximumLogicalOperatorCount)
	RuleStatementMaximumLimitValue            = string(advisor.SchemaRuleStatementMaximumLimitValue)
	RuleStatementMaximumJoinTableCount        = string(advisor.SchemaRuleStatementMaximumJoinTableCount)
	RuleStatementMaxStatementsInTransaction   = string(advisor.SchemaRuleStatementMaximumStatementsInTransaction)
	RuleStatementJoinStrictColumnAttrs        = string(advisor.SchemaRuleStatementJoinStrictColumnAttrs)
	RuleStatementPriorBackupCheck             = string(advisor.SchemaRuleStatementPriorBackupCheck)
	RuleStatementNonTransactional             = string(advisor.SchemaRuleStatementNonTransactional)
	RuleStatementAddColumnWithoutPosition     = string(advisor.SchemaRuleStatementAddColumnWithoutPosition)
	RuleStatementDisallowOfflineDDL           = string(advisor.SchemaRuleStatementDisallowOfflineDDL)
	RuleStatementDisallowCrossDBQueries       = string(advisor.SchemaRuleStatementDisallowCrossDBQueries)
	RuleStatementMaxExecutionTime             = string(advisor.SchemaRuleStatementMaxExecutionTime)
	RuleStatementRequireAlgorithmOption       = string(advisor.SchemaRuleStatementRequireAlgorithmOption)
	RuleStatementRequireLockOption            = string(advisor.SchemaRuleStatementRequireLockOption)
	RuleStatementObjectOwnerCheck             = string(advisor.SchemaRuleStatementObjectOwnerCheck)

	// Table rules
	RuleTableRequirePK             = string(advisor.SchemaRuleTableRequirePK)
	RuleTableNoFK                  = string(advisor.SchemaRuleTableNoFK)
	RuleTableDropNamingConvention  = string(advisor.SchemaRuleTableDropNamingConvention)
	RuleTableCommentConvention     = string(advisor.SchemaRuleTableCommentConvention)
	RuleTableDisallowPartition     = string(advisor.SchemaRuleTableDisallowPartition)
	RuleTableDisallowTrigger       = string(advisor.SchemaRuleTableDisallowTrigger)
	RuleTableNoDuplicateIndex      = string(advisor.SchemaRuleTableNoDuplicateIndex)
	RuleTableTextFieldsTotalLength = string(advisor.SchemaRuleTableTextFieldsTotalLength)
	RuleTableDisallowSetCharset    = string(advisor.SchemaRuleTableDisallowSetCharset)
	RuleTableDisallowDDL           = string(advisor.SchemaRuleTableDisallowDDL)
	RuleTableDisallowDML           = string(advisor.SchemaRuleTableDisallowDML)
	RuleTableLimitSize             = string(advisor.SchemaRuleTableLimitSize)
	RuleTableRequireCharset        = string(advisor.SchemaRuleTableRequireCharset)
	RuleTableRequireCollation      = string(advisor.SchemaRuleTableRequireCollation)

	// Column rules
	RuleRequiredColumn                  = string(advisor.SchemaRuleRequiredColumn)
	RuleColumnNotNull                   = string(advisor.SchemaRuleColumnNotNull)
	RuleColumnDisallowChangeType        = string(advisor.SchemaRuleColumnDisallowChangeType)
	RuleColumnSetDefaultForNotNull      = string(advisor.SchemaRuleColumnSetDefaultForNotNull)
	RuleColumnDisallowChange            = string(advisor.SchemaRuleColumnDisallowChange)
	RuleColumnDisallowChangingOrder     = string(advisor.SchemaRuleColumnDisallowChangingOrder)
	RuleColumnDisallowDrop              = string(advisor.SchemaRuleColumnDisallowDrop)
	RuleColumnDisallowDropInIndex       = string(advisor.SchemaRuleColumnDisallowDropInIndex)
	RuleColumnCommentConvention         = string(advisor.SchemaRuleColumnCommentConvention)
	RuleColumnAutoIncrementMustInteger  = string(advisor.SchemaRuleColumnAutoIncrementMustInteger)
	RuleColumnTypeDisallowList          = string(advisor.SchemaRuleColumnTypeDisallowList)
	RuleColumnDisallowSetCharset        = string(advisor.SchemaRuleColumnDisallowSetCharset)
	RuleColumnMaximumCharacterLength    = string(advisor.SchemaRuleColumnMaximumCharacterLength)
	RuleColumnMaximumVarcharLength      = string(advisor.SchemaRuleColumnMaximumVarcharLength)
	RuleColumnAutoIncrementInitialValue = string(advisor.SchemaRuleColumnAutoIncrementInitialValue)
	RuleColumnAutoIncrementMustUnsigned = string(advisor.SchemaRuleColumnAutoIncrementMustUnsigned)
	RuleCurrentTimeColumnCountLimit     = string(advisor.SchemaRuleCurrentTimeColumnCountLimit)
	RuleColumnRequireDefault            = string(advisor.SchemaRuleColumnRequireDefault)
	RuleColumnDefaultDisallowVolatile   = string(advisor.SchemaRuleColumnDefaultDisallowVolatile)
	RuleAddNotNullColumnRequireDefault  = string(advisor.SchemaRuleAddNotNullColumnRequireDefault)
	RuleColumnRequireCharset            = string(advisor.SchemaRuleColumnRequireCharset)
	RuleColumnRequireCollation          = string(advisor.SchemaRuleColumnRequireCollation)

	// Schema rules
	RuleSchemaBackwardCompatibility = string(advisor.SchemaRuleSchemaBackwardCompatibility)

	// Database rules
	RuleDropEmptyDatabase = string(advisor.SchemaRuleDropEmptyDatabase)

	// Index rules
	RuleIndexNoDuplicateColumn       = string(advisor.SchemaRuleIndexNoDuplicateColumn)
	RuleIndexKeyNumberLimit          = string(advisor.SchemaRuleIndexKeyNumberLimit)
	RuleIndexPKTypeLimit             = string(advisor.SchemaRuleIndexPKTypeLimit)
	RuleIndexTypeNoBlob              = string(advisor.SchemaRuleIndexTypeNoBlob)
	RuleIndexTotalNumberLimit        = string(advisor.SchemaRuleIndexTotalNumberLimit)
	RuleIndexPrimaryKeyTypeAllowlist = string(advisor.SchemaRuleIndexPrimaryKeyTypeAllowlist)
	RuleCreateIndexConcurrently      = string(advisor.SchemaRuleCreateIndexConcurrently)
	RuleIndexTypeAllowList           = string(advisor.SchemaRuleIndexTypeAllowList)
	RuleIndexNotRedundant            = string(advisor.SchemaRuleIndexNotRedundant)

	// System rules
	RuleCharsetAllowlist        = string(advisor.SchemaRuleCharsetAllowlist)
	RuleCollationAllowlist      = string(advisor.SchemaRuleCollationAllowlist)
	RuleCommentLength           = string(advisor.SchemaRuleCommentLength)
	RuleProcedureDisallowCreate = string(advisor.SchemaRuleProcedureDisallowCreate)
	RuleEventDisallowCreate     = string(advisor.SchemaRuleEventDisallowCreate)
	RuleViewDisallowCreate      = string(advisor.SchemaRuleViewDisallowCreate)
	RuleFunctionDisallowCreate  = string(advisor.SchemaRuleFunctionDisallowCreate)
	RuleFunctionDisallowList    = string(advisor.SchemaRuleFunctionDisallowList)

	// Advice rules
	RuleOnlineMigration = string(advisor.SchemaRuleOnlineMigration)

	// Builtin rules
	BuiltinRulePriorBackupCheck = string(advisor.BuiltinRulePriorBackupCheck)
)

Rule types - re-exported from Bytebase advisor package. These are the rule type identifiers used in configuration files.

Variables

This section is empty.

Functions

func AllRules

func AllRules() []string

AllRules returns all available rule types.

func GetRuleDescription

func GetRuleDescription(ruleType string) string

GetRuleDescription returns a description for the given rule type.

Types

type Advice

type Advice = storepb.Advice

Advice is the alias for storepb.Advice.

type AdviceStatus

type AdviceStatus = storepb.Advice_Status

AdviceStatus is the alias for Advice_Status.

type CommentConventionRulePayload

type CommentConventionRulePayload = advisor.CommentConventionRulePayload

CommentConventionRulePayload is the payload for comment convention rules.

type DatabaseSchemaMetadata

type DatabaseSchemaMetadata = storepb.DatabaseSchemaMetadata

DatabaseSchemaMetadata is the alias for storepb.DatabaseSchemaMetadata.

type Engine

type Engine = storepb.Engine

Engine represents the database engine type.

func EngineFromString

func EngineFromString(s string) Engine

EngineFromString converts a string to Engine type.

type NamingRulePayload

type NamingRulePayload = advisor.NamingRulePayload

NamingRulePayload is the payload for naming rules.

type NumberTypeRulePayload

type NumberTypeRulePayload = advisor.NumberTypeRulePayload

NumberTypeRulePayload is the payload for number type rules.

type Position

type Position = storepb.Position

Position is the alias for storepb.Position.

type ReviewRequest

type ReviewRequest struct {
	// Engine is the database engine type.
	Engine Engine
	// Statement is the SQL statement to review.
	Statement string
	// Rules is the list of review rules to apply.
	Rules []*SQLReviewRule
	// CurrentDatabase is the current database context (optional).
	CurrentDatabase string
	// DBSchema is the database schema metadata (optional, needed for some rules).
	// If not provided, rules that require metadata will be skipped or may fail.
	DBSchema *DatabaseSchemaMetadata
}

ReviewRequest represents a request to review SQL statements.

type ReviewResponse

type ReviewResponse struct {
	// Advices is the list of advice generated from the review.
	Advices []*Advice
	// HasError indicates if there are any error-level advices.
	HasError bool
	// HasWarning indicates if there are any warning-level advices.
	HasWarning bool
}

ReviewResponse represents the response from SQL review.

func SQLReviewCheck

func SQLReviewCheck(ctx context.Context, req *ReviewRequest) (*ReviewResponse, error)

SQLReviewCheck performs SQL review on the given statement with the specified rules. This is the main entry point for SQL review.

type RuleLevel

type RuleLevel = storepb.SQLReviewRuleLevel

RuleLevel is the alias for SQLReviewRuleLevel.

func RuleLevelFromString

func RuleLevelFromString(s string) RuleLevel

RuleLevelFromString converts a string to RuleLevel.

type SQLReviewRule

type SQLReviewRule = storepb.SQLReviewRule

SQLReviewRule is the alias for storepb.SQLReviewRule.

func NewRule

func NewRule(ruleType string, level RuleLevel) *SQLReviewRule

NewRule creates a new SQL review rule.

func NewRuleForEngine

func NewRuleForEngine(ruleType string, level RuleLevel, engine Engine) *SQLReviewRule

NewRuleForEngine creates a new SQL review rule for a specific engine.

func NewRuleWithPayload

func NewRuleWithPayload(ruleType string, level RuleLevel, payload interface{}) (*SQLReviewRule, error)

NewRuleWithPayload creates a new SQL review rule with payload.

type StringArrayTypeRulePayload

type StringArrayTypeRulePayload = advisor.StringArrayTypeRulePayload

StringArrayTypeRulePayload is the payload for string array rules.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL