Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: use POST instead of GET in SQL Service (#2146)
ecmadaocfd325ee5544
diff --git a/docs/openapi/docs.go b/docs/openapi/docs.goindex acc22f9a34..5fd5fa353b 100644--- a/docs/openapi/docs.go+++ b/docs/openapi/docs.go@@ -40,18 +40,22 @@ const docTemplate = `{"summary": "Check the SQL statement.","parameters": [{- "type": "string","description": "The environment name. Case sensitive.",- "name": "environment",- "in": "query",- "required": true+ "name": "environmentName",+ "in": "body",+ "required": true,+ "schema": {+ "type": "string"+ }},{- "type": "string","description": "The SQL statement.","name": "statement",- "in": "query",- "required": true+ "in": "body",+ "required": true,+ "schema": {+ "type": "string"+ }},{"enum": [@@ -59,28 +63,36 @@ const docTemplate = `{"POSTGRES","TIDB"],- "type": "string","description": "The database type. Required if the port, host and database name is not specified.","name": "databaseType",- "in": "query"+ "in": "body",+ "schema": {+ "type": "string"+ }},{- "type": "string","description": "The instance host.","name": "host",- "in": "query"+ "in": "body",+ "schema": {+ "type": "string"+ }},{- "type": "string","description": "The instance port.","name": "port",- "in": "query"+ "in": "body",+ "schema": {+ "type": "string"+ }},{- "type": "string","description": "The database name in the instance.","name": "databaseName",- "in": "query"+ "in": "body",+ "schema": {+ "type": "string"+ }}],"responses": {diff --git a/docs/openapi/swagger.yaml b/docs/openapi/swagger.yamlindex a3a4268866..15a8238a4a 100644--- a/docs/openapi/swagger.yaml+++ b/docs/openapi/swagger.yaml@@ -34,62 +34,66 @@ paths:/sql/advise:get:consumes:- - "*/*"- description: Parse and check the SQL statement according to the SQL review- policy.+ - '*/*'+ description: Parse and check the SQL statement according to the SQL review policy.parameters:- - description: The environment name. Case sensitive.- in: query- name: environment- required: true+ - description: The environment name. Case sensitive.+ in: body+ name: environmentName+ required: true+ schema:type: string- - description: The SQL statement.- in: query- name: statement- required: true+ - description: The SQL statement.+ in: body+ name: statement+ required: true+ schema:type: string- - description:- The database type. Required if the port, host and database name- is not specified.- enum:- - MYSQL- - POSTGRES- - TIDB- in: query- name: databaseType+ - description: The database type. Required if the port, host and database name+ is not specified.+ enum:+ - MYSQL+ - POSTGRES+ - TIDB+ in: body+ name: databaseType+ schema:type: string- - description: The instance host.- in: query- name: host+ - description: The instance host.+ in: body+ name: host+ schema:type: string- - description: The instance port.- in: query- name: port+ - description: The instance port.+ in: body+ name: port+ schema:type: string- - description: The database name in the instance.- in: query- name: databaseName+ - description: The database name in the instance.+ in: body+ name: databaseName+ schema:type: stringproduces:- - application/json+ - application/jsonresponses:"200":description: OKschema:items:- $ref: "#/definitions/advisor.Advice"+ $ref: '#/definitions/advisor.Advice'type: array"400":description: Bad Requestschema:- $ref: "#/definitions/echo.HTTPError"+ $ref: '#/definitions/echo.HTTPError'"500":description: Internal Server Errorschema:- $ref: "#/definitions/echo.HTTPError"+ $ref: '#/definitions/echo.HTTPError'summary: Check the SQL statement.tags:- - SQL review+ - SQL reviewschemes:- - http+- httpswagger: "2.0"diff --git a/docs/sqlservice/docs.go b/docs/sqlservice/docs.goindex d195b493d5..ab60608b00 100644--- a/docs/sqlservice/docs.go+++ b/docs/sqlservice/docs.go@@ -29,7 +29,7 @@ const docTemplate = `{"get": {"description": "Parse and check the SQL statement according to the SQL review rules.","consumes": [- "*/*"+ "application/json"],"produces": ["application/json"@@ -40,11 +40,13 @@ const docTemplate = `{"summary": "Check the SQL statement.","parameters": [{- "type": "string","description": "The SQL statement.","name": "statement",- "in": "query",- "required": true+ "in": "body",+ "required": true,+ "schema": {+ "type": "string"+ }},{"enum": [@@ -52,27 +54,33 @@ const docTemplate = `{"POSTGRES","TIDB"],- "type": "string","description": "The database type.","name": "databaseType",- "in": "query",- "required": true+ "in": "body",+ "required": true,+ "schema": {+ "type": "string"+ }},{"enum": ["bb.sql-review.prod","bb.sql-review.dev"],- "type": "string","description": "The SQL check template id. Required if the config is not specified.",- "name": "template",- "in": "query"+ "name": "templateId",+ "in": "body",+ "schema": {+ "type": "string"+ }},{- "type": "string","description": "The SQL check config override string in YAML format. Check https://github.com/bytebase/bytebase/tree/main/plugin/advisor/config/sql-review.override.yaml for example. Required if the template is not specified.","name": "override",- "in": "query"+ "in": "body",+ "schema": {+ "type": "string"+ }}],"responses": {diff --git a/docs/sqlservice/swagger.yaml b/docs/sqlservice/swagger.yamlindex 8a9c10fe50..df3859d941 100644--- a/docs/sqlservice/swagger.yaml+++ b/docs/sqlservice/swagger.yaml@@ -34,57 +34,59 @@ paths:/sql/advise:get:consumes:- - "*/*"- description: Parse and check the SQL statement according to the SQL review- rules.+ - application/json+ description: Parse and check the SQL statement according to the SQL review rules.parameters:- - description: The SQL statement.- in: query- name: statement- required: true+ - description: The SQL statement.+ in: body+ name: statement+ required: true+ schema:type: string- - description: The database type.- enum:- - MYSQL- - POSTGRES- - TIDB- in: query- name: databaseType- required: true+ - description: The database type.+ enum:+ - MYSQL+ - POSTGRES+ - TIDB+ in: body+ name: databaseType+ required: true+ schema:type: string- - description: The SQL check template id. Required if the config is not specified.- enum:- - bb.sql-review.prod- - bb.sql-review.dev- in: query- name: template+ - description: The SQL check template id. Required if the config is not specified.+ enum:+ - bb.sql-review.prod+ - bb.sql-review.dev+ in: body+ name: templateId+ schema:type: string- - description:- The SQL check config override string in YAML format. Check https://github.com/bytebase/bytebase/tree/main/plugin/advisor/config/sql-review.override.yaml- for example. Required if the template is not specified.- in: query- name: override+ - description: The SQL check config override string in YAML format. Check https://github.com/bytebase/bytebase/tree/main/plugin/advisor/config/sql-review.override.yaml+ for example. Required if the template is not specified.+ in: body+ name: override+ schema:type: stringproduces:- - application/json+ - application/jsonresponses:"200":description: OKschema:items:- $ref: "#/definitions/advisor.Advice"+ $ref: '#/definitions/advisor.Advice'type: array"400":description: Bad Requestschema:- $ref: "#/definitions/echo.HTTPError"+ $ref: '#/definitions/echo.HTTPError'"500":description: Internal Server Errorschema:- $ref: "#/definitions/echo.HTTPError"+ $ref: '#/definitions/echo.HTTPError'summary: Check the SQL statement.tags:- - SQL review+ - SQL reviewschemes:- - http+- httpswagger: "2.0"diff --git a/server/openapi.go b/server/openapi.goindex d152171c3a..8ddc3bd9ce 100644--- a/server/openapi.go+++ b/server/openapi.go@@ -2,7 +2,9 @@ package serverimport ("context"+ "encoding/json""fmt"+ "io""net/http""github.com/bytebase/bytebase/api"@@ -27,8 +29,17 @@ func (*catalogService) GetDatabase(_ context.Context) (*catalog.Database, error)return nil, nil}+type sqlCheckRequestBody struct {+ Statement string `json:"statement"`+ DatabaseType string `json:"databaseType"`+ DatabaseName string `json:"databaseName"`+ EnvironmentName string `json:"environmentName"`+ Host string `json:"host"`+ Port string `json:"port"`+}+func (s *Server) registerOpenAPIRoutes(g *echo.Group) {- g.GET("/sql/advise", s.sqlCheckController)+ g.POST("/sql/advise", s.sqlCheckController)}// sqlCheckController godoc@@ -37,24 +48,31 @@ func (s *Server) registerOpenAPIRoutes(g *echo.Group) {// @Accept */*// @Tags SQL review// @Produce json-// @Param environment query string true "The environment name. Case sensitive."-// @Param statement query string true "The SQL statement."-// @Param databaseType query string false "The database type. Required if the port, host and database name is not specified." Enums(MYSQL, POSTGRES, TIDB)-// @Param host query string false "The instance host."-// @Param port query string false "The instance port."-// @Param databaseName query string false "The database name in the instance."+// @Param environmentName body string true "The environment name. Case sensitive."+// @Param statement body string true "The SQL statement."+// @Param databaseType body string false "The database type. Required if the port, host and database name is not specified." Enums(MYSQL, POSTGRES, TIDB)+// @Param host body string false "The instance host."+// @Param port body string false "The instance port."+// @Param databaseName body string false "The database name in the instance."// @Success 200 {array} advisor.Advice// @Failure 400 {object} echo.HTTPError// @Failure 500 {object} echo.HTTPError// @Router /sql/advise [get].func (s *Server) sqlCheckController(c echo.Context) error {- envName := c.QueryParams().Get("environment")- if envName == "" {+ request := &sqlCheckRequestBody{}+ body, err := io.ReadAll(c.Request().Body)+ if err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Failed to read request body").SetInternal(err)+ }+ if err := json.Unmarshal(body, request); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Cannot format request body").SetInternal(err)+ }++ if request.EnvironmentName == "" {return echo.NewHTTPError(http.StatusBadRequest, "Missing required environment name")}- statement := c.QueryParams().Get("statement")- if statement == "" {+ if request.Statement == "" {return echo.NewHTTPError(http.StatusBadRequest, "Missing required SQL statement")}@@ -62,12 +80,8 @@ func (s *Server) sqlCheckController(c echo.Context) error {var databaseType stringvar catalog catalog.Catalog = &catalogService{}- databaseName := c.QueryParams().Get("databaseName")- host := c.QueryParams().Get("host")- port := c.QueryParams().Get("port")-- if databaseName != "" && host != "" && port != "" {- database, err := s.findDatabase(ctx, host, port, databaseName)+ if request.DatabaseName != "" && request.Host != "" && request.Port != "" {+ database, err := s.findDatabase(ctx, request.Host, request.Port, request.DatabaseName)if err != nil {return err}@@ -75,7 +89,7 @@ func (s *Server) sqlCheckController(c echo.Context) error {databaseType = string(dbType)catalog = store.NewCatalog(&database.ID, s.store, dbType)} else {- databaseType = c.QueryParams().Get("databaseType")+ databaseType = request.DatabaseTypeif databaseType == "" {return echo.NewHTTPError(http.StatusBadRequest, "Missing required database type")}@@ -87,13 +101,13 @@ func (s *Server) sqlCheckController(c echo.Context) error {}envList, err := s.store.FindEnvironment(ctx, &api.EnvironmentFind{- Name: &envName,+ Name: &request.EnvironmentName,})if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Failed to find environment %s", envName)).SetInternal(err)+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Failed to find environment %s", request.EnvironmentName)).SetInternal(err)}if len(envList) != 1 {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid environment %s", envName))+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid environment %s", request.EnvironmentName))}_, adviceList, err := s.sqlCheck(@@ -102,7 +116,7 @@ func (s *Server) sqlCheckController(c echo.Context) error {"utf8mb4","utf8mb4_general_ci",envList[0].ID,- statement,+ request.Statement,catalog,)if err != nil {@@ -115,7 +129,7 @@ func (s *Server) sqlCheckController(c echo.Context) error {Value: 1,Labels: map[string]string{"database_type": databaseType,- "environment": envName,+ "environment": request.EnvironmentName,},})}diff --git a/sql-server/advise.go b/sql-server/advise.goindex 924863501c..89e52ae579 100644--- a/sql-server/advise.go+++ b/sql-server/advise.go@@ -2,7 +2,9 @@ package sqlserverimport ("context"+ "encoding/json""fmt"+ "io""net/http""github.com/bytebase/bytebase/plugin/advisor"@@ -25,55 +27,64 @@ func (*catalogService) GetDatabase(_ context.Context) (*catalog.Database, error)return nil, nil}+type sqlCheckRequestBody struct {+ Statement string `json:"statement"`+ DatabaseType string `json:"databaseType"`+ TemplateID advisor.SQLReviewTemplateID `json:"templateId"`+ Override string `json:"override"`+}+func (s *Server) registerAdvisorRoutes(g *echo.Group) {- g.GET("/sql/advise", s.sqlCheckController)+ g.POST("/sql/advise", s.sqlCheckController)}// sqlCheckController godoc// @Summary Check the SQL statement.// @Description Parse and check the SQL statement according to the SQL review rules.-// @Accept */*+// @Accept application/json// @Tags SQL review// @Produce json-// @Param statement query string true "The SQL statement."-// @Param databaseType query string true "The database type." Enums(MYSQL, POSTGRES, TIDB)-// @Param template query string false "The SQL check template id. Required if the config is not specified." Enums(bb.sql-review.prod, bb.sql-review.dev)-// @Param override query string false "The SQL check config override string in YAML format. Check https://github.com/bytebase/bytebase/tree/main/plugin/advisor/config/sql-review.override.yaml for example. Required if the template is not specified."+// @Param statement body string true "The SQL statement."+// @Param databaseType body string true "The database type." Enums(MYSQL, POSTGRES, TIDB)+// @Param templateId body string false "The SQL check template id. Required if the config is not specified." Enums(bb.sql-review.prod, bb.sql-review.dev)+// @Param override body string false "The SQL check config override string in YAML format. Check https://github.com/bytebase/bytebase/tree/main/plugin/advisor/config/sql-review.override.yaml for example. Required if the template is not specified."// @Success 200 {array} advisor.Advice// @Failure 400 {object} echo.HTTPError// @Failure 500 {object} echo.HTTPError// @Router /sql/advise [get].func (*Server) sqlCheckController(c echo.Context) error {- statement := c.QueryParams().Get("statement")- if statement == "" {- return echo.NewHTTPError(http.StatusBadRequest, "Missing required SQL statement")+ request := &sqlCheckRequestBody{}+ body, err := io.ReadAll(c.Request().Body)+ if err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Failed to read request body").SetInternal(err)}-- databaseType := c.QueryParams().Get("databaseType")- if databaseType == "" {- return echo.NewHTTPError(http.StatusBadRequest, "Missing required database type")+ if err := json.Unmarshal(body, request); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Cannot format request body").SetInternal(err)}- advisorDBType, err := advisorDB.ConvertToAdvisorDBType(databaseType)- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Database %s is not support", databaseType))++ if request.Statement == "" {+ return echo.NewHTTPError(http.StatusBadRequest, "Missing required SQL statement")}- template := c.QueryParams().Get("template")- configOverrideYAMLStr := c.QueryParams().Get("override")- if template == "" && configOverrideYAMLStr == "" {+ if request.Override == "" && request.TemplateID == "" {return echo.NewHTTPError(http.StatusBadRequest, "Missing required template or override")}+ advisorDBType, err := advisorDB.ConvertToAdvisorDBType(request.DatabaseType)+ if err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Database %s is not support", request.DatabaseType))+ }+ruleOverride := &advisor.SQLReviewConfigOverride{}- if configOverrideYAMLStr != "" {- if err := yaml.Unmarshal([]byte(configOverrideYAMLStr), ruleOverride); err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid config: %v", configOverrideYAMLStr)).SetInternal(err)+ if request.Override != "" {+ if err := yaml.Unmarshal([]byte(request.Override), ruleOverride); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid config: %v", request.Override)).SetInternal(err)}- if template != "" && string(ruleOverride.Template) != template {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("The config override should extend from the same template. Found %s in override but also get %s template in request.", ruleOverride.Template, template))+ if request.TemplateID != "" && ruleOverride.Template != request.TemplateID {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("The config override should extend from the same template. Found %s in override but also get %s template in request.", ruleOverride.Template, request.TemplateID))}} else {- ruleOverride.Template = advisor.SQLReviewTemplateID(template)+ ruleOverride.Template = request.TemplateID}ruleList, err := advisor.MergeSQLReviewRules(ruleOverride)@@ -85,7 +96,7 @@ func (*Server) sqlCheckController(c echo.Context) error {advisorDBType,"utf8mb4","utf8mb4_general_ci",- statement,+ request.Statement,ruleList,&catalogService{},)