Switched to purer libraries

This commit is contained in:
Magnus Åhall 2026-08-19 17:53:52 +02:00
parent 8fa8bb4c3a
commit 9496947f01
21 changed files with 390 additions and 316 deletions

View file

@ -2,14 +2,16 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
"github.com/lib/pq"
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5/pgconn"
// Internal
"smon/notification"
// Standard
"context"
"encoding/json"
"errors"
)
type DbNotificationService struct {
@ -19,9 +21,11 @@ type DbNotificationService struct {
Prio int
}
func InitNotificationManager() (nm notification.Manager, err error) { // {{{
func initNotificationManager() (nm notification.Manager, err error) { // {{{
var dbServices []DbNotificationService
row := service.Db.Conn.QueryRow(`
row := db.QueryRow(
context.Background(),
`
WITH services AS (
SELECT
id,
@ -36,6 +40,7 @@ func InitNotificationManager() (nm notification.Manager, err error) { // {{{
FROM services s
`,
)
var dbData []byte
err = row.Scan(&dbData)
if err != nil {
@ -70,7 +75,8 @@ func InitNotificationManager() (nm notification.Manager, err error) { // {{{
} // }}}
func UpdateNotificationService(svc notification.Service) (created bool, err error) { // {{{
if svc.Exists() {
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
UPDATE public.notification
SET
@ -84,11 +90,11 @@ func UpdateNotificationService(svc notification.Service) (created bool, err erro
svc.Updated().JSON(),
)
} else {
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
INSERT INTO public.notification(prio, configuration, service)
VALUES($1, $2, $3)
`,
svc.Updated().GetPrio(),
svc.Updated().JSON(),
@ -99,8 +105,8 @@ func UpdateNotificationService(svc notification.Service) (created bool, err erro
if err != nil {
// Check if this is just a duplicated prio, which isn't allowed.
pgErr, isPgErr := err.(*pq.Error)
if isPgErr && pgErr.Code == "23505" {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr); pgErr.Code == "23505" {
return false, werr.New("Prio %d is already used by another service", svc.Updated().GetPrio())
}
@ -117,12 +123,12 @@ func UpdateNotificationService(svc notification.Service) (created bool, err erro
return
} // }}}
func DeleteNotificationService(prio int) (err error) { // {{{
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
DELETE FROM public.notification
WHERE
prio = $1
`,
prio,
)
@ -135,7 +141,7 @@ func DeleteNotificationService(prio int) (err error) { // {{{
func AcknowledgeNotification(uuid string) (err error) { // {{{
/*
_, err = service.Db.Conn.Exec(`UPDATE schedule SET acknowledged=true WHERE schedule_uuid=$1`, uuid)
_, err = db.Exec(context.Background(), `UPDATE schedule SET acknowledged=true WHERE schedule_uuid=$1`, uuid)
if err != nil {
err = werr.Wrap(err).WithData(uuid)
}