Skip to content

Commit

Permalink
Merge pull request #240 from actiontech/main
Browse files Browse the repository at this point in the history
sync main into main-ee
  • Loading branch information
ColdWaterLW authored Jun 14, 2022
2 parents 22563b6 + a671a80 commit db66f58
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 7 deletions.
3 changes: 3 additions & 0 deletions sqle/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config config.SqleConfi
v1Router.GET("/instances/:instance_name/rules", v1.GetInstanceRules)
v1Router.GET("/instances/:instance_name/workflow_template", v1.GetInstanceWorkflowTemplate)
v1Router.GET("/instances/:instance_name/schemas/:schema_name/tables", v1.ListTableBySchema)
v1Router.GET("/instances/:instance_name/schemas/:schema_name/tables/:table_name/metadata", v1.GetTableMetadata)

// rule template
v1Router.GET("/rule_templates", v1.GetRuleTemplates)
v1Router.GET("/rule_template_tips", v1.GetRuleTemplateTips)
Expand Down Expand Up @@ -233,6 +235,7 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config config.SqleConfi
v1Router.POST("/sql_query/prepare/:instance_name/", v1.PrepareSQLQuery)
v1Router.GET("/sql_query/history/:instance_name/", v1.GetSQLQueryHistory)
v1Router.GET("/sql_query/results/:query_id/", v1.GetSQLResult)
v1Router.GET("/sql_query/explain/:instance_name/", v1.GetSQLExplain)

