Skip to content

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

refactor: remove the api package dependency on plugin/advisor and plugin/catalog (#1531)

rebelice134950937760
diff --git a/api/api_dependency_gate.go b/api/api_dependency_gate.go
index 85446d94ef..85b380a352 100644
--- a/api/api_dependency_gate.go
+++ b/api/api_dependency_gate.go
@@ -8,9 +8,10 @@ package api
import (
// TODO(rebelice): fix the incorrect dependency and uncomment these
- // _ "github.com/bytebase/bytebase/plugin/advisor"
- // _ "github.com/bytebase/bytebase/plugin/catalog"
// _ "github.com/bytebase/bytebase/plugin/restore/mysql"
+
+ _ "github.com/bytebase/bytebase/plugin/advisor"
+ _ "github.com/bytebase/bytebase/plugin/catalog"
_ "github.com/bytebase/bytebase/plugin/db"
_ "github.com/bytebase/bytebase/plugin/metric"
_ "github.com/bytebase/bytebase/plugin/vcs"
diff --git a/api/policy.go b/api/policy.go
index 8a38c783db..353f4fc365 100644
--- a/api/policy.go
+++ b/api/policy.go
@@ -3,6 +3,8 @@ package api
import (
"encoding/json"
"fmt"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
)
// PolicyType is the type or name of a policy.
@@ -157,8 +159,8 @@ func UnmarshalBackupPlanPolicy(payload string) (*BackupPlanPolicy, error) {
}
// UnmarshalSchemaReviewPolicy will unmarshal payload to schema review policy.
-func UnmarshalSchemaReviewPolicy(payload string) (*SchemaReviewPolicy, error) {
- var sr SchemaReviewPolicy
+func UnmarshalSchemaReviewPolicy(payload string) (*advisor.SchemaReviewPolicy, error) {
+ var sr advisor.SchemaReviewPolicy
if err := json.Unmarshal([]byte(payload), &sr); err != nil {
return nil, fmt.Errorf("failed to unmarshal schema review policy %q: %q", payload, err)
}
diff --git a/api/project.go b/api/project.go
index 6dff06f980..68ecda9588 100644
--- a/api/project.go
+++ b/api/project.go
@@ -171,7 +171,7 @@ var (
// ValidateRepositoryFilePathTemplate validates the repository file path template.
func ValidateRepositoryFilePathTemplate(filePathTemplate string, tenantMode ProjectTenantMode) error {
- tokens, _ := parseTemplateTokens(filePathTemplate)
+ tokens, _ := common.ParseTemplateTokens(filePathTemplate)
tokenMap := make(map[string]bool)
for _, token := range tokens {
tokenMap[token] = true
@@ -202,7 +202,7 @@ func ValidateRepositorySchemaPathTemplate(schemaPathTemplate string, tenantMode
if schemaPathTemplate == "" {
return nil
}
- tokens, _ := parseTemplateTokens(schemaPathTemplate)
+ tokens, _ := common.ParseTemplateTokens(schemaPathTemplate)
tokenMap := make(map[string]bool)
for _, token := range tokens {
tokenMap[token] = true
@@ -233,7 +233,7 @@ func ValidateProjectDBNameTemplate(template string) error {
if template == "" {
return nil
}
- tokens, _ := parseTemplateTokens(template)
+ tokens, _ := common.ParseTemplateTokens(template)
// Must contain {{DB_NAME}}
hasDBName := false
for _, token := range tokens {
@@ -253,7 +253,7 @@ func ValidateProjectDBNameTemplate(template string) error {
// FormatTemplate formats the template by using the tokens as a replacement mapping.
// Note that the returned (modified) template should not be used as a regexp.
func FormatTemplate(template string, tokens map[string]string) (string, error) {
- keys, _ := parseTemplateTokens(template)
+ keys, _ := common.ParseTemplateTokens(template)
for _, key := range keys {
if _, ok := tokens[key]; !ok {
return "", fmt.Errorf("token %q not found", key)
@@ -266,7 +266,7 @@ func FormatTemplate(template string, tokens map[string]string) (string, error) {
// Similar to FormatTemplate, except that it will also escape special regexp characters in the delimiters
// of the template string, which will produce the correct regexp string.
func formatTemplateRegexp(template string, tokens map[string]string) (string, error) {
- keys, delimiters := parseTemplateTokens(template)
+ keys, delimiters := common.ParseTemplateTokens(template)
for _, key := range keys {
if _, ok := tokens[key]; !ok {
return "", fmt.Errorf("token %q not found", key)
@@ -319,24 +319,3 @@ func GetBaseDatabaseName(databaseName, dbNameTemplate, labelsJSON string) (strin
}
return names[1], nil
}
-
-// Parses the template and returns template tokens and their delimiters.
-// For example, if the template is "{{DB_NAME}}_hello_{{LOCATION}}", then the tokens will be ["{{DB_NAME}}", "{{LOCATION}}"],
-// and the delimiters will be ["_hello_"].
-// The caller will usually replace the tokens with a normal string, or a regexp. In the latter case, it will be a problem
-// if there are special regexp characters like "$" in the delimiters. The caller should escape the delimiters in such cases.
-func parseTemplateTokens(template string) ([]string, []string) {
- r := regexp.MustCompile(`{{[^{}]+}}`)
- tokens := r.FindAllString(template, -1)
- if len(tokens) > 0 {
- split := r.Split(template, -1)
- var delimiters []string
- for _, s := range split {
- if s != "" {
- delimiters = append(delimiters, s)
- }
- }
- return tokens, delimiters
- }
- return nil, nil
-}
diff --git a/api/project_test.go b/api/project_test.go
index 28bbfed891..93ce1917ea 100644
--- a/api/project_test.go
+++ b/api/project_test.go
@@ -3,6 +3,7 @@ package api
import (
"testing"
+ "github.com/bytebase/bytebase/common"
"github.com/stretchr/testify/require"
)
@@ -32,7 +33,7 @@ func TestGetTemplateTokens(t *testing.T) {
}
for _, test := range tests {
- tokens, delimiters := parseTemplateTokens(test.template)
+ tokens, delimiters := common.ParseTemplateTokens(test.template)
require.Equal(t, test.tokens, tokens)
require.Equal(t, test.delimiters, delimiters)
}
diff --git a/common/util.go b/common/util.go
index d69138b8f0..58189ba77a 100644
--- a/common/util.go
+++ b/common/util.go
@@ -4,6 +4,7 @@ import (
"crypto/rand"
"math/big"
"path"
+ "regexp"
"sort"
"strings"
"time"
@@ -70,3 +71,24 @@ func GetResourceDir(dataDir string) string {
func DefaultMigrationVersion() string {
return time.Now().Format("20060102150405")
}
+
+// ParseTemplateTokens parses the template and returns template tokens and their delimiters.
+// For example, if the template is "{{DB_NAME}}_hello_{{LOCATION}}", then the tokens will be ["{{DB_NAME}}", "{{LOCATION}}"],
+// and the delimiters will be ["_hello_"].
+// The caller will usually replace the tokens with a normal string, or a regexp. In the latter case, it will be a problem
+// if there are special regexp characters like "$" in the delimiters. The caller should escape the delimiters in such cases.
+func ParseTemplateTokens(template string) ([]string, []string) {
+ r := regexp.MustCompile(`{{[^{}]+}}`)
+ tokens := r.FindAllString(template, -1)
+ if len(tokens) > 0 {
+ split := r.Split(template, -1)
+ var delimiters []string
+ for _, s := range split {
+ if s != "" {
+ delimiters = append(delimiters, s)
+ }
+ }
+ return tokens, delimiters
+ }
+ return nil, nil
+}
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index f9a0fa905f..75560acc54 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -6,7 +6,6 @@ import (
"fmt"
"sync"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/catalog"
"github.com/bytebase/bytebase/plugin/db"
@@ -40,11 +39,11 @@ func (e Status) String() string {
}
// NewStatusBySchemaReviewRuleLevel returns status by SchemaReviewRuleLevel.
-func NewStatusBySchemaReviewRuleLevel(level api.SchemaReviewRuleLevel) (Status, error) {
+func NewStatusBySchemaReviewRuleLevel(level SchemaReviewRuleLevel) (Status, error) {
switch level {
- case api.SchemaRuleLevelError:
+ case SchemaRuleLevelError:
return Error, nil
- case api.SchemaRuleLevelWarning:
+ case SchemaRuleLevelWarning:
return Warn, nil
}
return "", fmt.Errorf("unexpected rule level type: %s", level)
@@ -114,8 +113,8 @@ type Context struct {
Collation string
// Schema review rule special fields.
- Rule *api.SchemaReviewRule
- Catalog catalog.Service
+ Rule *SchemaReviewRule
+ Catalog catalog.Catalog
}
// Advisor is the interface for advisor.
diff --git a/plugin/advisor/mysql/advisor_column_no_null_test.go b/plugin/advisor/mysql/advisor_column_no_null_test.go
index dfbb83c02c..5413c5139f 100644
--- a/plugin/advisor/mysql/advisor_column_no_null_test.go
+++ b/plugin/advisor/mysql/advisor_column_no_null_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -106,9 +105,9 @@ func TestColumnNoNull(t *testing.T) {
},
}
- runSchemaReviewRuleTests(t, tests, &ColumnNoNullAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleColumnNotNull,
- Level: api.SchemaRuleLevelWarning,
+ runSchemaReviewRuleTests(t, tests, &ColumnNoNullAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleColumnNotNull,
+ Level: advisor.SchemaRuleLevelWarning,
Payload: "",
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_column_required.go b/plugin/advisor/mysql/advisor_column_required.go
index f5dc63220a..d4b2fa3d54 100644
--- a/plugin/advisor/mysql/advisor_column_required.go
+++ b/plugin/advisor/mysql/advisor_column_required.go
@@ -5,7 +5,6 @@ import (
"sort"
"strings"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
@@ -36,7 +35,7 @@ func (adv *ColumnRequirementAdvisor) Check(ctx advisor.Context, statement string
if err != nil {
return nil, err
}
- payload, err := api.UnmarshalRequiredColumnRulePayload(ctx.Rule.Payload)
+ payload, err := advisor.UnmarshalRequiredColumnRulePayload(ctx.Rule.Payload)
if err != nil {
return nil, err
}
diff --git a/plugin/advisor/mysql/advisor_column_required_test.go b/plugin/advisor/mysql/advisor_column_required_test.go
index 5cfc1ba710..d0481ae863 100644
--- a/plugin/advisor/mysql/advisor_column_required_test.go
+++ b/plugin/advisor/mysql/advisor_column_required_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
@@ -218,7 +217,7 @@ func TestColumnRequirement(t *testing.T) {
},
},
}
- payload, err := json.Marshal(api.RequiredColumnRulePayload{
+ payload, err := json.Marshal(advisor.RequiredColumnRulePayload{
ColumnList: []string{
"id",
"created_ts",
@@ -228,9 +227,9 @@ func TestColumnRequirement(t *testing.T) {
},
})
require.NoError(t, err)
- runSchemaReviewRuleTests(t, tests, &ColumnRequirementAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleRequiredColumn,
- Level: api.SchemaRuleLevelWarning,
+ runSchemaReviewRuleTests(t, tests, &ColumnRequirementAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleRequiredColumn,
+ Level: advisor.SchemaRuleLevelWarning,
Payload: string(payload),
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_migration_compatibility_test.go b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
index 40fd37dcde..c1d840b44b 100644
--- a/plugin/advisor/mysql/advisor_migration_compatibility_test.go
+++ b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
@@ -5,7 +5,6 @@ import (
_ "github.com/pingcap/tidb/types/parser_driver"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -86,9 +85,9 @@ func TestBasic(t *testing.T) {
},
}
- runSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleSchemaBackwardCompatibility,
- Level: api.SchemaRuleLevelWarning,
+ runSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleSchemaBackwardCompatibility,
+ Level: advisor.SchemaRuleLevelWarning,
Payload: "",
}, &MockCatalogService{})
}
@@ -260,9 +259,9 @@ func TestAlterTable(t *testing.T) {
},
}
- runSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleSchemaBackwardCompatibility,
- Level: api.SchemaRuleLevelWarning,
+ runSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleSchemaBackwardCompatibility,
+ Level: advisor.SchemaRuleLevelWarning,
Payload: "",
}, &MockCatalogService{})
}
@@ -326,9 +325,9 @@ func TestAlterTableChangeColumnType(t *testing.T) {
},
}
- runSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleSchemaBackwardCompatibility,
- Level: api.SchemaRuleLevelWarning,
+ runSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleSchemaBackwardCompatibility,
+ Level: advisor.SchemaRuleLevelWarning,
Payload: "",
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_naming_column.go b/plugin/advisor/mysql/advisor_naming_column.go
index 2125ea293a..3ee018140a 100644
--- a/plugin/advisor/mysql/advisor_naming_column.go
+++ b/plugin/advisor/mysql/advisor_naming_column.go
@@ -4,7 +4,6 @@ import (
"fmt"
"regexp"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
@@ -35,7 +34,7 @@ func (adv *NamingColumnConventionAdvisor) Check(ctx advisor.Context, statement s
if err != nil {
return nil, err
}
- format, err := api.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
+ format, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
if err != nil {
return nil, err
}
diff --git a/plugin/advisor/mysql/advisor_naming_column_test.go b/plugin/advisor/mysql/advisor_naming_column_test.go
index 2aac6bf8d9..89ac04d21a 100644
--- a/plugin/advisor/mysql/advisor_naming_column_test.go
+++ b/plugin/advisor/mysql/advisor_naming_column_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
@@ -151,13 +150,13 @@ func TestNamingColumnConvention(t *testing.T) {
},
}
- payload, err := json.Marshal(api.NamingRulePayload{
+ payload, err := json.Marshal(advisor.NamingRulePayload{
Format: "^[a-z]+(_[a-z]+)*$",
})
require.NoError(t, err)
- runSchemaReviewRuleTests(t, tests, &NamingColumnConventionAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleColumnNaming,
- Level: api.SchemaRuleLevelWarning,
+ runSchemaReviewRuleTests(t, tests, &NamingColumnConventionAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleColumnNaming,
+ Level: advisor.SchemaRuleLevelWarning,
Payload: string(payload),
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
index 9a53be10bb..5e403e12c0 100644
--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
@@ -4,7 +4,6 @@ import (
"fmt"
"strings"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
@@ -36,7 +35,7 @@ func (check *NamingFKConventionAdvisor) Check(ctx advisor.Context, statement str
return nil, err
}
- format, templateList, err := api.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
+ format, templateList, err := advisor.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
if err != nil {
return nil, err
}
@@ -121,10 +120,10 @@ func (checker *namingFKConventionChecker) getMetaDataList(in ast.Node) []*indexM
}
metaData := map[string]string{
- api.ReferencingTableNameTemplateToken: node.Table.Name.String(),
- api.ReferencingColumnNameTemplateToken: strings.Join(referencingColumnList, "_"),
- api.ReferencedTableNameTemplateToken: constraint.Refer.Table.Name.String(),
- api.ReferencedColumnNameTemplateToken: strings.Join(referencedColumnList, "_"),
+ advisor.ReferencingTableNameTemplateToken: node.Table.Name.String(),
+ advisor.ReferencingColumnNameTemplateToken: strings.Join(referencingColumnList, "_"),
+ advisor.ReferencedTableNameTemplateToken: constraint.Refer.Table.Name.String(),
+ advisor.ReferencedColumnNameTemplateToken: strings.Join(referencedColumnList, "_"),
}
res = append(res, &indexMetaData{
@@ -147,10 +146,10 @@ func (checker *namingFKConventionChecker) getMetaDataList(in ast.Node) []*indexM
}
metaData := map[string]string{
- api.ReferencingTableNameTemplateToken: node.Table.Name.String(),
- api.ReferencingColumnNameTemplateToken: strings.Join(referencingColumnList, "_"),
- api.ReferencedTableNameTemplateToken: spec.Constraint.Refer.Table.Name.String(),
- api.ReferencedColumnNameTemplateToken: strings.Join(referencedColumnList, "_"),
+ advisor.ReferencingTableNameTemplateToken: node.Table.Name.String(),
+ advisor.ReferencingColumnNameTemplateToken: strings.Join(referencingColumnList, "_"),
+ advisor.ReferencedTableNameTemplateToken: spec.Constraint.Refer.Table.Name.String(),
+ advisor.ReferencedColumnNameTemplateToken: strings.Join(referencedColumnList, "_"),
}
res = append(res, &indexMetaData{
indexName: spec.Constraint.Name,
diff --git a/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go b/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
index 218509e320..2fe350721c 100644
--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
@@ -58,13 +57,13 @@ func TestNamingFKConvention(t *testing.T) {
},
}
- payload, err := json.Marshal(api.NamingRulePayload{
+ payload, err := json.Marshal(advisor.NamingRulePayload{
Format: "^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$",
})
require.NoError(t, err)
- runSchemaReviewRuleTests(t, tests, &NamingFKConventionAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleFKNaming,
- Level: api.SchemaRuleLevelError,
+ runSchemaReviewRuleTests(t, tests, &NamingFKConventionAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleFKNaming,
+ Level: advisor.SchemaRuleLevelError,
Payload: string(payload),
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention.go b/plugin/advisor/mysql/advisor_naming_index_convention.go
index 0f8a393db5..a1be8c0999 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention.go
@@ -6,7 +6,6 @@ import (
"regexp"
"strings"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/advisor"
@@ -41,7 +40,7 @@ func (check *NamingIndexConventionAdvisor) Check(ctx advisor.Context, statement
return nil, err
}
- format, templateList, err := api.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
+ format, templateList, err := advisor.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
if err != nil {
return nil, err
}
@@ -74,7 +73,7 @@ type namingIndexConventionChecker struct {
title string
format string
templateList []string
- catalog catalog.Service
+ catalog catalog.Catalog
}
// Enter implements the ast.Visitor interface
@@ -129,8 +128,8 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
columnList = append(columnList, key.Column.Name.String())
}
metaData := map[string]string{
- api.ColumnListTemplateToken: strings.Join(columnList, "_"),
- api.TableNameTemplateToken: node.Table.Name.String(),
+ advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
+ advisor.TableNameTemplateToken: node.Table.Name.String(),
}
res = append(res, &indexMetaData{
indexName: constraint.Name,
@@ -162,8 +161,8 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
continue
}
metaData := map[string]string{
- api.ColumnListTemplateToken: strings.Join(index.ColumnExpressions, "_"),
- api.TableNameTemplateToken: node.Table.Name.String(),
+ advisor.ColumnListTemplateToken: strings.Join(index.ColumnExpressions, "_"),
+ advisor.TableNameTemplateToken: node.Table.Name.String(),
}
res = append(res, &indexMetaData{
indexName: spec.ToKey.String(),
@@ -178,8 +177,8 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
}
metaData := map[string]string{
- api.ColumnListTemplateToken: strings.Join(columnList, "_"),
- api.TableNameTemplateToken: node.Table.Name.String(),
+ advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
+ advisor.TableNameTemplateToken: node.Table.Name.String(),
}
res = append(res, &indexMetaData{
indexName: spec.Constraint.Name,
@@ -196,8 +195,8 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
columnList = append(columnList, spec.Column.Name.String())
}
metaData := map[string]string{
- api.ColumnListTemplateToken: strings.Join(columnList, "_"),
- api.TableNameTemplateToken: node.Table.Name.String(),
+ advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
+ advisor.TableNameTemplateToken: node.Table.Name.String(),
}
res = append(res, &indexMetaData{
indexName: node.IndexName,
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention_test.go b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
index bdc171b1dd..dd0b142ba6 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
@@ -6,7 +6,6 @@ import (
"strings"
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
@@ -111,13 +110,13 @@ func TestNamingIndexConvention(t *testing.T) {
},
}
- payload, err := json.Marshal(api.NamingRulePayload{
+ payload, err := json.Marshal(advisor.NamingRulePayload{
Format: "^idx_{{table}}_{{column_list}}$",
})
require.NoError(t, err)
- runSchemaReviewRuleTests(t, tests, &NamingIndexConventionAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleIDXNaming,
- Level: api.SchemaRuleLevelError,
+ runSchemaReviewRuleTests(t, tests, &NamingIndexConventionAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleIDXNaming,
+ Level: advisor.SchemaRuleLevelError,
Payload: string(payload),
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_naming_table.go b/plugin/advisor/mysql/advisor_naming_table.go
index 19755e305e..d6c9f02190 100644
--- a/plugin/advisor/mysql/advisor_naming_table.go
+++ b/plugin/advisor/mysql/advisor_naming_table.go
@@ -4,7 +4,6 @@ import (
"fmt"
"regexp"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
@@ -35,7 +34,7 @@ func (adv *NamingTableConventionAdvisor) Check(ctx advisor.Context, statement st
if err != nil {
return nil, err
}
- format, err := api.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
+ format, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
if err != nil {
return nil, err
}
diff --git a/plugin/advisor/mysql/advisor_naming_table_test.go b/plugin/advisor/mysql/advisor_naming_table_test.go
index cc6d38a9f8..175c557ebc 100644
--- a/plugin/advisor/mysql/advisor_naming_table_test.go
+++ b/plugin/advisor/mysql/advisor_naming_table_test.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
@@ -96,13 +95,13 @@ func TestNamingTableConvention(t *testing.T) {
},
},
}
- payload, err := json.Marshal(api.NamingRulePayload{
+ payload, err := json.Marshal(advisor.NamingRulePayload{
Format: "^[a-z]+(_[a-z]+)*$",
})
require.NoError(t, err)
- runSchemaReviewRuleTests(t, tests, &NamingTableConventionAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleTableNaming,
- Level: api.SchemaRuleLevelError,
+ runSchemaReviewRuleTests(t, tests, &NamingTableConventionAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleTableNaming,
+ Level: advisor.SchemaRuleLevelError,
Payload: string(payload),
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
index e853561246..1825518394 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
@@ -5,7 +5,6 @@ import (
"fmt"
"strings"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/advisor"
@@ -40,7 +39,7 @@ func (check *NamingUKConventionAdvisor) Check(ctx advisor.Context, statement str
return nil, err
}
- format, templateList, err := api.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
+ format, templateList, err := advisor.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
if err != nil {
return nil, err
}
@@ -72,7 +71,7 @@ type namingUKConventionChecker struct {
title string
format string
templateList []string
- catalog catalog.Service
+ catalog catalog.Catalog
}
// Enter implements the ast.Visitor interface
@@ -122,8 +121,8 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
columnList = append(columnList, key.Column.Name.String())
}
metaData := map[string]string{
- api.ColumnListTemplateToken: strings.Join(columnList, "_"),
- api.TableNameTemplateToken: node.Table.Name.String(),
+ advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
+ advisor.TableNameTemplateToken: node.Table.Name.String(),
}
res = append(res, &indexMetaData{
indexName: constraint.Name,
@@ -155,8 +154,8 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
continue
}
metaData := map[string]string{
- api.ColumnListTemplateToken: strings.Join(index.ColumnExpressions, "_"),
- api.TableNameTemplateToken: node.Table.Name.String(),
+ advisor.ColumnListTemplateToken: strings.Join(index.ColumnExpressions, "_"),
+ advisor.TableNameTemplateToken: node.Table.Name.String(),
}
res = append(res, &indexMetaData{
indexName: spec.ToKey.String(),
@@ -172,8 +171,8 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
}
metaData := map[string]string{
- api.ColumnListTemplateToken: strings.Join(columnList, "_"),
- api.TableNameTemplateToken: node.Table.Name.String(),
+ advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
+ advisor.TableNameTemplateToken: node.Table.Name.String(),
}
res = append(res, &indexMetaData{
indexName: spec.Constraint.Name,
@@ -190,8 +189,8 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
columnList = append(columnList, spec.Column.Name.String())
}
metaData := map[string]string{
- api.ColumnListTemplateToken: strings.Join(columnList, "_"),
- api.TableNameTemplateToken: node.Table.Name.String(),
+ advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
+ advisor.TableNameTemplateToken: node.Table.Name.String(),
}
res = append(res, &indexMetaData{
indexName: node.IndexName,
diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
index cc48d6eb28..b35abcbf11 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
@@ -6,7 +6,6 @@ import (
"strings"
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/stretchr/testify/require"
@@ -133,13 +132,13 @@ func TestNamingUKConvention(t *testing.T) {
},
}
- payload, err := json.Marshal(api.NamingRulePayload{
+ payload, err := json.Marshal(advisor.NamingRulePayload{
Format: "^uk_{{table}}_{{column_list}}$",
})
require.NoError(t, err)
- runSchemaReviewRuleTests(t, tests, &NamingUKConventionAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleUKNaming,
- Level: api.SchemaRuleLevelError,
+ runSchemaReviewRuleTests(t, tests, &NamingUKConventionAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleUKNaming,
+ Level: advisor.SchemaRuleLevelError,
Payload: string(payload),
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go
index fb3e9dea57..c81da6f127 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -67,9 +66,9 @@ func TestNoLeadingWildcardLike(t *testing.T) {
},
}
- runSchemaReviewRuleTests(t, tests, &NoLeadingWildcardLikeAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleStatementNoLeadingWildcardLike,
- Level: api.SchemaRuleLevelError,
+ runSchemaReviewRuleTests(t, tests, &NoLeadingWildcardLikeAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleStatementNoLeadingWildcardLike,
+ Level: advisor.SchemaRuleLevelError,
Payload: "",
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go b/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
index 00b87463b7..d490827d24 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -45,9 +44,9 @@ func TestNoSelectAll(t *testing.T) {
},
}
- runSchemaReviewRuleTests(t, tests, &NoSelectAllAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleStatementNoSelectAll,
- Level: api.SchemaRuleLevelError,
+ runSchemaReviewRuleTests(t, tests, &NoSelectAllAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleStatementNoSelectAll,
+ Level: advisor.SchemaRuleLevelError,
Payload: "",
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required_test.go b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
index 8751b48c71..19517663c4 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -89,9 +88,9 @@ func TestWhereRequirement(t *testing.T) {
},
}
- runSchemaReviewRuleTests(t, tests, &WhereRequirementAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleStatementRequireWhere,
- Level: api.SchemaRuleLevelWarning,
+ runSchemaReviewRuleTests(t, tests, &WhereRequirementAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleStatementRequireWhere,
+ Level: advisor.SchemaRuleLevelWarning,
Payload: "",
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_table_require_pk.go b/plugin/advisor/mysql/advisor_table_require_pk.go
index 25fce0473f..cd6e8b6381 100644
--- a/plugin/advisor/mysql/advisor_table_require_pk.go
+++ b/plugin/advisor/mysql/advisor_table_require_pk.go
@@ -61,7 +61,7 @@ type tableRequirePKChecker struct {
level advisor.Status
title string
tables tablePK
- catalog catalog.Service
+ catalog catalog.Catalog
}
// Enter implements the ast.Visitor interface
diff --git a/plugin/advisor/mysql/advisor_table_require_pk_test.go b/plugin/advisor/mysql/advisor_table_require_pk_test.go
index 0a99b11729..d08e01a6fa 100644
--- a/plugin/advisor/mysql/advisor_table_require_pk_test.go
+++ b/plugin/advisor/mysql/advisor_table_require_pk_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -214,9 +213,9 @@ func TestRequirePK(t *testing.T) {
},
}
- runSchemaReviewRuleTests(t, tests, &TableRequirePKAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleTableRequirePK,
- Level: api.SchemaRuleLevelError,
+ runSchemaReviewRuleTests(t, tests, &TableRequirePKAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleTableRequirePK,
+ Level: advisor.SchemaRuleLevelError,
Payload: "",
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/advisor_use_innodb_test.go b/plugin/advisor/mysql/advisor_use_innodb_test.go
index b7dbca8559..99a08313e4 100644
--- a/plugin/advisor/mysql/advisor_use_innodb_test.go
+++ b/plugin/advisor/mysql/advisor_use_innodb_test.go
@@ -3,7 +3,6 @@ package mysql
import (
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
)
@@ -88,9 +87,9 @@ func TestUseInnoDB(t *testing.T) {
},
},
}
- runSchemaReviewRuleTests(t, tests, &UseInnoDBAdvisor{}, &api.SchemaReviewRule{
- Type: api.SchemaRuleMySQLEngine,
- Level: api.SchemaRuleLevelError,
+ runSchemaReviewRuleTests(t, tests, &UseInnoDBAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleMySQLEngine,
+ Level: advisor.SchemaRuleLevelError,
Payload: "",
}, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/utils_for_test.go b/plugin/advisor/mysql/utils_for_test.go
index b5c4ed519e..8683b30353 100644
--- a/plugin/advisor/mysql/utils_for_test.go
+++ b/plugin/advisor/mysql/utils_for_test.go
@@ -5,7 +5,6 @@ import (
"fmt"
"testing"
- "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/catalog"
"github.com/stretchr/testify/assert"
@@ -56,8 +55,8 @@ func runSchemaReviewRuleTests(
t *testing.T,
tests []test,
adv advisor.Advisor,
- rule *api.SchemaReviewRule,
- catalog catalog.Service,
+ rule *advisor.SchemaReviewRule,
+ catalog catalog.Catalog,
) {
ctx := advisor.Context{
Charset: "",
diff --git a/api/schema_system.go b/plugin/advisor/schema_review.go
similarity index 98%
rename from api/schema_system.go
rename to plugin/advisor/schema_review.go
index e40f255b16..4b901efc08 100644
--- a/api/schema_system.go
+++ b/plugin/advisor/schema_review.go
@@ -1,9 +1,11 @@
-package api
+package advisor
import (
"encoding/json"
"fmt"
"regexp"
+
+ "github.com/bytebase/bytebase/common"
)
// SchemaReviewRuleLevel is the error level for schema review rule.
@@ -167,7 +169,7 @@ func UnmarshalNamingRulePayloadAsTemplate(ruleType SchemaReviewRuleType, payload
}
template := nr.Format
- keys, _ := parseTemplateTokens(template)
+ keys, _ := common.ParseTemplateTokens(template)
for _, key := range keys {
if _, ok := TemplateNamingTokens[ruleType][key]; !ok {
diff --git a/plugin/catalog/catalog.go b/plugin/catalog/catalog.go
index 89d2536bd0..1cd2c73478 100644
--- a/plugin/catalog/catalog.go
+++ b/plugin/catalog/catalog.go
@@ -2,21 +2,10 @@ package catalog
import (
"context"
- "sort"
-
- "github.com/bytebase/bytebase/api"
- "github.com/bytebase/bytebase/store"
)
-// Catalog is the database catalog.
-type Catalog struct {
- // catalog special fields.
- databaseID *int
- store *store.Store
-}
-
-// Service is the service for catalog.
-type Service interface {
+// Catalog is the service for catalog.
+type Catalog interface {
FindIndex(ctx context.Context, find *IndexFind) (*Index, error)
}
@@ -34,51 +23,3 @@ type IndexFind struct {
TableName string
IndexName string
}
-
-// NewService creates a new instance of Catalog
-func NewService(databaseID *int, store *store.Store) *Catalog {
- return &Catalog{
- databaseID: databaseID,
- store: store,
- }
-}
-
-// FindIndex finds the index by IndexFind
-func (c *Catalog) FindIndex(ctx context.Context, find *IndexFind) (*Index, error) {
- table, err := c.store.GetTable(ctx, &api.TableFind{
- DatabaseID: c.databaseID,
- Name: &find.TableName,
- })
- if err != nil {
- return nil, err
- }
-
- indexList, err := c.store.FindIndex(ctx, &api.IndexFind{
- DatabaseID: c.databaseID,
- TableID: &table.ID,
- Name: &find.IndexName,
- })
- if err != nil {
- return nil, err
- }
- if len(indexList) == 0 {
- return nil, nil
- }
-
- sort.Slice(indexList, func(i, j int) bool {
- return indexList[i].Position < indexList[j].Position
- })
-
- var columnExpressions []string
- for _, index := range indexList {
- columnExpressions = append(columnExpressions, index.Expression)
- }
-
- return &Index{
- Name: indexList[0].Name,
- TableName: table.Name,
- Type: indexList[0].Type,
- Unique: indexList[0].Unique,
- ColumnExpressions: columnExpressions,
- }, nil
-}
diff --git a/server/task_check_executor_statement_advisor_composite.go b/server/task_check_executor_statement_advisor_composite.go
index fa47ad46c2..7a28728cc9 100644
--- a/server/task_check_executor_statement_advisor_composite.go
+++ b/server/task_check_executor_statement_advisor_composite.go
@@ -9,8 +9,8 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/catalog"
"github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/store"
"go.uber.org/zap"
)
@@ -70,7 +70,7 @@ func (exec *TaskCheckStatementAdvisorCompositeExecutor) Run(ctx context.Context,
result = []api.TaskCheckResult{}
for _, rule := range policy.RuleList {
- if rule.Level == api.SchemaRuleLevelDisabled {
+ if rule.Level == advisor.SchemaRuleLevelDisabled {
continue
}
advisorType, err := getAdvisorTypeByRule(rule.Type, payload.DbType)
@@ -85,7 +85,7 @@ func (exec *TaskCheckStatementAdvisorCompositeExecutor) Run(ctx context.Context,
Charset: payload.Charset,
Collation: payload.Collation,
Rule: rule,
- Catalog: catalog.NewService(task.DatabaseID, server.store),
+ Catalog: store.NewCatalog(task.DatabaseID, server.store),
},
payload.Statement,
)
@@ -129,69 +129,69 @@ func (exec *TaskCheckStatementAdvisorCompositeExecutor) Run(ctx context.Context,
}
-func getAdvisorTypeByRule(ruleType api.SchemaReviewRuleType, engine db.Type) (advisor.Type, error) {
+func getAdvisorTypeByRule(ruleType advisor.SchemaReviewRuleType, engine db.Type) (advisor.Type, error) {
switch ruleType {
- case api.SchemaRuleStatementRequireWhere:
+ case advisor.SchemaRuleStatementRequireWhere:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLWhereRequirement, nil
}
- case api.SchemaRuleStatementNoLeadingWildcardLike:
+ case advisor.SchemaRuleStatementNoLeadingWildcardLike:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLNoLeadingWildcardLike, nil
}
- case api.SchemaRuleStatementNoSelectAll:
+ case advisor.SchemaRuleStatementNoSelectAll:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLNoSelectAll, nil
}
- case api.SchemaRuleSchemaBackwardCompatibility:
+ case advisor.SchemaRuleSchemaBackwardCompatibility:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLMigrationCompatibility, nil
}
- case api.SchemaRuleTableNaming:
+ case advisor.SchemaRuleTableNaming:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLNamingTableConvention, nil
}
- case api.SchemaRuleIDXNaming:
+ case advisor.SchemaRuleIDXNaming:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLNamingIndexConvention, nil
}
- case api.SchemaRuleUKNaming:
+ case advisor.SchemaRuleUKNaming:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLNamingUKConvention, nil
}
- case api.SchemaRuleFKNaming:
+ case advisor.SchemaRuleFKNaming:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLNamingFKConvention, nil
}
- case api.SchemaRuleColumnNaming:
+ case advisor.SchemaRuleColumnNaming:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLNamingColumnConvention, nil
}
- case api.SchemaRuleRequiredColumn:
+ case advisor.SchemaRuleRequiredColumn:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLColumnRequirement, nil
}
- case api.SchemaRuleColumnNotNull:
+ case advisor.SchemaRuleColumnNotNull:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLColumnNoNull, nil
}
- case api.SchemaRuleTableRequirePK:
+ case advisor.SchemaRuleTableRequirePK:
switch engine {
case db.MySQL, db.TiDB:
return advisor.MySQLTableRequirePK, nil
}
- case api.SchemaRuleMySQLEngine:
+ case advisor.SchemaRuleMySQLEngine:
if engine == db.MySQL {
return advisor.MySQLUseInnoDB, nil
}
diff --git a/store/catalog.go b/store/catalog.go
new file mode 100644
index 0000000000..083a9016c5
--- /dev/null
+++ b/store/catalog.go
@@ -0,0 +1,67 @@
+package store
+
+import (
+ "context"
+ "sort"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/plugin/catalog"
+)
+
+var (
+ _ catalog.Catalog = (*Catalog)(nil)
+)
+
+// Catalog is the database catalog.
+type Catalog struct {
+ databaseID *int
+ store *Store
+}
+
+// NewCatalog creates a new database catalog.
+func NewCatalog(databaseID *int, store *Store) *Catalog {
+ return &Catalog{
+ databaseID: databaseID,
+ store: store,
+ }
+}
+
+// FindIndex finds the index by IndexFind. Implement the catalog.Catalog interface.
+func (c *Catalog) FindIndex(ctx context.Context, find *catalog.IndexFind) (*catalog.Index, error) {
+ table, err := c.store.GetTable(ctx, &api.TableFind{
+ DatabaseID: c.databaseID,
+ Name: &find.TableName,
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ indexList, err := c.store.FindIndex(ctx, &api.IndexFind{
+ DatabaseID: c.databaseID,
+ TableID: &table.ID,
+ Name: &find.IndexName,
+ })
+ if err != nil {
+ return nil, err
+ }
+ if len(indexList) == 0 {
+ return nil, nil
+ }
+
+ sort.Slice(indexList, func(i, j int) bool {
+ return indexList[i].Position < indexList[j].Position
+ })
+
+ var columnExpressions []string
+ for _, index := range indexList {
+ columnExpressions = append(columnExpressions, index.Expression)
+ }
+
+ return &catalog.Index{
+ Name: indexList[0].Name,
+ TableName: table.Name,
+ Type: indexList[0].Type,
+ Unique: indexList[0].Unique,
+ ColumnExpressions: columnExpressions,
+ }, nil
+}
diff --git a/store/policy.go b/store/policy.go
index a8ffd9269d..09abcd01b7 100644
--- a/store/policy.go
+++ b/store/policy.go
@@ -8,6 +8,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/advisor"
)
// policyRaw is the store model for an Policy.
@@ -191,7 +192,7 @@ func (s *Store) GetSchemaReviewPolicyIDByEnvID(ctx context.Context, environmentI
// GetSchemaReviewPolicyNormalByID will get the schema review policy for an environment.
// If policy.ID is DefaultPolicyID or policy is archived, return the default empty policy.
-func (s *Store) GetSchemaReviewPolicyNormalByID(ctx context.Context, id int) (*api.SchemaReviewPolicy, error) {
+func (s *Store) GetSchemaReviewPolicyNormalByID(ctx context.Context, id int) (*advisor.SchemaReviewPolicy, error) {
pType := api.PolicyTypeSchemaReview
if id == api.DefaultPolicyID {
diff --git a/tests/tests.go b/tests/tests.go
index 0b11e49e72..f1320b9330 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -22,6 +22,7 @@ import (
"github.com/bytebase/bytebase/bin/server/cmd"
"github.com/bytebase/bytebase/common/log"
enterpriseAPI "github.com/bytebase/bytebase/enterprise/api"
+ "github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/server"
"github.com/bytebase/bytebase/tests/fake"
@@ -1406,37 +1407,37 @@ func (ctl *controller) getSchemaReviewResult(id int) ([]api.TaskCheckResult, err
}
// setDefaultSchemaReviewRulePayload sets the default payload for this rule.
-func setDefaultSchemaReviewRulePayload(ruleTp api.SchemaReviewRuleType) (string, error) {
+func setDefaultSchemaReviewRulePayload(ruleTp advisor.SchemaReviewRuleType) (string, error) {
var payload []byte
var err error
switch ruleTp {
- case api.SchemaRuleMySQLEngine:
- case api.SchemaRuleStatementNoSelectAll:
- case api.SchemaRuleStatementRequireWhere:
- case api.SchemaRuleStatementNoLeadingWildcardLike:
- case api.SchemaRuleTableRequirePK:
- case api.SchemaRuleColumnNotNull:
- case api.SchemaRuleSchemaBackwardCompatibility:
- case api.SchemaRuleTableNaming:
+ case advisor.SchemaRuleMySQLEngine:
+ case advisor.SchemaRuleStatementNoSelectAll:
+ case advisor.SchemaRuleStatementRequireWhere:
+ case advisor.SchemaRuleStatementNoLeadingWildcardLike:
+ case advisor.SchemaRuleTableRequirePK:
+ case advisor.SchemaRuleColumnNotNull:
+ case advisor.SchemaRuleSchemaBackwardCompatibility:
+ case advisor.SchemaRuleTableNaming:
fallthrough
- case api.SchemaRuleColumnNaming:
- payload, err = json.Marshal(api.NamingRulePayload{
+ case advisor.SchemaRuleColumnNaming:
+ payload, err = json.Marshal(advisor.NamingRulePayload{
Format: "^[a-z]+(_[a-z]+)*$",
})
- case api.SchemaRuleIDXNaming:
- payload, err = json.Marshal(api.NamingRulePayload{
+ case advisor.SchemaRuleIDXNaming:
+ payload, err = json.Marshal(advisor.NamingRulePayload{
Format: "^idx_{{table}}_{{column_list}}$",
})
- case api.SchemaRuleUKNaming:
- payload, err = json.Marshal(api.NamingRulePayload{
+ case advisor.SchemaRuleUKNaming:
+ payload, err = json.Marshal(advisor.NamingRulePayload{
Format: "^uk_{{table}}_{{column_list}}$",
})
- case api.SchemaRuleFKNaming:
- payload, err = json.Marshal(api.NamingRulePayload{
+ case advisor.SchemaRuleFKNaming:
+ payload, err = json.Marshal(advisor.NamingRulePayload{
Format: "^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$",
})
- case api.SchemaRuleRequiredColumn:
- payload, err = json.Marshal(api.RequiredColumnRulePayload{
+ case advisor.SchemaRuleRequiredColumn:
+ payload, err = json.Marshal(advisor.RequiredColumnRulePayload{
ColumnList: []string{
"id",
"created_ts",
@@ -1457,60 +1458,60 @@ func setDefaultSchemaReviewRulePayload(ruleTp api.SchemaReviewRuleType) (string,
// prodTemplateSchemaReviewPolicy returns the default schema review policy.
func prodTemplateSchemaReviewPolicy() (string, error) {
- policy := api.SchemaReviewPolicy{
+ policy := advisor.SchemaReviewPolicy{
Name: "Prod",
- RuleList: []*api.SchemaReviewRule{
+ RuleList: []*advisor.SchemaReviewRule{
{
- Type: api.SchemaRuleMySQLEngine,
- Level: api.SchemaRuleLevelError,
+ Type: advisor.SchemaRuleMySQLEngine,
+ Level: advisor.SchemaRuleLevelError,
},
{
- Type: api.SchemaRuleTableNaming,
- Level: api.SchemaRuleLevelWarning,
+ Type: advisor.SchemaRuleTableNaming,
+ Level: advisor.SchemaRuleLevelWarning,
},
{
- Type: api.SchemaRuleColumnNaming,
- Level: api.SchemaRuleLevelWarning,
+ Type: advisor.SchemaRuleColumnNaming,
+ Level: advisor.SchemaRuleLevelWarning,
},
{
- Type: api.SchemaRuleIDXNaming,
- Level: api.SchemaRuleLevelWarning,
+ Type: advisor.SchemaRuleIDXNaming,
+ Level: advisor.SchemaRuleLevelWarning,
},
{
- Type: api.SchemaRuleUKNaming,
- Level: api.SchemaRuleLevelWarning,
+ Type: advisor.SchemaRuleUKNaming,
+ Level: advisor.SchemaRuleLevelWarning,
},
{
- Type: api.SchemaRuleFKNaming,
- Level: api.SchemaRuleLevelWarning,
+ Type: advisor.SchemaRuleFKNaming,
+ Level: advisor.SchemaRuleLevelWarning,
},
{
- Type: api.SchemaRuleStatementNoSelectAll,
- Level: api.SchemaRuleLevelError,
+ Type: advisor.SchemaRuleStatementNoSelectAll,
+ Level: advisor.SchemaRuleLevelError,
},
{
- Type: api.SchemaRuleStatementRequireWhere,
- Level: api.SchemaRuleLevelError,
+ Type: advisor.SchemaRuleStatementRequireWhere,
+ Level: advisor.SchemaRuleLevelError,
},
{
- Type: api.SchemaRuleStatementNoLeadingWildcardLike,
- Level: api.SchemaRuleLevelError,
+ Type: advisor.SchemaRuleStatementNoLeadingWildcardLike,
+ Level: advisor.SchemaRuleLevelError,
},
{
- Type: api.SchemaRuleTableRequirePK,
- Level: api.SchemaRuleLevelError,
+ Type: advisor.SchemaRuleTableRequirePK,
+ Level: advisor.SchemaRuleLevelError,
},
{
- Type: api.SchemaRuleRequiredColumn,
- Level: api.SchemaRuleLevelWarning,
+ Type: advisor.SchemaRuleRequiredColumn,
+ Level: advisor.SchemaRuleLevelWarning,
},
{
- Type: api.SchemaRuleColumnNotNull,
- Level: api.SchemaRuleLevelWarning,
+ Type: advisor.SchemaRuleColumnNotNull,
+ Level: advisor.SchemaRuleLevelWarning,
},
{
- Type: api.SchemaRuleSchemaBackwardCompatibility,
- Level: api.SchemaRuleLevelWarning,
+ Type: advisor.SchemaRuleSchemaBackwardCompatibility,
+ Level: advisor.SchemaRuleLevelWarning,
},
},
}