/
deleteallutil.go
66 lines (59 loc) · 2.11 KB
/
deleteallutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package graphql
import (
"context"
"database/sql"
)
type deleteAllData struct {
EscalationPolicyIDs []string `json:"escalation_policie_ids"`
EscalationPolicyStepIDs []string `json:"escalation_policy_step_ids"`
ServiceIDs []string `json:"service_ids"`
IntegrationKeyIDs []string `json:"integration_key_ids"`
RotationIDs []string `json:"rotation_ids"`
RotationParticipantIDs []string `json:"rotation_participant_ids"`
ScheduleIDs []string `json:"schedule_ids"`
ScheduleRuleIDs []string `json:"schedule_rule_ids"`
HeartbeatMonitorIDs []string `json:"heartbeat_monitor_ids"`
UserOverrideIDs []string `json:"user_override_ids"`
}
func (c *Config) deleteAll(ctx context.Context, data *deleteAllData) error {
tx, err := c.DB.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
deleteIDs := func(fn func(context.Context, *sql.Tx, string) error, ids []string) {
if err != nil {
return
}
for _, id := range ids {
err = fn(ctx, tx, id)
if err != nil {
return
}
}
}
deleteIDs(func(ctx context.Context, tx *sql.Tx, id string) error {
return c.OverrideStore.DeleteUserOverrideTx(ctx, tx, id)
}, data.UserOverrideIDs)
deleteIDs(func(ctx context.Context, tx *sql.Tx, id string) error {
return c.HeartbeatStore.DeleteTx(ctx, tx, id)
}, data.HeartbeatMonitorIDs)
deleteIDs(c.ScheduleRuleStore.DeleteTx, data.ScheduleRuleIDs)
deleteIDs(c.IntegrationKeyStore.DeleteTx, data.IntegrationKeyIDs)
deleteIDs(func(ctx context.Context, tx *sql.Tx, id string) error {
_, err := c.EscalationStore.DeleteStepTx(ctx, tx, id)
return err
}, data.EscalationPolicyStepIDs)
deleteIDs(func(ctx context.Context, tx *sql.Tx, id string) error {
_, err := c.RotationStore.RemoveParticipantTx(ctx, tx, id)
return err
}, data.RotationParticipantIDs)
deleteIDs(c.ScheduleStore.DeleteTx, data.ScheduleIDs)
deleteIDs(c.RotationStore.DeleteRotationTx, data.RotationIDs)
deleteIDs(c.ServiceStore.DeleteTx, data.ServiceIDs)
deleteIDs(c.EscalationStore.DeletePolicyTx, data.EscalationPolicyIDs)
if err != nil {
return err
}
return tx.Commit()
}