// UI
e.File("/", "ui/index.html")
Expand Down
3 changes: 2 additions & 1 deletion sqle/api/controller/v1/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,7 @@ type TableIndexes struct {

type TableMeta struct {
Name string `json:"name"`
Schema string `json:"schema"`
Columns TableColumns `json:"columns"`
Indexes TableIndexes `json:"indexes"`
CreateTableSQL string `json:"create_table_sql"`
Expand All @@ -1103,5 +1104,5 @@ type GetTableMetadataResV1 struct {
// @Success 200 {object} v1.GetTableMetadataResV1
// @router /v1/instances/{instance_name}/schemas/{schema_name}/tables/{table_name}/metadata [get]
func GetTableMetadata(c echo.Context) error {
return nil
return getTableMetadata(c)
}
5 changes: 5 additions & 0 deletions sqle/api/controller/v1/instance_ce.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

var errCommunityEditionDoesNotSupportListTables = errors.New(errors.EnterpriseEditionFeatures, e.New("community edition does not support list tables"))
var errCommunityEditionDoesNotSupportGetTablesMetadata = errors.New(errors.EnterpriseEditionFeatures, e.New("community edition does not support get table metadata"))

func getInstanceTips(c echo.Context) error {
req := new(InstanceTipReqV1)
Expand Down Expand Up @@ -54,3 +55,7 @@ func getInstanceTips(c echo.Context) error {
func listTableBySchema(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportListTables)
}

func getTableMetadata(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportGetTablesMetadata)
}
2 changes: 1 addition & 1 deletion sqle/api/controller/v1/sql_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ type GetSQLExplainResV1 struct {
// @Success 200 {object} v1.GetSQLExplainResV1
// @router /v1/sql_query/explain/{instance_name}/ [get]
func GetSQLExplain(c echo.Context) error {
return nil
return getSQLExplain(c)
}
4 changes: 4 additions & 0 deletions sqle/api/controller/v1/sql_query_ce.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func getSQLResult(c echo.Context) error {
func getSQLQueryHistory(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportSqlQuery)
}

func getSQLExplain(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportSqlQuery)
}
1 change: 1 addition & 0 deletions sqle/api/controller/v2/audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func GetAuditPlanReportSQLs(c echo.Context) error {
auditPlanReportSQLsResV2[i] = AuditPlanReportSQLResV2{
SQL: auditPlanReportSQL.SQL,
AuditResult: auditPlanReportSQL.AuditResult,
Number: auditPlanReportSQL.Number,
}
}
return c.JSON(http.StatusOK, &GetAuditPlanReportSQLsResV2{
Expand Down
3 changes: 3 additions & 0 deletions sqle/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7185,6 +7185,9 @@ var doc = `{
},
"name": {
"type": "string"
},
"schema": {
"type": "string"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions sqle/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7169,6 +7169,9 @@
},
"name": {
"type": "string"
},
"schema": {
"type": "string"
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions sqle/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,8 @@ definitions:
type: object
name:
type: string
schema:
type: string
type: object
v1.TableMetaItemHeadResV1:
properties:
Expand Down
2 changes: 2 additions & 0 deletions sqle/driver/driver_analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type IndexesInfo struct {
}

type TableMetaItem struct {
Name string
Schema string
ColumnsInfo ColumnsInfo
IndexesInfo IndexesInfo
CreateTableSQL string
Expand Down
6 changes: 3 additions & 3 deletions sqle/model/audit_plan_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ func TestStorage_GetAuditPlanReportSQLsByReq(t *testing.T) {
assert.NoError(t, err)
defer mockDB.Close()
InitMockStorage(mockDB)
mock.ExpectPrepare(fmt.Sprintf(`SELECT report_sqls.sql, report_sqls.audit_result %v LIMIT ? OFFSET ?`, tableAndRowOfSQL)).
mock.ExpectPrepare(fmt.Sprintf(`SELECT report_sqls.sql, report_sqls.audit_result, report_sqls.number %v LIMIT ? OFFSET ?`, tableAndRowOfSQL)).
ExpectQuery().WithArgs(1, 100, 10).WillReturnRows(sqlmock.NewRows([]string{
"sql", "audit_result",
}).AddRow("select * from t1 where id = 1", "FAKE AUDIT RESULT"))
"sql", "audit_result", "number",
}).AddRow("select * from t1 where id = 1", "FAKE AUDIT RESULT", "1"))

mock.ExpectPrepare(fmt.Sprintf(`SELECT COUNT(*) %v`, tableAndRowOfSQL)).
ExpectQuery().WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("2"))
Expand Down
1 change: 1 addition & 0 deletions sqle/model/audit_plan_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type AuditPlanReportSQLV2 struct {
Model
AuditPlanReportID uint `json:"audit_plan_report_id" gorm:"index"`
SQL string `json:"sql" gorm:"type:text;not null"`
Number uint `json:"number"`
AuditResult string `json:"audit_result" gorm:"type:text"`

AuditPlanReport *AuditPlanReportV2 `gorm:"foreignkey:AuditPlanReportID"`
Expand Down
3 changes: 2 additions & 1 deletion sqle/model/audit_plan_report_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ func (s *Storage) GetAuditPlanReportsByReq(data map[string]interface{}) (
type AuditPlanReportSQLListDetail struct {
SQL string `json:"sql"`
AuditResult string `json:"audit_result"`
Number uint `json:"number"`
}

var auditPlanReportSQLQueryTpl = `
SELECT report_sqls.sql, report_sqls.audit_result
SELECT report_sqls.sql, report_sqls.audit_result, report_sqls.number
{{- template "body" . -}}
Expand Down
3 changes: 2 additions & 1 deletion sqle/server/auditplan/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ func (at *baseTask) audit(task *model.Task) (*model.AuditPlanReportV2, error) {
Score: task.Score,
AuditLevel: task.AuditLevel,
}
for _, executeSQL := range task.ExecuteSQLs {
for i, executeSQL := range task.ExecuteSQLs {
auditPlanReport.AuditPlanReportSQLs = append(auditPlanReport.AuditPlanReportSQLs, &model.AuditPlanReportSQLV2{
SQL: executeSQL.Content,
Number: uint(i + 1),
AuditResult: executeSQL.AuditResult,
})
}
Expand Down

0 comments on commit db66f58

Please sign in to comment.