From 6482284658b4369e2b5fe85bdfb2d9eef9f86680 Mon Sep 17 00:00:00 2001 From: iwanghc Date: Wed, 31 Jul 2024 11:15:53 +0800 Subject: [PATCH 1/4] fix: add instance audit plan result data param name --- sqle/api/controller/v1/instance_audit_plan.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqle/api/controller/v1/instance_audit_plan.go b/sqle/api/controller/v1/instance_audit_plan.go index 44361b965..a8c4cd043 100644 --- a/sqle/api/controller/v1/instance_audit_plan.go +++ b/sqle/api/controller/v1/instance_audit_plan.go @@ -46,7 +46,7 @@ type AuditPlan struct { type CreatInstanceAuditPlanResV1 struct { controller.BaseRes - Data CreatInstanceAuditPlanRes + Data CreatInstanceAuditPlanRes `json:"data"` } type CreatInstanceAuditPlanRes struct { From 17d3cfe0a641b6a393a33854b2e9cfc3235cde24 Mon Sep 17 00:00:00 2001 From: iwanghc Date: Wed, 31 Jul 2024 11:25:59 +0800 Subject: [PATCH 2/4] fix: instance audit plan business update sync dms data source update --- sqle/dms/instance.go | 1 + 1 file changed, 1 insertion(+) diff --git a/sqle/dms/instance.go b/sqle/dms/instance.go index 56a86617e..8dd8da95f 100644 --- a/sqle/dms/instance.go +++ b/sqle/dms/instance.go @@ -138,6 +138,7 @@ func convertInstance(instance *dmsV1.ListDBService) (*model.Instance, error) { Desc: instance.Desc, AdditionalParams: additionalParams, SqlQueryConfig: sqlQueryConfig, + Business: instance.Business, }, nil } From 6f84c0412f9ca565d25b2aa7d6f7b1688f37bc09 Mon Sep 17 00:00:00 2001 From: iwanghc Date: Wed, 31 Jul 2024 11:26:54 +0800 Subject: [PATCH 3/4] fix: exector sql database name special character --- sqle/driver/mysql/executor/executor.go | 2 +- sqle/driver/mysql/session/context.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sqle/driver/mysql/executor/executor.go b/sqle/driver/mysql/executor/executor.go index 88e1c310a..a77b791e9 100644 --- a/sqle/driver/mysql/executor/executor.go +++ b/sqle/driver/mysql/executor/executor.go @@ -275,7 +275,7 @@ func Ping(entry *logrus.Entry, instance *driverV2.DSN) error { } func (c *Executor) UseSchema(schemaName string) error { - _, err := c.Db.Exec(fmt.Sprintf("use %s", schemaName)) + _, err := c.Db.Exec(fmt.Sprintf("use `%s`", schemaName)) if err != nil { return errors.New(errors.ConnectRemoteDatabaseError, err) } diff --git a/sqle/driver/mysql/session/context.go b/sqle/driver/mysql/session/context.go index bbea0418e..c252462cf 100644 --- a/sqle/driver/mysql/session/context.go +++ b/sqle/driver/mysql/session/context.go @@ -1107,7 +1107,7 @@ func (c *Context) GetColumnCardinality(tn *ast.TableName, columnName string) (in } func (c *Context) UseSchema(schemaName string) error { - _, err := c.e.Db.Exec(fmt.Sprintf("use %s", schemaName)) + _, err := c.e.Db.Exec(fmt.Sprintf("use `%s`", schemaName)) if err != nil { return errors.Wrap(err, "exec use schema") } From 8a61bbdd82a2cc3e7c12e01d16437565ebf14a6f Mon Sep 17 00:00:00 2001 From: iwanghc Date: Wed, 31 Jul 2024 11:28:07 +0800 Subject: [PATCH 4/4] fix: delete audit plan sync delete queue data and fix soft delete --- sqle/model/instance_audit_plan.go | 74 +++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/sqle/model/instance_audit_plan.go b/sqle/model/instance_audit_plan.go index 60af61ac5..945af665f 100644 --- a/sqle/model/instance_audit_plan.go +++ b/sqle/model/instance_audit_plan.go @@ -54,7 +54,7 @@ type AuditPlanDetail struct { func (s *Storage) ListActiveAuditPlanDetail() ([]*AuditPlanDetail, error) { var aps []*AuditPlanDetail - err := s.db.Table("audit_plans_v2").Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). + err := s.db.Model(AuditPlanV2{}).Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). Where("audit_plans_v2.active_status = ? AND instance_audit_plans.active_status = ?", ActiveStatusNormal, ActiveStatusNormal). Select("audit_plans_v2.*,instance_audit_plans.project_id,instance_audit_plans.db_type,instance_audit_plans.token,instance_audit_plans.instance_name,instance_audit_plans.create_user_id"). Scan(&aps).Error @@ -64,7 +64,7 @@ func (s *Storage) ListActiveAuditPlanDetail() ([]*AuditPlanDetail, error) { func (s *Storage) GetAuditPlanDetailByIDType(id int, typ string) (*AuditPlanDetail, error) { var aps *AuditPlanDetail - err := s.db.Table("audit_plans_v2").Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). + err := s.db.Model(AuditPlanV2{}).Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). Where("audit_plans_v2.instance_audit_plan_id = ?", id). Where("audit_plans_v2.type = ?", typ). Select("audit_plans_v2.*,instance_audit_plans.project_id,instance_audit_plans.db_type,instance_audit_plans.token,instance_audit_plans.instance_name,instance_audit_plans.create_user_id"). @@ -90,7 +90,7 @@ func (s *Storage) GetAuditPlanDetailByID(id uint) (*AuditPlanDetail, error) { func (s *Storage) getAuditPlanDetailByID(id uint) (*AuditPlanDetail, bool, error) { var ap *AuditPlanDetail - err := s.db.Table("audit_plans_v2").Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). + err := s.db.Model(AuditPlanV2{}).Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). Where("audit_plans_v2.id = ?", id). Select("audit_plans_v2.*,instance_audit_plans.project_id,instance_audit_plans.db_type,instance_audit_plans.token,instance_audit_plans.instance_name,instance_audit_plans.create_user_id"). Where("audit_plans_v2.active_status = ? AND instance_audit_plans.active_status = ?", ActiveStatusNormal, ActiveStatusNormal). @@ -236,7 +236,7 @@ type SQLManager struct { func (s *Storage) GetAuditPlanByInstanceIdAndType(instanceAuditPlanID string, auditPlanType string) (*AuditPlanV2, bool, error) { auditPlan := &AuditPlanV2{} - err := s.db.Table("audit_plans_v2"). + err := s.db.Model(AuditPlanV2{}). Where("audit_plans_v2.instance_audit_plan_id = ?", instanceAuditPlanID). Where("audit_plans_v2.type = ?", auditPlanType). First(auditPlan).Error @@ -273,33 +273,59 @@ func (s *Storage) BatchSaveAuditPlans(auditPlans []*AuditPlanV2) error { } func (s *Storage) DeleteInstanceAuditPlan(instanceAuditPlanId string) error { - err := s.db.Exec(`UPDATE instance_audit_plans iap - LEFT JOIN audit_plans_v2 ap ON iap.id = ap.instance_audit_plan_id - LEFT JOIN origin_manage_sqls oms ON oms.source_id = ap.id - LEFT JOIN sql_managers sm ON sm.origin_manage_sql_id = oms.id - SET iap.deleted_at = now(), - ap.deleted_at = now(), - oms.deleted_at = now(), - sm.deleted_at = now() - WHERE iap.ID = ?`, instanceAuditPlanId).Error - return err + return s.Tx(func(txDB *gorm.DB) error { + // 删除队列表中数据 + err := txDB.Exec(`DELETE FROM origin_manage_sql_queues USING origin_manage_sql_queues + JOIN audit_plans_v2 ap ON ap.id=origin_manage_sql_queues.source_id + JOIN instance_audit_plans iap ON iap.id = ap.instance_audit_plan_id + WHERE iap.ID = ?`, instanceAuditPlanId).Error + if err != nil { + return err + } + err = txDB.Exec(`UPDATE instance_audit_plans iap + LEFT JOIN audit_plans_v2 ap ON iap.id = ap.instance_audit_plan_id + LEFT JOIN origin_manage_sqls oms ON oms.source_id = ap.id + LEFT JOIN sql_managers sm ON sm.origin_manage_sql_id = oms.id + SET iap.deleted_at = now(), + ap.deleted_at = now(), + oms.deleted_at = now(), + sm.deleted_at = now() + WHERE iap.ID = ?`, instanceAuditPlanId).Error + if err != nil { + return err + } + return nil + }) } func (s *Storage) DeleteAuditPlan(instanceAuditPlanId, auditPlanType string) error { - err := s.db.Exec(`UPDATE instance_audit_plans iap - LEFT JOIN audit_plans_v2 ap ON iap.id = ap.instance_audit_plan_id - LEFT JOIN origin_manage_sqls oms ON oms.source_id = ap.id - LEFT JOIN sql_managers sm ON sm.origin_manage_sql_id = oms.id - SET ap.deleted_at = now(), - oms.deleted_at = now(), - sm.deleted_at = now() - WHERE iap.ID = ? AND ap.type = ?`, instanceAuditPlanId, auditPlanType).Error - return err + return s.Tx(func(txDB *gorm.DB) error { + // 删除队列表中数据 + err := txDB.Exec(`DELETE FROM origin_manage_sql_queues USING origin_manage_sql_queues + JOIN audit_plans_v2 ap ON ap.id=origin_manage_sql_queues.source_id + JOIN instance_audit_plans iap ON iap.id = ap.instance_audit_plan_id + WHERE iap.ID = ? AND ap.type = ?`, instanceAuditPlanId, auditPlanType).Error + if err != nil { + return err + } + err = txDB.Exec(`UPDATE instance_audit_plans iap + LEFT JOIN audit_plans_v2 ap ON iap.id = ap.instance_audit_plan_id + LEFT JOIN origin_manage_sqls oms ON oms.source_id = ap.id + LEFT JOIN sql_managers sm ON sm.origin_manage_sql_id = oms.id + SET ap.deleted_at = now(), + oms.deleted_at = now(), + sm.deleted_at = now() + WHERE iap.ID = ? AND ap.type = ?`, instanceAuditPlanId, auditPlanType).Error + if err != nil { + return err + } + return nil + }) } func (s *Storage) GetAuditPlanDetailByType(InstanceAuditPlanId, auditPlanType string) (*AuditPlanDetail, bool, error) { var auditPlanDetail *AuditPlanDetail - err := s.db.Table("audit_plans_v2").Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). + err := s.db.Model(AuditPlanV2{}).Joins("JOIN instance_audit_plans ON instance_audit_plans.id = audit_plans_v2.instance_audit_plan_id"). Where("instance_audit_plans.id = ? AND audit_plans_v2.type = ?", InstanceAuditPlanId, auditPlanType). Scan(&auditPlanDetail).Error if err == gorm.ErrRecordNotFound {