From 7553e1f51e84f6941644d956ae65db3161b92479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 31 May 2024 23:56:16 +0200 Subject: [PATCH] Upgrade goext --- flutter/TODO.md | 15 +- scnserver/api/ginresp/wrapper.go | 13 +- scnserver/db/dbtools/preprocessor.go | 4 +- scnserver/db/impl/logs/database.go | 2 +- scnserver/db/impl/primary/channels.go | 12 +- scnserver/db/impl/primary/clients.go | 4 +- scnserver/db/impl/primary/database.go | 2 +- scnserver/db/impl/primary/deliveries.go | 2 +- scnserver/db/impl/primary/keytokens.go | 6 +- scnserver/db/impl/primary/messages.go | 6 +- scnserver/db/impl/primary/subscriptions.go | 6 +- scnserver/db/impl/primary/users.go | 2 +- scnserver/db/impl/requests/database.go | 4 +- scnserver/db/impl/requests/requestlogs.go | 2 +- scnserver/models/channel.go | 17 +- scnserver/models/client.go | 9 +- scnserver/models/delivery.go | 9 +- scnserver/models/enums_gen.go | 2 +- scnserver/models/ids_gen.go | 2 +- scnserver/models/keytoken.go | 9 +- scnserver/models/message.go | 9 +- scnserver/models/requestlog.go | 9 +- scnserver/models/subscription.go | 9 +- scnserver/models/user.go | 9 +- scnserver/swagger/swagger.json | 250 ++++----------------- scnserver/swagger/swagger.yaml | 245 +++++--------------- scnserver/test/database_test.go | 2 +- scnserver/test/main_test.go | 15 ++ 28 files changed, 210 insertions(+), 466 deletions(-) create mode 100644 scnserver/test/main_test.go diff --git a/flutter/TODO.md b/flutter/TODO.md index 30a3196..c74ee12 100644 --- a/flutter/TODO.md +++ b/flutter/TODO.md @@ -8,18 +8,27 @@ - [ ] Channel List * [ ] Show subs * [ ] CRUD + * [ ] what about unsubbed foreign channels? - thex should still be visible (or should they, do i still get the messages?) - [ ] Sub List * [ ] Sub/Unsub/Accept/Deny - [ ] Debug List (Show logs, requests) - [ ] Key List * [ ] CRUD - - [ ] Auto R-only key for admin, use for QR+link + - [ ] Auto R-only key for admin, use for QR+link+send - [ ] settings - [ ] notifications - [ ] push navigation stack - - [ ] read + migrate old SharedPrefs + - [ ] read + migrate old SharedPrefs (or not? - who uses SCN even??) - [ ] Account-Page - [ ] Logout - [ ] Send-page - \ No newline at end of file + + ----- + + - [ ] Switch server to sq style from faby + - [ ] switch from mattn to go-sqlite + - [ ] Single struct for model/db/json + - [ ] use sq.Query | sq.Update | sq.InsertAndQuery | .... + - [ ] sq.DBOptions - enable CommentTrimmer and DefaultConverter + - [ ] run unit-tests... \ No newline at end of file diff --git a/scnserver/api/ginresp/wrapper.go b/scnserver/api/ginresp/wrapper.go index 41dd9ad..3994a98 100644 --- a/scnserver/api/ginresp/wrapper.go +++ b/scnserver/api/ginresp/wrapper.go @@ -11,6 +11,7 @@ import ( "github.com/rs/zerolog/log" "gogs.mikescher.com/BlackForestBytes/goext/dataext" "gogs.mikescher.com/BlackForestBytes/goext/langext" + "math/rand" "runtime/debug" "time" ) @@ -60,7 +61,7 @@ func Wrap(rlacc RequestLogAcceptor, fn WHandlerFunc) gin.HandlerFunc { panic(err) } - time.Sleep(retrySleep) + time.Sleep(time.Duration(int64(float64(retrySleep) * (0.5 + rand.Float64())))) continue } @@ -174,8 +175,14 @@ func resetBody(g *gin.Context) error { func isSqlite3Busy(r HTTPResponse) bool { if errwrap, ok := r.(*errorHTTPResponse); ok && errwrap != nil { - if s3err, ok := (errwrap.error).(sqlite3.Error); ok { - if s3err.Code == sqlite3.ErrBusy { + + if errors.Is(errwrap.error, sqlite3.ErrBusy) { + return true + } + + var s3err sqlite3.Error + if errors.As(errwrap.error, &s3err) { + if errors.Is(s3err.Code, sqlite3.ErrBusy) { return true } } diff --git a/scnserver/db/dbtools/preprocessor.go b/scnserver/db/dbtools/preprocessor.go index fe56e47..b220519 100644 --- a/scnserver/db/dbtools/preprocessor.go +++ b/scnserver/db/dbtools/preprocessor.go @@ -75,7 +75,7 @@ func (pp *DBPreprocessor) Init(ctx context.Context) error { if err != nil { return err } - resrows1, err := sq.ScanAll[tabInfo](rows1, sq.SModeFast, sq.Unsafe, true) + resrows1, err := sq.ScanAll[tabInfo](ctx, pp.db, rows1, sq.SModeFast, sq.Unsafe, true) if err != nil { return err } @@ -85,7 +85,7 @@ func (pp *DBPreprocessor) Init(ctx context.Context) error { if err != nil { return err } - resrows2, err := sq.ScanAll[colInfo](rows2, sq.SModeFast, sq.Unsafe, true) + resrows2, err := sq.ScanAll[colInfo](ctx, pp.db, rows2, sq.SModeFast, sq.Unsafe, true) if err != nil { return err } diff --git a/scnserver/db/impl/logs/database.go b/scnserver/db/impl/logs/database.go index f99e6b0..0863ec9 100644 --- a/scnserver/db/impl/logs/database.go +++ b/scnserver/db/impl/logs/database.go @@ -42,7 +42,7 @@ func NewLogsDatabase(cfg server.Config) (*Database, error) { xdb.SetConnMaxIdleTime(60 * time.Minute) } - qqdb := sq.NewDB(xdb) + qqdb := sq.NewDB(xdb, sq.DBOptions{}) if conf.EnableLogger { qqdb.AddListener(dbtools.DBLogger{}) diff --git a/scnserver/db/impl/primary/channels.go b/scnserver/db/impl/primary/channels.go index a5c44b2..157c4ec 100644 --- a/scnserver/db/impl/primary/channels.go +++ b/scnserver/db/impl/primary/channels.go @@ -23,7 +23,7 @@ func (db *Database) GetChannelByName(ctx db.TxContext, userid models.UserID, cha return nil, err } - channel, err := models.DecodeChannel(rows) + channel, err := models.DecodeChannel(ctx, tx, rows) if errors.Is(err, sql.ErrNoRows) { return nil, nil } @@ -47,7 +47,7 @@ func (db *Database) GetChannelByID(ctx db.TxContext, chanid models.ChannelID) (* return nil, err } - channel, err := models.DecodeChannel(rows) + channel, err := models.DecodeChannel(ctx, tx, rows) if errors.Is(err, sql.ErrNoRows) { return nil, nil } @@ -108,7 +108,7 @@ func (db *Database) ListChannelsByOwner(ctx db.TxContext, userid models.UserID, return nil, err } - data, err := models.DecodeChannelsWithSubscription(rows) + data, err := models.DecodeChannelsWithSubscription(ctx, tx, rows) if err != nil { return nil, err } @@ -138,7 +138,7 @@ func (db *Database) ListChannelsBySubscriber(ctx db.TxContext, userid models.Use return nil, err } - data, err := models.DecodeChannelsWithSubscription(rows) + data, err := models.DecodeChannelsWithSubscription(ctx, tx, rows) if err != nil { return nil, err } @@ -169,7 +169,7 @@ func (db *Database) ListChannelsByAccess(ctx db.TxContext, userid models.UserID, return nil, err } - data, err := models.DecodeChannelsWithSubscription(rows) + data, err := models.DecodeChannelsWithSubscription(ctx, tx, rows) if err != nil { return nil, err } @@ -203,7 +203,7 @@ func (db *Database) GetChannel(ctx db.TxContext, userid models.UserID, channelid return models.ChannelWithSubscription{}, err } - channel, err := models.DecodeChannelWithSubscription(rows) + channel, err := models.DecodeChannelWithSubscription(ctx, tx, rows) if err != nil { return models.ChannelWithSubscription{}, err } diff --git a/scnserver/db/impl/primary/clients.go b/scnserver/db/impl/primary/clients.go index c300cc4..4c56f2b 100644 --- a/scnserver/db/impl/primary/clients.go +++ b/scnserver/db/impl/primary/clients.go @@ -56,7 +56,7 @@ func (db *Database) ListClients(ctx db.TxContext, userid models.UserID) ([]model return nil, err } - data, err := models.DecodeClients(rows) + data, err := models.DecodeClients(ctx, tx, rows) if err != nil { return nil, err } @@ -78,7 +78,7 @@ func (db *Database) GetClient(ctx db.TxContext, userid models.UserID, clientid m return models.Client{}, err } - client, err := models.DecodeClient(rows) + client, err := models.DecodeClient(ctx, tx, rows) if err != nil { return models.Client{}, err } diff --git a/scnserver/db/impl/primary/database.go b/scnserver/db/impl/primary/database.go index c7c0ffa..237a35d 100644 --- a/scnserver/db/impl/primary/database.go +++ b/scnserver/db/impl/primary/database.go @@ -42,7 +42,7 @@ func NewPrimaryDatabase(cfg server.Config) (*Database, error) { xdb.SetConnMaxIdleTime(60 * time.Minute) } - qqdb := sq.NewDB(xdb) + qqdb := sq.NewDB(xdb, sq.DBOptions{}) if conf.EnableLogger { qqdb.AddListener(dbtools.DBLogger{}) diff --git a/scnserver/db/impl/primary/deliveries.go b/scnserver/db/impl/primary/deliveries.go index 120f8f0..cdb7f55 100644 --- a/scnserver/db/impl/primary/deliveries.go +++ b/scnserver/db/impl/primary/deliveries.go @@ -82,7 +82,7 @@ func (db *Database) ListRetrieableDeliveries(ctx db.TxContext, pageSize int) ([] return nil, err } - data, err := models.DecodeDeliveries(rows) + data, err := models.DecodeDeliveries(ctx, tx, rows) if err != nil { return nil, err } diff --git a/scnserver/db/impl/primary/keytokens.go b/scnserver/db/impl/primary/keytokens.go index 4a8a1f8..a8cd82c 100644 --- a/scnserver/db/impl/primary/keytokens.go +++ b/scnserver/db/impl/primary/keytokens.go @@ -49,7 +49,7 @@ func (db *Database) ListKeyTokens(ctx db.TxContext, ownerID models.UserID) ([]mo return nil, err } - data, err := models.DecodeKeyTokens(rows) + data, err := models.DecodeKeyTokens(ctx, tx, rows) if err != nil { return nil, err } @@ -71,7 +71,7 @@ func (db *Database) GetKeyToken(ctx db.TxContext, userid models.UserID, keyToken return models.KeyToken{}, err } - keyToken, err := models.DecodeKeyToken(rows) + keyToken, err := models.DecodeKeyToken(ctx, tx, rows) if err != nil { return models.KeyToken{}, err } @@ -90,7 +90,7 @@ func (db *Database) GetKeyTokenByToken(ctx db.TxContext, key string) (*models.Ke return nil, err } - user, err := models.DecodeKeyToken(rows) + user, err := models.DecodeKeyToken(ctx, tx, rows) if errors.Is(err, sql.ErrNoRows) { return nil, nil } diff --git a/scnserver/db/impl/primary/messages.go b/scnserver/db/impl/primary/messages.go index 32b71c4..134331c 100644 --- a/scnserver/db/impl/primary/messages.go +++ b/scnserver/db/impl/primary/messages.go @@ -21,7 +21,7 @@ func (db *Database) GetMessageByUserMessageID(ctx db.TxContext, usrMsgId string) return nil, err } - msg, err := models.DecodeMessage(rows) + msg, err := models.DecodeMessage(ctx, tx, rows) if errors.Is(err, sql.ErrNoRows) { return nil, nil } @@ -50,7 +50,7 @@ func (db *Database) GetMessage(ctx db.TxContext, scnMessageID models.MessageID, return models.Message{}, err } - msg, err := models.DecodeMessage(rows) + msg, err := models.DecodeMessage(ctx, tx, rows) if err != nil { return models.Message{}, err } @@ -138,7 +138,7 @@ func (db *Database) ListMessages(ctx db.TxContext, filter models.MessageFilter, return nil, ct.CursorToken{}, err } - data, err := models.DecodeMessages(rows) + data, err := models.DecodeMessages(ctx, tx, rows) if err != nil { return nil, ct.CursorToken{}, err } diff --git a/scnserver/db/impl/primary/subscriptions.go b/scnserver/db/impl/primary/subscriptions.go index d9e7ef2..7f1d521 100644 --- a/scnserver/db/impl/primary/subscriptions.go +++ b/scnserver/db/impl/primary/subscriptions.go @@ -50,7 +50,7 @@ func (db *Database) ListSubscriptions(ctx db.TxContext, filter models.Subscripti return nil, err } - data, err := models.DecodeSubscriptions(rows) + data, err := models.DecodeSubscriptions(ctx, tx, rows) if err != nil { return nil, err } @@ -69,7 +69,7 @@ func (db *Database) GetSubscription(ctx db.TxContext, subid models.SubscriptionI return models.Subscription{}, err } - sub, err := models.DecodeSubscription(rows) + sub, err := models.DecodeSubscription(ctx, tx, rows) if err != nil { return models.Subscription{}, err } @@ -91,7 +91,7 @@ func (db *Database) GetSubscriptionBySubscriber(ctx db.TxContext, subscriberId m return nil, err } - user, err := models.DecodeSubscription(rows) + user, err := models.DecodeSubscription(ctx, tx, rows) if errors.Is(err, sql.ErrNoRows) { return nil, nil } diff --git a/scnserver/db/impl/primary/users.go b/scnserver/db/impl/primary/users.go index 0f5eadd..aeab690 100644 --- a/scnserver/db/impl/primary/users.go +++ b/scnserver/db/impl/primary/users.go @@ -61,7 +61,7 @@ func (db *Database) GetUser(ctx db.TxContext, userid models.UserID) (models.User return models.User{}, err } - user, err := models.DecodeUser(rows) + user, err := models.DecodeUser(ctx, tx, rows) if err != nil { return models.User{}, err } diff --git a/scnserver/db/impl/requests/database.go b/scnserver/db/impl/requests/database.go index 30070b4..283bf5f 100644 --- a/scnserver/db/impl/requests/database.go +++ b/scnserver/db/impl/requests/database.go @@ -42,7 +42,7 @@ func NewRequestsDatabase(cfg server.Config) (*Database, error) { xdb.SetConnMaxIdleTime(60 * time.Minute) } - qqdb := sq.NewDB(xdb) + qqdb := sq.NewDB(xdb, sq.DBOptions{}) if conf.EnableLogger { qqdb.AddListener(dbtools.DBLogger{}) @@ -92,7 +92,7 @@ func (db *Database) Migrate(outerctx context.Context) error { schemastr := schema.RequestsSchema[schema.RequestsSchemaVersion].SQL schemahash := schema.RequestsSchema[schema.RequestsSchemaVersion].Hash - schemahash, err := sq.HashSqliteSchema(tctx, schemastr) + schemahash, err := sq.HashMattnSqliteSchema(tctx, schemastr) if err != nil { return err } diff --git a/scnserver/db/impl/requests/requestlogs.go b/scnserver/db/impl/requests/requestlogs.go index dd94c5f..3bad77f 100644 --- a/scnserver/db/impl/requests/requestlogs.go +++ b/scnserver/db/impl/requests/requestlogs.go @@ -78,7 +78,7 @@ func (db *Database) ListRequestLogs(ctx context.Context, filter models.RequestLo return nil, ct.CursorToken{}, err } - data, err := models.DecodeRequestLogs(rows) + data, err := models.DecodeRequestLogs(ctx, db.db, rows) if err != nil { return nil, ct.CursorToken{}, err } diff --git a/scnserver/models/channel.go b/scnserver/models/channel.go index 91091e2..2b080ac 100644 --- a/scnserver/models/channel.go +++ b/scnserver/models/channel.go @@ -1,6 +1,7 @@ package models import ( + "context" "github.com/jmoiron/sqlx" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/sq" @@ -115,32 +116,32 @@ func (c ChannelWithSubscriptionDB) Model() ChannelWithSubscription { } } -func DecodeChannel(r *sqlx.Rows) (Channel, error) { - data, err := sq.ScanSingle[ChannelDB](r, sq.SModeFast, sq.Safe, true) +func DecodeChannel(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Channel, error) { + data, err := sq.ScanSingle[ChannelDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return Channel{}, err } return data.Model(), nil } -func DecodeChannels(r *sqlx.Rows) ([]Channel, error) { - data, err := sq.ScanAll[ChannelDB](r, sq.SModeFast, sq.Safe, true) +func DecodeChannels(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Channel, error) { + data, err := sq.ScanAll[ChannelDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return nil, err } return langext.ArrMap(data, func(v ChannelDB) Channel { return v.Model() }), nil } -func DecodeChannelWithSubscription(r *sqlx.Rows) (ChannelWithSubscription, error) { - data, err := sq.ScanSingle[ChannelWithSubscriptionDB](r, sq.SModeExtended, sq.Safe, true) +func DecodeChannelWithSubscription(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (ChannelWithSubscription, error) { + data, err := sq.ScanSingle[ChannelWithSubscriptionDB](ctx, q, r, sq.SModeExtended, sq.Safe, true) if err != nil { return ChannelWithSubscription{}, err } return data.Model(), nil } -func DecodeChannelsWithSubscription(r *sqlx.Rows) ([]ChannelWithSubscription, error) { - data, err := sq.ScanAll[ChannelWithSubscriptionDB](r, sq.SModeExtended, sq.Safe, true) +func DecodeChannelsWithSubscription(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]ChannelWithSubscription, error) { + data, err := sq.ScanAll[ChannelWithSubscriptionDB](ctx, q, r, sq.SModeExtended, sq.Safe, true) if err != nil { return nil, err } diff --git a/scnserver/models/client.go b/scnserver/models/client.go index 0040eaf..61d2dfa 100644 --- a/scnserver/models/client.go +++ b/scnserver/models/client.go @@ -1,6 +1,7 @@ package models import ( + "context" "github.com/jmoiron/sqlx" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/sq" @@ -71,16 +72,16 @@ func (c ClientDB) Model() Client { } } -func DecodeClient(r *sqlx.Rows) (Client, error) { - data, err := sq.ScanSingle[ClientDB](r, sq.SModeFast, sq.Safe, true) +func DecodeClient(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Client, error) { + data, err := sq.ScanSingle[ClientDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return Client{}, err } return data.Model(), nil } -func DecodeClients(r *sqlx.Rows) ([]Client, error) { - data, err := sq.ScanAll[ClientDB](r, sq.SModeFast, sq.Safe, true) +func DecodeClients(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Client, error) { + data, err := sq.ScanAll[ClientDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return nil, err } diff --git a/scnserver/models/delivery.go b/scnserver/models/delivery.go index ec4c8e3..8812ebe 100644 --- a/scnserver/models/delivery.go +++ b/scnserver/models/delivery.go @@ -1,6 +1,7 @@ package models import ( + "context" "github.com/jmoiron/sqlx" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/sq" @@ -88,16 +89,16 @@ func (d DeliveryDB) Model() Delivery { } } -func DecodeDelivery(r *sqlx.Rows) (Delivery, error) { - data, err := sq.ScanSingle[DeliveryDB](r, sq.SModeFast, sq.Safe, true) +func DecodeDelivery(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Delivery, error) { + data, err := sq.ScanSingle[DeliveryDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return Delivery{}, err } return data.Model(), nil } -func DecodeDeliveries(r *sqlx.Rows) ([]Delivery, error) { - data, err := sq.ScanAll[DeliveryDB](r, sq.SModeFast, sq.Safe, true) +func DecodeDeliveries(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Delivery, error) { + data, err := sq.ScanAll[DeliveryDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return nil, err } diff --git a/scnserver/models/enums_gen.go b/scnserver/models/enums_gen.go index e6ae732..755fada 100644 --- a/scnserver/models/enums_gen.go +++ b/scnserver/models/enums_gen.go @@ -5,7 +5,7 @@ package models import "gogs.mikescher.com/BlackForestBytes/goext/langext" import "gogs.mikescher.com/BlackForestBytes/goext/enums" -const ChecksumEnumGenerator = "69a99f8c01a15baad2fc9b99c516a700e02ce4dbcae452699e6d43cc23bc01bc" // GoExtVersion: 0.0.463 +const ChecksumEnumGenerator = "20ad4720103f17781ed6aaa102b503abb182ac08c077b42d4f56557ef38f28eb" // GoExtVersion: 0.0.463 // ================================ ClientType ================================ // diff --git a/scnserver/models/ids_gen.go b/scnserver/models/ids_gen.go index 8fd4682..119d58a 100644 --- a/scnserver/models/ids_gen.go +++ b/scnserver/models/ids_gen.go @@ -15,7 +15,7 @@ import "reflect" import "regexp" import "strings" -const ChecksumCharsetIDGenerator = "69a99f8c01a15baad2fc9b99c516a700e02ce4dbcae452699e6d43cc23bc01bc" // GoExtVersion: 0.0.463 +const ChecksumCharsetIDGenerator = "20ad4720103f17781ed6aaa102b503abb182ac08c077b42d4f56557ef38f28eb" // GoExtVersion: 0.0.463 const idlen = 24 diff --git a/scnserver/models/keytoken.go b/scnserver/models/keytoken.go index 6b8b8ca..071234c 100644 --- a/scnserver/models/keytoken.go +++ b/scnserver/models/keytoken.go @@ -1,6 +1,7 @@ package models import ( + "context" "github.com/jmoiron/sqlx" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/sq" @@ -149,16 +150,16 @@ func (k KeyTokenDB) Model() KeyToken { } } -func DecodeKeyToken(r *sqlx.Rows) (KeyToken, error) { - data, err := sq.ScanSingle[KeyTokenDB](r, sq.SModeFast, sq.Safe, true) +func DecodeKeyToken(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (KeyToken, error) { + data, err := sq.ScanSingle[KeyTokenDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return KeyToken{}, err } return data.Model(), nil } -func DecodeKeyTokens(r *sqlx.Rows) ([]KeyToken, error) { - data, err := sq.ScanAll[KeyTokenDB](r, sq.SModeFast, sq.Safe, true) +func DecodeKeyTokens(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]KeyToken, error) { + data, err := sq.ScanAll[KeyTokenDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return nil, err } diff --git a/scnserver/models/message.go b/scnserver/models/message.go index cd36f68..51b1a0b 100644 --- a/scnserver/models/message.go +++ b/scnserver/models/message.go @@ -1,6 +1,7 @@ package models import ( + "context" "github.com/jmoiron/sqlx" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/sq" @@ -145,16 +146,16 @@ func (m MessageDB) Model() Message { } } -func DecodeMessage(r *sqlx.Rows) (Message, error) { - data, err := sq.ScanSingle[MessageDB](r, sq.SModeFast, sq.Safe, true) +func DecodeMessage(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Message, error) { + data, err := sq.ScanSingle[MessageDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return Message{}, err } return data.Model(), nil } -func DecodeMessages(r *sqlx.Rows) ([]Message, error) { - data, err := sq.ScanAll[MessageDB](r, sq.SModeFast, sq.Safe, true) +func DecodeMessages(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Message, error) { + data, err := sq.ScanAll[MessageDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return nil, err } diff --git a/scnserver/models/requestlog.go b/scnserver/models/requestlog.go index c61d8b7..bda1e14 100644 --- a/scnserver/models/requestlog.go +++ b/scnserver/models/requestlog.go @@ -1,6 +1,7 @@ package models import ( + "context" "github.com/jmoiron/sqlx" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/sq" @@ -170,16 +171,16 @@ func (c RequestLogDB) Model() RequestLog { } } -func DecodeRequestLog(r *sqlx.Rows) (RequestLog, error) { - data, err := sq.ScanSingle[RequestLogDB](r, sq.SModeFast, sq.Safe, true) +func DecodeRequestLog(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (RequestLog, error) { + data, err := sq.ScanSingle[RequestLogDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return RequestLog{}, err } return data.Model(), nil } -func DecodeRequestLogs(r *sqlx.Rows) ([]RequestLog, error) { - data, err := sq.ScanAll[RequestLogDB](r, sq.SModeFast, sq.Safe, true) +func DecodeRequestLogs(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]RequestLog, error) { + data, err := sq.ScanAll[RequestLogDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return nil, err } diff --git a/scnserver/models/subscription.go b/scnserver/models/subscription.go index d51cd84..8ef7785 100644 --- a/scnserver/models/subscription.go +++ b/scnserver/models/subscription.go @@ -1,6 +1,7 @@ package models import ( + "context" "github.com/jmoiron/sqlx" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/sq" @@ -68,16 +69,16 @@ func (s SubscriptionDB) Model() Subscription { } } -func DecodeSubscription(r *sqlx.Rows) (Subscription, error) { - data, err := sq.ScanSingle[SubscriptionDB](r, sq.SModeFast, sq.Safe, true) +func DecodeSubscription(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Subscription, error) { + data, err := sq.ScanSingle[SubscriptionDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return Subscription{}, err } return data.Model(), nil } -func DecodeSubscriptions(r *sqlx.Rows) ([]Subscription, error) { - data, err := sq.ScanAll[SubscriptionDB](r, sq.SModeFast, sq.Safe, true) +func DecodeSubscriptions(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Subscription, error) { + data, err := sq.ScanAll[SubscriptionDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return nil, err } diff --git a/scnserver/models/user.go b/scnserver/models/user.go index 0fd7753..6ae5992 100644 --- a/scnserver/models/user.go +++ b/scnserver/models/user.go @@ -2,6 +2,7 @@ package models import ( scn "blackforestbytes.com/simplecloudnotifier" + "context" "github.com/jmoiron/sqlx" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/sq" @@ -171,16 +172,16 @@ func (u UserDB) Model() User { } } -func DecodeUser(r *sqlx.Rows) (User, error) { - data, err := sq.ScanSingle[UserDB](r, sq.SModeFast, sq.Safe, true) +func DecodeUser(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (User, error) { + data, err := sq.ScanSingle[UserDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return User{}, err } return data.Model(), nil } -func DecodeUsers(r *sqlx.Rows) ([]User, error) { - data, err := sq.ScanAll[UserDB](r, sq.SModeFast, sq.Safe, true) +func DecodeUsers(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]User, error) { + data, err := sq.ScanAll[UserDB](ctx, q, r, sq.SModeFast, sq.Safe, true) if err != nil { return nil, err } diff --git a/scnserver/swagger/swagger.json b/scnserver/swagger/swagger.json index 44bdce8..8347912 100644 --- a/scnserver/swagger/swagger.json +++ b/scnserver/swagger/swagger.json @@ -19,63 +19,39 @@ "parameters": [ { "type": "string", - "example": "test", - "name": "channel", - "in": "query" - }, - { - "type": "string", - "example": "This is a message", "name": "content", "in": "query" }, { "type": "string", - "example": "P3TNH8mvv14fm", - "name": "key", - "in": "query" - }, - { - "type": "string", - "example": "db8b0e6a-a08c-4646", "name": "msg_id", "in": "query" }, { - "enum": [ - 0, - 1, - 2 - ], "type": "integer", - "example": 1, "name": "priority", "in": "query" }, - { - "type": "string", - "example": "example-server", - "name": "sender_name", - "in": "query" - }, { "type": "number", - "example": 1669824037, "name": "timestamp", "in": "query" }, { "type": "string", - "example": "Hello World", "name": "title", "in": "query" }, { - "type": "string", - "example": "7725", + "type": "integer", "name": "user_id", "in": "query" }, + { + "type": "string", + "name": "user_key", + "in": "query" + }, { "description": " ", "name": "post_body", @@ -86,62 +62,38 @@ }, { "type": "string", - "example": "test", - "name": "channel", - "in": "formData" - }, - { - "type": "string", - "example": "This is a message", "name": "content", "in": "formData" }, { "type": "string", - "example": "P3TNH8mvv14fm", - "name": "key", - "in": "formData" - }, - { - "type": "string", - "example": "db8b0e6a-a08c-4646", "name": "msg_id", "in": "formData" }, { - "enum": [ - 0, - 1, - 2 - ], "type": "integer", - "example": 1, "name": "priority", "in": "formData" }, - { - "type": "string", - "example": "example-server", - "name": "sender_name", - "in": "formData" - }, { "type": "number", - "example": 1669824037, "name": "timestamp", "in": "formData" }, { "type": "string", - "example": "Hello World", "name": "title", "in": "formData" }, { - "type": "string", - "example": "7725", + "type": "integer", "name": "user_id", "in": "formData" + }, + { + "type": "string", + "name": "user_key", + "in": "formData" } ], "responses": { @@ -2557,63 +2509,39 @@ "parameters": [ { "type": "string", - "example": "test", - "name": "channel", - "in": "query" - }, - { - "type": "string", - "example": "This is a message", "name": "content", "in": "query" }, { "type": "string", - "example": "P3TNH8mvv14fm", - "name": "key", - "in": "query" - }, - { - "type": "string", - "example": "db8b0e6a-a08c-4646", "name": "msg_id", "in": "query" }, { - "enum": [ - 0, - 1, - 2 - ], "type": "integer", - "example": 1, "name": "priority", "in": "query" }, - { - "type": "string", - "example": "example-server", - "name": "sender_name", - "in": "query" - }, { "type": "number", - "example": 1669824037, "name": "timestamp", "in": "query" }, { "type": "string", - "example": "Hello World", "name": "title", "in": "query" }, { - "type": "string", - "example": "7725", + "type": "integer", "name": "user_id", "in": "query" }, + { + "type": "string", + "name": "user_key", + "in": "query" + }, { "description": " ", "name": "post_body", @@ -2624,62 +2552,38 @@ }, { "type": "string", - "example": "test", - "name": "channel", - "in": "formData" - }, - { - "type": "string", - "example": "This is a message", "name": "content", "in": "formData" }, { "type": "string", - "example": "P3TNH8mvv14fm", - "name": "key", - "in": "formData" - }, - { - "type": "string", - "example": "db8b0e6a-a08c-4646", "name": "msg_id", "in": "formData" }, { - "enum": [ - 0, - 1, - 2 - ], "type": "integer", - "example": 1, "name": "priority", "in": "formData" }, - { - "type": "string", - "example": "example-server", - "name": "sender_name", - "in": "formData" - }, { "type": "number", - "example": 1669824037, "name": "timestamp", "in": "formData" }, { "type": "string", - "example": "Hello World", "name": "title", "in": "formData" }, { - "type": "string", - "example": "7725", + "type": "integer", "name": "user_id", "in": "formData" + }, + { + "type": "string", + "name": "user_key", + "in": "formData" } ], "responses": { @@ -2727,121 +2631,73 @@ "parameters": [ { "type": "string", - "example": "test", - "name": "channel", - "in": "query" - }, - { - "type": "string", - "example": "This is a message", "name": "content", "in": "query" }, { "type": "string", - "example": "P3TNH8mvv14fm", - "name": "key", - "in": "query" - }, - { - "type": "string", - "example": "db8b0e6a-a08c-4646", "name": "msg_id", "in": "query" }, { - "enum": [ - 0, - 1, - 2 - ], "type": "integer", - "example": 1, "name": "priority", "in": "query" }, - { - "type": "string", - "example": "example-server", - "name": "sender_name", - "in": "query" - }, { "type": "number", - "example": 1669824037, "name": "timestamp", "in": "query" }, { "type": "string", - "example": "Hello World", "name": "title", "in": "query" }, { - "type": "string", - "example": "7725", + "type": "integer", "name": "user_id", "in": "query" }, { "type": "string", - "example": "test", - "name": "channel", - "in": "formData" + "name": "user_key", + "in": "query" }, { "type": "string", - "example": "This is a message", "name": "content", "in": "formData" }, { "type": "string", - "example": "P3TNH8mvv14fm", - "name": "key", - "in": "formData" - }, - { - "type": "string", - "example": "db8b0e6a-a08c-4646", "name": "msg_id", "in": "formData" }, { - "enum": [ - 0, - 1, - 2 - ], "type": "integer", - "example": 1, "name": "priority", "in": "formData" }, - { - "type": "string", - "example": "example-server", - "name": "sender_name", - "in": "formData" - }, { "type": "number", - "example": 1669824037, "name": "timestamp", "in": "formData" }, { "type": "string", - "example": "Hello World", "name": "title", "in": "formData" }, { - "type": "string", - "example": "7725", + "type": "integer", "name": "user_id", "in": "formData" + }, + { + "type": "string", + "name": "user_key", + "in": "formData" } ], "responses": { @@ -3331,46 +3187,26 @@ "handler.SendMessage.combined": { "type": "object", "properties": { - "channel": { - "type": "string", - "example": "test" - }, "content": { - "type": "string", - "example": "This is a message" - }, - "key": { - "type": "string", - "example": "P3TNH8mvv14fm" + "type": "string" }, "msg_id": { - "type": "string", - "example": "db8b0e6a-a08c-4646" + "type": "string" }, "priority": { - "type": "integer", - "enum": [ - 0, - 1, - 2 - ], - "example": 1 - }, - "sender_name": { - "type": "string", - "example": "example-server" + "type": "integer" }, "timestamp": { - "type": "number", - "example": 1669824037 + "type": "number" }, "title": { - "type": "string", - "example": "Hello World" + "type": "string" }, "user_id": { - "type": "string", - "example": "7725" + "type": "integer" + }, + "user_key": { + "type": "string" } } }, @@ -3399,7 +3235,7 @@ "type": "integer" }, "scn_msg_id": { - "type": "string" + "type": "integer" }, "success": { "type": "boolean" diff --git a/scnserver/swagger/swagger.yaml b/scnserver/swagger/swagger.yaml index a4f0f08..987e65f 100644 --- a/scnserver/swagger/swagger.yaml +++ b/scnserver/swagger/swagger.yaml @@ -323,36 +323,19 @@ definitions: type: object handler.SendMessage.combined: properties: - channel: - example: test - type: string content: - example: This is a message - type: string - key: - example: P3TNH8mvv14fm type: string msg_id: - example: db8b0e6a-a08c-4646 type: string priority: - enum: - - 0 - - 1 - - 2 - example: 1 type: integer - sender_name: - example: example-server - type: string timestamp: - example: 1669824037 type: number title: - example: Hello World type: string user_id: - example: "7725" + type: integer + user_key: type: string type: object handler.SendMessage.response: @@ -372,7 +355,7 @@ definitions: quota_max: type: integer scn_msg_id: - type: string + type: integer success: type: boolean suppress_send: @@ -734,90 +717,52 @@ paths: description: All parameter can be set via query-parameter or the json body. Only UserID, UserKey and Title are required parameters: - - example: test - in: query - name: channel - type: string - - example: This is a message - in: query + - in: query name: content type: string - - example: P3TNH8mvv14fm - in: query - name: key - type: string - - example: db8b0e6a-a08c-4646 - in: query + - in: query name: msg_id type: string - - enum: - - 0 - - 1 - - 2 - example: 1 - in: query + - in: query name: priority type: integer - - example: example-server - in: query - name: sender_name - type: string - - example: 1669824037 - in: query + - in: query name: timestamp type: number - - example: Hello World - in: query + - in: query name: title type: string - - example: "7725" - in: query + - in: query name: user_id + type: integer + - in: query + name: user_key type: string - description: ' ' in: body name: post_body schema: $ref: '#/definitions/handler.SendMessage.combined' - - example: test - in: formData - name: channel - type: string - - example: This is a message - in: formData + - in: formData name: content type: string - - example: P3TNH8mvv14fm - in: formData - name: key - type: string - - example: db8b0e6a-a08c-4646 - in: formData + - in: formData name: msg_id type: string - - enum: - - 0 - - 1 - - 2 - example: 1 - in: formData + - in: formData name: priority type: integer - - example: example-server - in: formData - name: sender_name - type: string - - example: 1669824037 - in: formData + - in: formData name: timestamp type: number - - example: Hello World - in: formData + - in: formData name: title type: string - - example: "7725" - in: formData + - in: formData name: user_id + type: integer + - in: formData + name: user_key type: string responses: "200": @@ -2475,90 +2420,52 @@ paths: description: All parameter can be set via query-parameter or the json body. Only UserID, UserKey and Title are required parameters: - - example: test - in: query - name: channel - type: string - - example: This is a message - in: query + - in: query name: content type: string - - example: P3TNH8mvv14fm - in: query - name: key - type: string - - example: db8b0e6a-a08c-4646 - in: query + - in: query name: msg_id type: string - - enum: - - 0 - - 1 - - 2 - example: 1 - in: query + - in: query name: priority type: integer - - example: example-server - in: query - name: sender_name - type: string - - example: 1669824037 - in: query + - in: query name: timestamp type: number - - example: Hello World - in: query + - in: query name: title type: string - - example: "7725" - in: query + - in: query name: user_id + type: integer + - in: query + name: user_key type: string - description: ' ' in: body name: post_body schema: $ref: '#/definitions/handler.SendMessage.combined' - - example: test - in: formData - name: channel - type: string - - example: This is a message - in: formData + - in: formData name: content type: string - - example: P3TNH8mvv14fm - in: formData - name: key - type: string - - example: db8b0e6a-a08c-4646 - in: formData + - in: formData name: msg_id type: string - - enum: - - 0 - - 1 - - 2 - example: 1 - in: formData + - in: formData name: priority type: integer - - example: example-server - in: formData - name: sender_name - type: string - - example: 1669824037 - in: formData + - in: formData name: timestamp type: number - - example: Hello World - in: formData + - in: formData name: title type: string - - example: "7725" - in: formData + - in: formData name: user_id + type: integer + - in: formData + name: user_key type: string responses: "200": @@ -2591,85 +2498,47 @@ paths: description: All parameter can be set via query-parameter or form-data body. Only UserID, UserKey and Title are required parameters: - - example: test - in: query - name: channel - type: string - - example: This is a message - in: query + - in: query name: content type: string - - example: P3TNH8mvv14fm - in: query - name: key - type: string - - example: db8b0e6a-a08c-4646 - in: query + - in: query name: msg_id type: string - - enum: - - 0 - - 1 - - 2 - example: 1 - in: query + - in: query name: priority type: integer - - example: example-server - in: query - name: sender_name - type: string - - example: 1669824037 - in: query + - in: query name: timestamp type: number - - example: Hello World - in: query + - in: query name: title type: string - - example: "7725" - in: query + - in: query name: user_id + type: integer + - in: query + name: user_key type: string - - example: test - in: formData - name: channel - type: string - - example: This is a message - in: formData + - in: formData name: content type: string - - example: P3TNH8mvv14fm - in: formData - name: key - type: string - - example: db8b0e6a-a08c-4646 - in: formData + - in: formData name: msg_id type: string - - enum: - - 0 - - 1 - - 2 - example: 1 - in: formData + - in: formData name: priority type: integer - - example: example-server - in: formData - name: sender_name - type: string - - example: 1669824037 - in: formData + - in: formData name: timestamp type: number - - example: Hello World - in: formData + - in: formData name: title type: string - - example: "7725" - in: formData + - in: formData name: user_id + type: integer + - in: formData + name: user_key type: string responses: "200": diff --git a/scnserver/test/database_test.go b/scnserver/test/database_test.go index 1be1884..882bf07 100644 --- a/scnserver/test/database_test.go +++ b/scnserver/test/database_test.go @@ -310,7 +310,7 @@ func TestPrimaryDB_Migrate_from_3(t *testing.T) { xdb, err := sqlx.Open("sqlite3", url) tt.TestFailIfErr(t, err) - qqdb := sq.NewDB(xdb) + qqdb := sq.NewDB(xdb, sq.DBOptions{}) schemavers := 3 diff --git a/scnserver/test/main_test.go b/scnserver/test/main_test.go new file mode 100644 index 0000000..864b9fc --- /dev/null +++ b/scnserver/test/main_test.go @@ -0,0 +1,15 @@ +package test + +import ( + "gogs.mikescher.com/BlackForestBytes/goext/exerr" + "gogs.mikescher.com/BlackForestBytes/goext/langext" + "os" + "testing" +) + +func TestMain(m *testing.M) { + if !exerr.Initialized() { + exerr.Init(exerr.ErrorPackageConfigInit{ZeroLogErrTraces: langext.PFalse, ZeroLogAllTraces: langext.PFalse}) + } + os.Exit(m.Run()) +}