@@ -7,13 +7,14 @@ import (
7
7
"github.com/icinga/icinga-notifications/internal/rule"
8
8
"github.com/icinga/icinga-notifications/internal/utils"
9
9
"github.com/icinga/icingadb/pkg/types"
10
+ "github.com/jmoiron/sqlx"
10
11
"time"
11
12
)
12
13
13
14
// Sync initiates an *incident.IncidentRow from the current incident state and syncs it with the database.
14
15
// Before syncing any incident related database entries, this method should be called at least once.
15
16
// Returns an error on db failure.
16
- func (i * Incident ) Sync () error {
17
+ func (i * Incident ) Sync (tx * sqlx. Tx ) error {
17
18
incidentRow := & IncidentRow {
18
19
ID : i .incidentRowID ,
19
20
ObjectID : i .Object .ID ,
@@ -22,7 +23,7 @@ func (i *Incident) Sync() error {
22
23
Severity : i .Severity (),
23
24
}
24
25
25
- err := incidentRow .Sync (i .db , i .incidentRowID != 0 )
26
+ err := incidentRow .Sync (tx , i .db , i .incidentRowID != 0 )
26
27
if err != nil {
27
28
return err
28
29
}
@@ -32,54 +33,51 @@ func (i *Incident) Sync() error {
32
33
return nil
33
34
}
34
35
35
- func (i * Incident ) AddHistory (historyRow * HistoryRow , fetchId bool ) (types.Int , error ) {
36
+ func (i * Incident ) AddHistory (tx * sqlx. Tx , historyRow * HistoryRow , fetchId bool ) (types.Int , error ) {
36
37
historyRow .IncidentID = i .incidentRowID
37
38
38
39
stmt := utils .BuildInsertStmtWithout (i .db , historyRow , "id" )
39
40
if fetchId {
40
- historyId , err := utils .InsertAndFetchId (i . db , stmt , historyRow )
41
+ historyId , err := utils .InsertAndFetchId (tx , stmt , historyRow )
41
42
if err != nil {
42
43
return types.Int {}, err
43
44
}
44
45
45
46
return utils .ToDBInt (historyId ), nil
46
47
} else {
47
- _ , err := i . db .NamedExec (stmt , historyRow )
48
+ _ , err := tx .NamedExec (stmt , historyRow )
48
49
if err != nil {
49
- return types.Int {}, fmt . Errorf ( "failed to insert incident history: %w" , err )
50
+ return types.Int {}, err
50
51
}
51
52
}
52
53
53
54
return types.Int {}, nil
54
55
}
55
56
56
- func (i * Incident ) AddEscalationTriggered (state * EscalationState , hr * HistoryRow ) (types.Int , error ) {
57
+ func (i * Incident ) AddEscalationTriggered (tx * sqlx. Tx , state * EscalationState , hr * HistoryRow ) (types.Int , error ) {
57
58
state .IncidentID = i .incidentRowID
58
59
59
60
stmt , _ := i .db .BuildUpsertStmt (state )
60
- _ , err := i . db .NamedExec (stmt , state )
61
+ _ , err := tx .NamedExec (stmt , state )
61
62
if err != nil {
62
- return types.Int {}, fmt . Errorf ( "failed to insert incident rule escalation state: %w" , err )
63
+ return types.Int {}, err
63
64
}
64
65
65
- return i .AddHistory (hr , true )
66
+ return i .AddHistory (tx , hr , true )
66
67
}
67
68
68
69
// AddEvent Inserts incident history record to the database and returns an error on db failure.
69
- func (i * Incident ) AddEvent (ev * event.Event ) error {
70
+ func (i * Incident ) AddEvent (tx * sqlx. Tx , ev * event.Event ) error {
70
71
ie := & EventRow {IncidentID : i .incidentRowID , EventID : ev .ID }
71
72
stmt , _ := i .db .BuildInsertStmt (ie )
72
- _ , err := i .db .NamedExec (stmt , ie )
73
- if err != nil {
74
- return fmt .Errorf ("failed to insert incident event: %w" , err )
75
- }
73
+ _ , err := tx .NamedExec (stmt , ie )
76
74
77
- return nil
75
+ return err
78
76
}
79
77
80
78
// AddRecipient adds recipient from the given *rule.Escalation to this incident.
81
79
// Syncs also all the recipients with the database and returns an error on db failure.
82
- func (i * Incident ) AddRecipient (escalation * rule.Escalation , eventId int64 ) error {
80
+ func (i * Incident ) AddRecipient (tx * sqlx. Tx , escalation * rule.Escalation , eventId int64 ) error {
83
81
newRole := RoleRecipient
84
82
if i .HasManager () {
85
83
newRole = RoleSubscriber
@@ -112,7 +110,7 @@ func (i *Incident) AddRecipient(escalation *rule.Escalation, eventId int64) erro
112
110
OldRecipientRole : oldRole ,
113
111
}
114
112
115
- _ , err := i .AddHistory (hr , false )
113
+ _ , err := i .AddHistory (tx , hr , false )
116
114
if err != nil {
117
115
return err
118
116
}
@@ -121,7 +119,7 @@ func (i *Incident) AddRecipient(escalation *rule.Escalation, eventId int64) erro
121
119
}
122
120
123
121
stmt , _ := i .db .BuildUpsertStmt (cr )
124
- _ , err := i . db .NamedExec (stmt , cr )
122
+ _ , err := tx .NamedExec (stmt , cr )
125
123
if err != nil {
126
124
return fmt .Errorf ("failed to upsert incident contact %s: %w" , r , err )
127
125
}
@@ -132,18 +130,18 @@ func (i *Incident) AddRecipient(escalation *rule.Escalation, eventId int64) erro
132
130
133
131
// AddRuleMatchedHistory syncs the given *rule.Rule and history entry to the database.
134
132
// Returns an error on database failure.
135
- func (i * Incident ) AddRuleMatchedHistory (r * rule.Rule , hr * HistoryRow ) (types.Int , error ) {
133
+ func (i * Incident ) AddRuleMatchedHistory (tx * sqlx. Tx , r * rule.Rule , hr * HistoryRow ) (types.Int , error ) {
136
134
rr := & RuleRow {IncidentID : i .incidentRowID , RuleID : r .ID }
137
135
stmt , _ := i .db .BuildUpsertStmt (rr )
138
- _ , err := i . db .NamedExec (stmt , rr )
136
+ _ , err := tx .NamedExec (stmt , rr )
139
137
if err != nil {
140
- return types.Int {}, fmt . Errorf ( "failed to insert incident rule: %w" , err )
138
+ return types.Int {}, err
141
139
}
142
140
143
- return i .AddHistory (hr , true )
141
+ return i .AddHistory (tx , hr , true )
144
142
}
145
143
146
- func (i * Incident ) AddSourceSeverity (severity event.Severity , sourceID int64 ) error {
144
+ func (i * Incident ) AddSourceSeverity (tx * sqlx. Tx , severity event.Severity , sourceID int64 ) error {
147
145
i .SeverityBySource [sourceID ] = severity
148
146
149
147
sourceSeverity := & SourceSeverity {
@@ -153,7 +151,7 @@ func (i *Incident) AddSourceSeverity(severity event.Severity, sourceID int64) er
153
151
}
154
152
155
153
stmt , _ := i .db .BuildUpsertStmt (sourceSeverity )
156
- _ , err := i . db .NamedExec (stmt , sourceSeverity )
154
+ _ , err := tx .NamedExec (stmt , sourceSeverity )
157
155
158
156
return err
159
157
}
0 commit comments