From 450ab6b252f1f3058c420fd21267b9a38f738811 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Tue, 1 Nov 2022 17:39:53 +0100 Subject: [PATCH] shared providers: allow to immediately re-add soft-deleted event rules Signed-off-by: Nicola Murino --- internal/dataprovider/sqlcommon.go | 6 ++++++ internal/dataprovider/sqlqueries.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/internal/dataprovider/sqlcommon.go b/internal/dataprovider/sqlcommon.go index a873c1ba..8ffce39b 100644 --- a/internal/dataprovider/sqlcommon.go +++ b/internal/dataprovider/sqlcommon.go @@ -3133,6 +3133,12 @@ func sqlCommonAddEventRule(rule *EventRule, dbHandle *sql.DB) error { defer cancel() return sqlCommonExecuteTx(ctx, dbHandle, func(tx *sql.Tx) error { + if config.IsShared == 1 { + _, err := tx.ExecContext(ctx, getRemoveSoftDeletedRuleQuery(), rule.Name) + if err != nil { + return err + } + } q := getAddEventRuleQuery() _, err := tx.ExecContext(ctx, q, rule.Name, rule.Description, util.GetTimeAsMsSinceEpoch(time.Now()), util.GetTimeAsMsSinceEpoch(time.Now()), rule.Trigger, string(conditions)) diff --git a/internal/dataprovider/sqlqueries.go b/internal/dataprovider/sqlqueries.go index b2230897..2ae1ed30 100644 --- a/internal/dataprovider/sqlqueries.go +++ b/internal/dataprovider/sqlqueries.go @@ -895,6 +895,10 @@ func getDeleteEventRuleQuery(softDelete bool) string { return fmt.Sprintf(`DELETE FROM %s WHERE name = %s`, sqlTableEventsRules, sqlPlaceholders[0]) } +func getRemoveSoftDeletedRuleQuery() string { + return fmt.Sprintf(`DELETE FROM %s WHERE name = %s AND deleted_at > 0`, sqlTableEventsRules, sqlPlaceholders[0]) +} + func getClearRuleActionMappingQuery() string { return fmt.Sprintf(`DELETE FROM %s WHERE rule_id = (SELECT id FROM %s WHERE name = %s)`, sqlTableRulesActionsMapping, sqlTableEventsRules, sqlPlaceholders[0])