Skip to content

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

fix: create the empty database with db type for openapi and sql-server (#2719)

rebelicee09eaa56955e
diff --git a/plugin/advisor/catalog/catalog.go b/plugin/advisor/catalog/catalog.go
index 9eda71087d..bde2644fd6 100644
--- a/plugin/advisor/catalog/catalog.go
+++ b/plugin/advisor/catalog/catalog.go
@@ -7,7 +7,6 @@ import (
// Catalog is the service for catalog.
type Catalog interface {
- GetDatabase() *Database
GetFinder() *Finder
}
diff --git a/plugin/advisor/catalog/finder.go b/plugin/advisor/catalog/finder.go
index 71ad25a505..b4374c44c6 100644
--- a/plugin/advisor/catalog/finder.go
+++ b/plugin/advisor/catalog/finder.go
@@ -1,5 +1,7 @@
package catalog
+import "github.com/bytebase/bytebase/plugin/advisor/db"
+
// FinderContext is the context for finder.
type FinderContext struct {
// CheckIntegrity defines the policy for integrity checking.
@@ -37,8 +39,8 @@ func NewFinder(database *Database, context *FinderContext) *Finder {
}
// NewEmptyFinder creates a finder with empty databse.
-func NewEmptyFinder(ctx *FinderContext) *Finder {
- return &Finder{Origin: newDatabaseState(&Database{}, ctx)}
+func NewEmptyFinder(ctx *FinderContext, dbType db.Type) *Finder {
+ return &Finder{Origin: newDatabaseState(&Database{DbType: dbType}, ctx)}
}
// WalkThrough does the walk through.
diff --git a/server/openapi.go b/server/openapi.go
index f4114b52b4..657805ed29 100644
--- a/server/openapi.go
+++ b/server/openapi.go
@@ -23,18 +23,19 @@ var (
)
// catalogService is the catalog service for sql check api.
-type catalogService struct{}
+type catalogService struct {
+ finder *catalog.Finder
+}
-// GetDatabase is the API message in catalog.
-// We will not connect to the user's database in the early version of sql check api.
-func (*catalogService) GetDatabase() *catalog.Database {
- return &catalog.Database{}
+func newCatalogService(dbType advisorDB.Type) *catalogService {
+ return &catalogService{
+ finder: catalog.NewEmptyFinder(&catalog.FinderContext{CheckIntegrity: false}, dbType),
+ }
}
// GetFinder is the API message in catalog.
-// We will not connect to the user's database in the early version of sql check api.
-func (*catalogService) GetFinder() *catalog.Finder {
- return catalog.NewEmptyFinder(&catalog.FinderContext{CheckIntegrity: false})
+func (c *catalogService) GetFinder() *catalog.Finder {
+ return c.finder
}
func (s *Server) registerOpenAPIRoutes(g *echo.Group) {
@@ -87,7 +88,7 @@ func (s *Server) sqlCheckController(c echo.Context) error {
ctx := c.Request().Context()
var databaseType string
- var catalog catalog.Catalog = &catalogService{}
+ var catalog catalog.Catalog
if request.DatabaseName != "" && request.Host != "" && request.Port != "" {
database, err := s.findDatabase(ctx, request.Host, request.Port, request.DatabaseName)
@@ -111,6 +112,9 @@ func (s *Server) sqlCheckController(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Database %s is not support", databaseType))
}
+ if catalog == nil {
+ catalog = newCatalogService(advisorDBType)
+ }
envList, err := s.store.FindEnvironment(ctx, &api.EnvironmentFind{
Name: &request.EnvironmentName,
diff --git a/sql-server/advise.go b/sql-server/advise.go
index 4db9eb2234..b6b7cccdf3 100644
--- a/sql-server/advise.go
+++ b/sql-server/advise.go
@@ -22,18 +22,19 @@ var (
)
// catalogService is the catalog service for sql check api.
-type catalogService struct{}
+type catalogService struct {
+ finder *catalog.Finder
+}
-// GetDatabase is the API message in catalog.
-// We will not connect to the user's database in the early version of sql check api.
-func (*catalogService) GetDatabase() *catalog.Database {
- return &catalog.Database{}
+func newCatalogService(dbType advisorDB.Type) *catalogService {
+ return &catalogService{
+ finder: catalog.NewEmptyFinder(&catalog.FinderContext{CheckIntegrity: false}, dbType),
+ }
}
// GetDatabase is the API message in catalog.
-// We will not connect to the user's database in the early version of sql check api.
-func (*catalogService) GetFinder() *catalog.Finder {
- return catalog.NewEmptyFinder(&catalog.FinderContext{CheckIntegrity: false})
+func (c *catalogService) GetFinder() *catalog.Finder {
+ return c.finder
}
type sqlCheckRequestBody struct {
@@ -107,7 +108,7 @@ func (s *Server) sqlCheckController(c echo.Context) error {
"utf8mb4_general_ci",
request.Statement,
ruleList,
- &catalogService{},
+ newCatalogService(advisorDBType),
)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to run sql check").SetInternal(err)
diff --git a/store/catalog.go b/store/catalog.go
index ff5a16ef0b..c3a6f2999e 100644
--- a/store/catalog.go
+++ b/store/catalog.go
@@ -55,11 +55,6 @@ func (s *Store) NewCatalog(ctx context.Context, databaseID int, engineType db.Ty
return c, nil
}
-// GetDatabase implements the catalog.Catalog interface.
-func (c *Catalog) GetDatabase() *catalog.Database {
- return c.Database
-}
-
// GetFinder implements the catalog.Catalog interface.
func (c *Catalog) GetFinder() *catalog.Finder {
return c.Finder