Skip to content

Commit 0167a08

Browse files
committed
internal/utils: Cleanup and IGL unification
With Icinga/icinga-go-library#64 and Icinga/icinga-go-library#127 being merged, most of the internal/utils package is moved to the IGL and can be removed here. This is another step to unify our Go codebase.
1 parent ff553d2 commit 0167a08

File tree

12 files changed

+47
-212
lines changed

12 files changed

+47
-212
lines changed

internal/event/event.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"github.com/icinga/icinga-go-library/database"
99
"github.com/icinga/icinga-go-library/types"
10-
"github.com/icinga/icinga-notifications/internal/utils"
1110
"github.com/jmoiron/sqlx"
1211
"time"
1312
)
@@ -176,7 +175,7 @@ func (e *Event) Sync(ctx context.Context, tx *sqlx.Tx, db *database.DB, objectId
176175
}
177176

178177
eventRow := NewEventRow(e, objectId)
179-
eventID, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, eventRow, "id"), eventRow)
178+
eventID, err := database.InsertObtainID(ctx, tx, database.BuildInsertStmtWithout(db, eventRow, "id"), eventRow)
180179
if err == nil {
181180
e.ID = eventID
182181
}
@@ -206,11 +205,11 @@ func NewEventRow(e *Event, objectId types.Binary) *EventRow {
206205
return &EventRow{
207206
Time: types.UnixMilli(e.Time),
208207
ObjectID: objectId,
209-
Type: utils.ToDBString(e.Type),
208+
Type: types.MakeString(e.Type, types.TransformEmptyStringToNull),
210209
Severity: e.Severity,
211-
Username: utils.ToDBString(e.Username),
212-
Message: utils.ToDBString(e.Message),
210+
Username: types.MakeString(e.Username, types.TransformEmptyStringToNull),
211+
Message: types.MakeString(e.Message, types.TransformEmptyStringToNull),
213212
Mute: e.Mute,
214-
MuteReason: utils.ToDBString(e.MuteReason),
213+
MuteReason: types.MakeString(e.MuteReason, types.TransformEmptyStringToNull),
215214
}
216215
}

internal/incident/db_types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/icinga/icinga-go-library/types"
77
"github.com/icinga/icinga-notifications/internal/event"
88
"github.com/icinga/icinga-notifications/internal/recipient"
9-
"github.com/icinga/icinga-notifications/internal/utils"
109
"github.com/jmoiron/sqlx"
1110
)
1211

@@ -92,7 +91,7 @@ func (h *HistoryRow) TableName() string {
9291
// Sync persists the current state of this history to the database and retrieves the just inserted history ID.
9392
// Returns error when failed to execute the query.
9493
func (h *HistoryRow) Sync(ctx context.Context, db *database.DB, tx *sqlx.Tx) error {
95-
historyId, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, h, "id"), h)
94+
historyId, err := database.InsertObtainID(ctx, tx, database.BuildInsertStmtWithout(db, h, "id"), h)
9695
if err != nil {
9796
return err
9897
}

internal/incident/incident.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"sync"
8+
"time"
9+
710
"github.com/icinga/icinga-go-library/database"
811
"github.com/icinga/icinga-go-library/types"
912
"github.com/icinga/icinga-notifications/internal/config"
@@ -13,11 +16,8 @@ import (
1316
"github.com/icinga/icinga-notifications/internal/object"
1417
"github.com/icinga/icinga-notifications/internal/recipient"
1518
"github.com/icinga/icinga-notifications/internal/rule"
16-
"github.com/icinga/icinga-notifications/internal/utils"
1719
"github.com/jmoiron/sqlx"
1820
"go.uber.org/zap"
19-
"sync"
20-
"time"
2121
)
2222

2323
type ruleID = int64
@@ -252,7 +252,7 @@ func (i *Incident) RetriggerEscalations(ev *event.Event) {
252252

253253
var notifications []*NotificationEntry
254254
ctx := context.Background()
255-
err = utils.RunInTx(ctx, i.db, func(tx *sqlx.Tx) error {
255+
err = i.db.ExecTx(ctx, func(ctx context.Context, tx *sqlx.Tx) error {
256256
err := ev.Sync(ctx, tx, i.db, i.Object.ID)
257257
if err != nil {
258258
return err
@@ -298,12 +298,12 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx,
298298

299299
hr := &HistoryRow{
300300
IncidentID: i.Id,
301-
EventID: utils.ToDBInt(ev.ID),
301+
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
302302
Time: types.UnixMilli(time.Now()),
303303
Type: IncidentSeverityChanged,
304304
NewSeverity: newSeverity,
305305
OldSeverity: oldSeverity,
306-
Message: utils.ToDBString(ev.Message),
306+
Message: types.MakeString(ev.Message, types.TransformEmptyStringToNull),
307307
}
308308

309309
if err := hr.Sync(ctx, i.db, tx); err != nil {
@@ -319,7 +319,7 @@ func (i *Incident) processSeverityChangedEvent(ctx context.Context, tx *sqlx.Tx,
319319

320320
hr = &HistoryRow{
321321
IncidentID: i.Id,
322-
EventID: utils.ToDBInt(ev.ID),
322+
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
323323
Time: i.RecoveredAt,
324324
Type: Closed,
325325
}
@@ -357,9 +357,9 @@ func (i *Incident) processIncidentOpenedEvent(ctx context.Context, tx *sqlx.Tx,
357357
IncidentID: i.Id,
358358
Type: Opened,
359359
Time: types.UnixMilli(ev.Time),
360-
EventID: utils.ToDBInt(ev.ID),
360+
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
361361
NewSeverity: i.Severity,
362-
Message: utils.ToDBString(ev.Message),
362+
Message: types.MakeString(ev.Message, types.TransformEmptyStringToNull),
363363
}
364364

365365
if err := hr.Sync(ctx, i.db, tx); err != nil {
@@ -377,7 +377,7 @@ func (i *Incident) handleMuteUnmute(ctx context.Context, tx *sqlx.Tx, ev *event.
377377
return nil
378378
}
379379

380-
hr := &HistoryRow{IncidentID: i.Id, EventID: utils.ToDBInt(ev.ID), Time: types.UnixMilli(time.Now())}
380+
hr := &HistoryRow{IncidentID: i.Id, EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull), Time: types.UnixMilli(time.Now())}
381381
logger := i.logger.With(zap.String("event", ev.String()))
382382
if i.Object.IsMuted() {
383383
hr.Type = Muted
@@ -388,7 +388,7 @@ func (i *Incident) handleMuteUnmute(ctx context.Context, tx *sqlx.Tx, ev *event.
388388
} else {
389389
hr.Type = Unmuted
390390
// On the other hand, if an object is unmuted, its mute reason is already reset, and we can't access it anymore.
391-
hr.Message = utils.ToDBString(ev.MuteReason)
391+
hr.Message = types.MakeString(ev.MuteReason, types.TransformEmptyStringToNull)
392392
logger.Infow("Unmuting incident", zap.String("reason", ev.MuteReason))
393393
}
394394

@@ -426,8 +426,8 @@ func (i *Incident) evaluateRules(ctx context.Context, tx *sqlx.Tx, eventID int64
426426
hr := &HistoryRow{
427427
IncidentID: i.Id,
428428
Time: types.UnixMilli(time.Now()),
429-
EventID: utils.ToDBInt(eventID),
430-
RuleID: utils.ToDBInt(r.ID),
429+
EventID: types.MakeInt(eventID, types.TransformZeroIntToNull),
430+
RuleID: types.MakeInt(r.ID, types.TransformZeroIntToNull),
431431
Type: RuleMatched,
432432
}
433433
if err := hr.Sync(ctx, i.db, tx); err != nil {
@@ -534,9 +534,9 @@ func (i *Incident) triggerEscalations(ctx context.Context, tx *sqlx.Tx, ev *even
534534
hr := &HistoryRow{
535535
IncidentID: i.Id,
536536
Time: state.TriggeredAt,
537-
EventID: utils.ToDBInt(ev.ID),
538-
RuleEscalationID: utils.ToDBInt(state.RuleEscalationID),
539-
RuleID: utils.ToDBInt(r.ID),
537+
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
538+
RuleEscalationID: types.MakeInt(state.RuleEscalationID, types.TransformZeroIntToNull),
539+
RuleID: types.MakeInt(r.ID, types.TransformZeroIntToNull),
540540
Type: EscalationTriggered,
541541
}
542542

@@ -649,12 +649,12 @@ func (i *Incident) processAcknowledgementEvent(ctx context.Context, tx *sqlx.Tx,
649649
hr := &HistoryRow{
650650
IncidentID: i.Id,
651651
Key: recipientKey,
652-
EventID: utils.ToDBInt(ev.ID),
652+
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
653653
Type: RecipientRoleChanged,
654654
Time: types.UnixMilli(time.Now()),
655655
NewRecipientRole: newRole,
656656
OldRecipientRole: oldRole,
657-
Message: utils.ToDBString(ev.Message),
657+
Message: types.MakeString(ev.Message, types.TransformEmptyStringToNull),
658658
}
659659

660660
if err := hr.Sync(ctx, i.db, tx); err != nil {

internal/incident/incidents.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func ProcessEvent(
245245
}
246246

247247
// There is no active incident, but the event appears to be relevant, so try to persist it in the DB.
248-
err = utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error { return ev.Sync(ctx, tx, db, obj.ID) })
248+
err = db.ExecTx(ctx, func(ctx context.Context, tx *sqlx.Tx) error { return ev.Sync(ctx, tx, db, obj.ID) })
249249
if err != nil {
250250
return errors.New("cannot sync non-state event to the database")
251251
}

internal/incident/incidents_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/icinga/icinga-notifications/internal/event"
1010
"github.com/icinga/icinga-notifications/internal/object"
1111
"github.com/icinga/icinga-notifications/internal/testutils"
12-
"github.com/icinga/icinga-notifications/internal/utils"
1312
"github.com/jmoiron/sqlx"
1413
"github.com/stretchr/testify/assert"
1514
"github.com/stretchr/testify/require"
@@ -31,14 +30,14 @@ func TestLoadOpenIncidents(t *testing.T) {
3130
source.ChangedAt = types.UnixMilli(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))
3231
source.Deleted = types.Bool{Bool: false, Valid: true}
3332

34-
err := utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
35-
id, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, source, "id"), source)
33+
err := db.ExecTx(ctx, func(ctx context.Context, tx *sqlx.Tx) error {
34+
id, err := database.InsertObtainID(ctx, tx, database.BuildInsertStmtWithout(db, source, "id"), source)
3635
require.NoError(t, err, "populating source table should not fail")
3736

3837
source.ID = id
3938
return nil
4039
})
41-
require.NoError(t, err, "utils.RunInTx() should not fail")
40+
require.NoError(t, err, "db.ExecTx should not fail")
4241

4342
// Reduce the default placeholders per statement to a meaningful number, so that we can
4443
// test some parallelism when loading the incidents.

internal/incident/sync.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package incident
33
import (
44
"context"
55
"fmt"
6+
"time"
7+
8+
"github.com/icinga/icinga-go-library/database"
69
"github.com/icinga/icinga-go-library/types"
710
"github.com/icinga/icinga-notifications/internal/event"
811
"github.com/icinga/icinga-notifications/internal/recipient"
912
"github.com/icinga/icinga-notifications/internal/rule"
10-
"github.com/icinga/icinga-notifications/internal/utils"
1113
"github.com/jmoiron/sqlx"
1214
"go.uber.org/zap"
13-
"time"
1415
)
1516

1617
// Upsert implements the contracts.Upserter interface.
@@ -32,8 +33,8 @@ func (i *Incident) Sync(ctx context.Context, tx *sqlx.Tx) error {
3233
return fmt.Errorf("failed to upsert incident: %w", err)
3334
}
3435
} else {
35-
stmt := utils.BuildInsertStmtWithout(i.db, i, "id")
36-
incidentId, err := utils.InsertAndFetchId(ctx, tx, stmt, i)
36+
stmt := database.BuildInsertStmtWithout(i.db, i, "id")
37+
incidentId, err := database.InsertObtainID(ctx, tx, stmt, i)
3738
if err != nil {
3839
return err
3940
}
@@ -89,7 +90,7 @@ func (i *Incident) AddRecipient(ctx context.Context, tx *sqlx.Tx, escalation *ru
8990

9091
hr := &HistoryRow{
9192
IncidentID: i.Id,
92-
EventID: utils.ToDBInt(eventId),
93+
EventID: types.MakeInt(eventId, types.TransformZeroIntToNull),
9394
Key: cr.Key,
9495
Time: types.UnixMilli(time.Now()),
9596
Type: RecipientRoleChanged,
@@ -147,12 +148,12 @@ func (i *Incident) generateNotifications(
147148
hr := &HistoryRow{
148149
IncidentID: i.Id,
149150
Key: recipient.ToKey(contact),
150-
EventID: utils.ToDBInt(ev.ID),
151+
EventID: types.MakeInt(ev.ID, types.TransformZeroIntToNull),
151152
Time: types.UnixMilli(time.Now()),
152153
Type: Notified,
153-
ChannelID: utils.ToDBInt(chID),
154+
ChannelID: types.MakeInt(chID, types.TransformZeroIntToNull),
154155
NotificationState: NotificationStatePending,
155-
Message: utils.ToDBString(ev.Message),
156+
Message: types.MakeString(ev.Message, types.TransformEmptyStringToNull),
156157
}
157158
if suppress {
158159
hr.NotificationState = NotificationStateSuppressed

internal/object/object.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/icinga/icinga-go-library/database"
1313
"github.com/icinga/icinga-go-library/types"
1414
"github.com/icinga/icinga-notifications/internal/event"
15-
"github.com/icinga/icinga-notifications/internal/utils"
1615
"regexp"
1716
"sort"
1817
"strings"
@@ -37,7 +36,7 @@ func New(db *database.DB, ev *event.Event) *Object {
3736
SourceID: ev.SourceId,
3837
Name: ev.Name,
3938
db: db,
40-
URL: utils.ToDBString(ev.URL),
39+
URL: types.MakeString(ev.URL, types.TransformEmptyStringToNull),
4140
Tags: ev.Tags,
4241
ExtraTags: ev.ExtraTags,
4342
}
@@ -85,10 +84,10 @@ func FromEvent(ctx context.Context, db *database.DB, ev *event.Event) (*Object,
8584

8685
newObject.ExtraTags = ev.ExtraTags
8786
newObject.Name = ev.Name
88-
newObject.URL = utils.ToDBString(ev.URL)
87+
newObject.URL = types.MakeString(ev.URL, types.TransformEmptyStringToNull)
8988
if ev.Mute.Valid {
9089
if ev.Mute.Bool {
91-
newObject.MuteReason = utils.ToDBString(ev.MuteReason)
90+
newObject.MuteReason = types.MakeString(ev.MuteReason, types.TransformEmptyStringToNull)
9291
} else {
9392
// The ongoing event unmutes the object, so reset the mute reason to null.
9493
newObject.MuteReason = types.String{}

internal/object/objects_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/icinga/icinga-go-library/types"
77
"github.com/icinga/icinga-notifications/internal/event"
88
"github.com/icinga/icinga-notifications/internal/testutils"
9-
"github.com/icinga/icinga-notifications/internal/utils"
109
"github.com/jmoiron/sqlx"
1110
"github.com/stretchr/testify/assert"
1211
"github.com/stretchr/testify/require"
@@ -19,20 +18,20 @@ func TestRestoreMutedObjects(t *testing.T) {
1918
db := testutils.GetTestDB(ctx, t)
2019

2120
var sourceID int64
22-
err := utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
21+
err := db.ExecTx(ctx, func(ctx context.Context, tx *sqlx.Tx) error {
2322
args := map[string]any{
2423
"type": "notifications",
2524
"name": "Icinga Notifications",
2625
"changed_at": int64(1720702049000),
2726
}
2827
// We can't use config.Source here unfortunately due to cyclic import error!
29-
id, err := utils.InsertAndFetchId(ctx, tx, `INSERT INTO source (type, name, changed_at) VALUES (:type, :name, :changed_at)`, args)
28+
id, err := database.InsertObtainID(ctx, tx, `INSERT INTO source (type, name, changed_at) VALUES (:type, :name, :changed_at)`, args)
3029
require.NoError(t, err, "populating source table should not fail")
3130

3231
sourceID = id
3332
return nil
3433
})
35-
require.NoError(t, err, "utils.RunInTx() should not fail")
34+
require.NoError(t, err, "db.ExecTx should not fail")
3635

3736
ClearCache()
3837

internal/recipient/recipient.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package recipient
33
import (
44
"fmt"
55
"github.com/icinga/icinga-go-library/types"
6-
"github.com/icinga/icinga-notifications/internal/utils"
76
"go.uber.org/zap/zapcore"
87
"time"
98
)
@@ -55,11 +54,11 @@ func (r Key) MarshalText() (text []byte, err error) {
5554
func ToKey(r Recipient) Key {
5655
switch v := r.(type) {
5756
case *Contact:
58-
return Key{ContactID: utils.ToDBInt(v.ID)}
57+
return Key{ContactID: types.MakeInt(v.ID, types.TransformZeroIntToNull)}
5958
case *Group:
60-
return Key{GroupID: utils.ToDBInt(v.ID)}
59+
return Key{GroupID: types.MakeInt(v.ID, types.TransformZeroIntToNull)}
6160
case *Schedule:
62-
return Key{ScheduleID: utils.ToDBInt(v.ID)}
61+
return Key{ScheduleID: types.MakeInt(v.ID, types.TransformZeroIntToNull)}
6362
default:
6463
panic(fmt.Sprintf("unexpected recipient type: %T", r))
6564
}

0 commit comments

Comments
 (0)