Use sq.InsertSingle to insert entities

This commit is contained in:
Mike Schwörer 2023-05-28 22:27:38 +02:00
parent e9b4db0f1c
commit 3a9b15c2be
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
11 changed files with 114 additions and 242 deletions

View File

@ -59,6 +59,8 @@
- switch send script everywhere (we can use the new server, but we need to send correct channels)
- do i need bool2db()? it seems to work for keytokens without them?
#### UNSURE
- (?) default-priority for channels
@ -79,7 +81,10 @@
- Pagination for ListChannels / ListSubscriptions / ListClients / ListChannelSubscriptions / ListUserSubscriptions
- Add .Insert() function to sq.DB interface (auto generate insert for an object based on struct keys)
- Use only single struct for DB|Model|JSON
* needs sq.Converter implementation
* needs to handle joined data
* rfctime.Time...
- cannot open sqlite in dbbrowsr (cannot parse schema?)
-> https://github.com/sqlitebrowser/sqlitebrowser/issues/292 -> https://github.com/sqlitebrowser/sqlitebrowser/issues/29266

View File

@ -62,33 +62,23 @@ func (db *Database) CreateChannel(ctx TxContext, userid models.UserID, dispName
return models.Channel{}, err
}
now := time.Now().UTC()
channelid := models.NewChannelID()
_, err = tx.Exec(ctx, "INSERT INTO channels (channel_id, owner_user_id, display_name, internal_name, description_name, subscribe_key, timestamp_created) VALUES (:cid, :ouid, :dnam, :inam, :hnam, :subkey, :ts)", sq.PP{
"cid": channelid,
"ouid": userid,
"dnam": dispName,
"inam": intName,
"hnam": nil,
"subkey": subscribeKey,
"ts": time2DB(now),
})
if err != nil {
return models.Channel{}, err
}
return models.Channel{
ChannelID: channelid,
entity := models.ChannelDB{
ChannelID: models.NewChannelID(),
OwnerUserID: userid,
DisplayName: dispName,
InternalName: intName,
SubscribeKey: subscribeKey,
TimestampCreated: now,
TimestampCreated: time2DB(time.Now()),
TimestampLastSent: nil,
MessagesSent: 0,
}, nil
}
_, err = sq.InsertSingle(ctx, tx, "channels", entity)
if err != nil {
return models.Channel{}, err
}
return entity.Model(), nil
}
func (db *Database) ListChannelsByOwner(ctx TxContext, userid models.UserID, subUserID models.UserID) ([]models.ChannelWithSubscription, error) {

View File

@ -13,32 +13,22 @@ func (db *Database) CreateClient(ctx TxContext, userid models.UserID, ctype mode
return models.Client{}, err
}
now := time.Now().UTC()
entity := models.ClientDB{
ClientID: models.NewClientID(),
UserID: userid,
Type: ctype,
FCMToken: langext.Ptr(fcmToken),
TimestampCreated: time2DB(time.Now()),
AgentModel: agentModel,
AgentVersion: agentVersion,
}
clientid := models.NewClientID()
_, err = tx.Exec(ctx, "INSERT INTO clients (client_id, user_id, type, fcm_token, timestamp_created, agent_model, agent_version) VALUES (:cid, :uid, :typ, :fcm, :ts, :am, :av)", sq.PP{
"cid": clientid,
"uid": userid,
"typ": string(ctype),
"fcm": fcmToken,
"ts": time2DB(now),
"am": agentModel,
"av": agentVersion,
})
_, err = sq.InsertSingle(ctx, tx, "clients", entity)
if err != nil {
return models.Client{}, err
}
return models.Client{
ClientID: clientid,
UserID: userid,
Type: ctype,
FCMToken: langext.Ptr(fcmToken),
TimestampCreated: now,
AgentModel: agentModel,
AgentVersion: agentVersion,
}, nil
return entity.Model(), nil
}
func (db *Database) ClearFCMTokens(ctx TxContext, fcmtoken string) error {

View File

@ -14,38 +14,28 @@ func (db *Database) CreateRetryDelivery(ctx TxContext, client models.Client, msg
return models.Delivery{}, err
}
now := time.Now().UTC()
now := time.Now()
next := scn.NextDeliveryTimestamp(now)
deliveryid := models.NewDeliveryID()
entity := models.DeliveryDB{
DeliveryID: models.NewDeliveryID(),
MessageID: msg.MessageID,
ReceiverUserID: client.UserID,
ReceiverClientID: client.ClientID,
TimestampCreated: time2DB(now),
TimestampFinalized: nil,
Status: models.DeliveryStatusRetry,
RetryCount: 0,
NextDelivery: langext.Ptr(time2DB(next)),
FCMMessageID: nil,
}
_, err = tx.Exec(ctx, "INSERT INTO deliveries (delivery_id, message_id, receiver_user_id, receiver_client_id, timestamp_created, timestamp_finalized, status, fcm_message_id, next_delivery) VALUES (:did, :mid, :ruid, :rcid, :tsc, :tsf, :stat, :fcm, :next)", sq.PP{
"did": deliveryid,
"mid": msg.MessageID,
"ruid": client.UserID,
"rcid": client.ClientID,
"tsc": time2DB(now),
"tsf": nil,
"stat": models.DeliveryStatusRetry,
"fcm": nil,
"next": time2DB(next),
})
_, err = sq.InsertSingle(ctx, tx, "deliveries", entity)
if err != nil {
return models.Delivery{}, err
}
return models.Delivery{
DeliveryID: deliveryid,
MessageID: msg.MessageID,
ReceiverUserID: client.UserID,
ReceiverClientID: client.ClientID,
TimestampCreated: now,
TimestampFinalized: nil,
Status: models.DeliveryStatusRetry,
RetryCount: 0,
NextDelivery: langext.Ptr(next),
FCMMessageID: nil,
}, nil
return entity.Model(), nil
}
func (db *Database) CreateSuccessDelivery(ctx TxContext, client models.Client, msg models.Message, fcmDelivID string) (models.Delivery, error) {
@ -54,37 +44,27 @@ func (db *Database) CreateSuccessDelivery(ctx TxContext, client models.Client, m
return models.Delivery{}, err
}
now := time.Now().UTC()
now := time.Now()
deliveryid := models.NewDeliveryID()
_, err = tx.Exec(ctx, "INSERT INTO deliveries (delivery_id, message_id, receiver_user_id, receiver_client_id, timestamp_created, timestamp_finalized, status, fcm_message_id, next_delivery) VALUES (:did, :mid, :ruid, :rcid, :tsc, :tsf, :stat, :fcm, :next)", sq.PP{
"did": deliveryid,
"mid": msg.MessageID,
"ruid": client.UserID,
"rcid": client.ClientID,
"tsc": time2DB(now),
"tsf": time2DB(now),
"stat": models.DeliveryStatusSuccess,
"fcm": fcmDelivID,
"next": nil,
})
if err != nil {
return models.Delivery{}, err
}
return models.Delivery{
DeliveryID: deliveryid,
entity := models.DeliveryDB{
DeliveryID: models.NewDeliveryID(),
MessageID: msg.MessageID,
ReceiverUserID: client.UserID,
ReceiverClientID: client.ClientID,
TimestampCreated: now,
TimestampFinalized: langext.Ptr(now),
TimestampCreated: time2DB(now),
TimestampFinalized: langext.Ptr(time2DB(now)),
Status: models.DeliveryStatusSuccess,
RetryCount: 0,
NextDelivery: nil,
FCMMessageID: langext.Ptr(fcmDelivID),
}, nil
}
_, err = sq.InsertSingle(ctx, tx, "deliveries", entity)
if err != nil {
return models.Delivery{}, err
}
return entity.Model(), nil
}
func (db *Database) ListRetrieableDeliveries(ctx TxContext, pageSize int) ([]models.Delivery, error) {

View File

@ -15,36 +15,25 @@ func (db *Database) CreateKeyToken(ctx TxContext, name string, owner models.User
return models.KeyToken{}, err
}
now := time.Now().UTC()
entity := models.KeyTokenDB{
KeyTokenID: models.NewKeyTokenID(),
Name: name,
TimestampCreated: time2DB(time.Now()),
TimestampLastUsed: nil,
OwnerUserID: owner,
AllChannels: allChannels,
Channels: strings.Join(langext.ArrMap(channels, func(v models.ChannelID) string { return v.String() }), ";"),
Token: token,
Permissions: permissions.String(),
MessagesSent: 0,
}
keyTokenid := models.NewKeyTokenID()
_, err = tx.Exec(ctx, "INSERT INTO keytokens (keytoken_id, name, timestamp_created, owner_user_id, all_channels, channels, token, permissions) VALUES (:tid, :nam, :tsc, :owr, :all, :cha, :tok, :prm)", sq.PP{
"tid": keyTokenid,
"nam": name,
"tsc": time2DB(now),
"owr": owner.String(),
"all": bool2DB(allChannels),
"cha": strings.Join(langext.ArrMap(channels, func(v models.ChannelID) string { return v.String() }), ";"),
"tok": token,
"prm": permissions.String(),
})
_, err = sq.InsertSingle(ctx, tx, "keytokens", entity)
if err != nil {
return models.KeyToken{}, err
}
return models.KeyToken{
KeyTokenID: keyTokenid,
Name: name,
TimestampCreated: now,
TimestampLastUsed: nil,
OwnerUserID: owner,
AllChannels: allChannels,
Channels: channels,
Token: token,
Permissions: permissions,
MessagesSent: 0,
}, nil
return entity.Model(), nil
}
func (db *Database) ListKeyTokens(ctx TxContext, ownerID models.UserID) ([]models.KeyToken, error) {

View File

@ -62,46 +62,30 @@ func (db *Database) CreateMessage(ctx TxContext, senderUserID models.UserID, cha
return models.Message{}, err
}
now := time.Now().UTC()
messageid := models.NewMessageID()
_, err = tx.Exec(ctx, "INSERT INTO messages (message_id, sender_user_id, owner_user_id, channel_internal_name, channel_id, timestamp_real, timestamp_client, title, content, priority, usr_message_id, sender_ip, sender_name, used_key_id) VALUES (:mid, :suid, :ouid, :cnam, :cid, :tsr, :tsc, :tit, :cnt, :prio, :umid, :ip, :snam, :uk)", sq.PP{
"mid": messageid,
"suid": senderUserID,
"ouid": channel.OwnerUserID,
"cnam": channel.InternalName,
"cid": channel.ChannelID,
"tsr": time2DB(now),
"tsc": time2DBOpt(timestampSend),
"tit": title,
"cnt": content,
"prio": priority,
"umid": userMsgId,
"ip": senderIP,
"snam": senderName,
"uk": usedKeyID,
})
if err != nil {
return models.Message{}, err
}
return models.Message{
MessageID: messageid,
entity := models.MessageDB{
MessageID: models.NewMessageID(),
SenderUserID: senderUserID,
OwnerUserID: channel.OwnerUserID,
ChannelInternalName: channel.InternalName,
ChannelID: channel.ChannelID,
SenderIP: senderIP,
SenderName: senderName,
TimestampReal: now,
TimestampClient: timestampSend,
TimestampReal: time2DB(time.Now()),
TimestampClient: time2DBOpt(timestampSend),
Title: title,
Content: content,
Priority: priority,
UserMessageID: userMsgId,
UsedKeyID: usedKeyID,
}, nil
Deleted: bool2DB(false),
}
_, err = sq.InsertSingle(ctx, tx, "messages", entity)
if err != nil {
return models.Message{}, err
}
return entity.Model(), nil
}
func (db *Database) DeleteMessage(ctx TxContext, messageID models.MessageID) error {

View File

@ -13,32 +13,22 @@ func (db *Database) CreateSubscription(ctx TxContext, subscriberUID models.UserI
return models.Subscription{}, err
}
now := time.Now().UTC()
subscriptionid := models.NewSubscriptionID()
_, err = tx.Exec(ctx, "INSERT INTO subscriptions (subscription_id, subscriber_user_id, channel_owner_user_id, channel_internal_name, channel_id, timestamp_created, confirmed) VALUES (:sid, :suid, :ouid, :cnam, :cid, :ts, :conf)", sq.PP{
"sid": subscriptionid,
"suid": subscriberUID,
"ouid": channel.OwnerUserID,
"cnam": channel.InternalName,
"cid": channel.ChannelID,
"ts": time2DB(now),
"conf": confirmed,
})
if err != nil {
return models.Subscription{}, err
}
return models.Subscription{
SubscriptionID: subscriptionid,
entity := models.SubscriptionDB{
SubscriptionID: models.NewSubscriptionID(),
SubscriberUserID: subscriberUID,
ChannelOwnerUserID: channel.OwnerUserID,
ChannelID: channel.ChannelID,
ChannelInternalName: channel.InternalName,
TimestampCreated: now,
Confirmed: confirmed,
}, nil
TimestampCreated: time2DB(time.Now()),
Confirmed: bool2DB(confirmed),
}
_, err = sq.InsertSingle(ctx, tx, "subscriptions", entity)
if err != nil {
return models.Subscription{}, err
}
return entity.Model(), nil
}
func (db *Database) ListSubscriptionsByChannel(ctx TxContext, channelID models.ChannelID) ([]models.Subscription, error) {

View File

@ -13,25 +13,10 @@ func (db *Database) CreateUser(ctx TxContext, protoken *string, username *string
return models.User{}, err
}
now := time.Now().UTC()
userid := models.NewUserID()
_, err = tx.Exec(ctx, "INSERT INTO users (user_id, username, is_pro, pro_token, timestamp_created) VALUES (:uid, :un, :pro, :tok, :ts)", sq.PP{
"uid": userid,
"un": username,
"pro": bool2DB(protoken != nil),
"tok": protoken,
"ts": time2DB(now),
})
if err != nil {
return models.User{}, err
}
return models.User{
UserID: userid,
entity := models.UserDB{
UserID: models.NewUserID(),
Username: username,
TimestampCreated: now,
TimestampCreated: time2DB(time.Now()),
TimestampLastRead: nil,
TimestampLastSent: nil,
MessagesSent: 0,
@ -39,7 +24,14 @@ func (db *Database) CreateUser(ctx TxContext, protoken *string, username *string
QuotaUsedDay: nil,
IsPro: protoken != nil,
ProToken: protoken,
}, nil
}
_, err = sq.InsertSingle(ctx, tx, "users", entity)
if err != nil {
return models.User{}, err
}
return entity.Model(), nil
}
func (db *Database) ClearProTokens(ctx TxContext, protoken string) error {

View File

@ -8,64 +8,18 @@ import (
"time"
)
func (db *Database) InsertRequestLog(ctx context.Context, requestid models.RequestID, data models.RequestLogDB) (models.RequestLogDB, error) {
func (db *Database) InsertRequestLog(ctx context.Context, requestid models.RequestID, data models.RequestLog) (models.RequestLog, error) {
now := time.Now()
entity := data.DB()
entity.RequestID = requestid
entity.TimestampCreated = time2DB(time.Now())
_, err := db.db.Exec(ctx, "INSERT INTO requests (request_id, method, uri, user_agent, authentication, request_body, request_body_size, request_content_type, remote_ip, userid, permissions, response_statuscode, response_body_size, response_body, response_content_type, retry_count, panicked, panic_str, processing_time, timestamp_created, timestamp_start, timestamp_finish, key_id) VALUES (:request_id, :method, :uri, :user_agent, :authentication, :request_body, :request_body_size, :request_content_type, :remote_ip, :userid, :permissions, :response_statuscode, :response_body_size, :response_body, :response_content_type, :retry_count, :panicked, :panic_str, :processing_time, :timestamp_created, :timestamp_start, :timestamp_finish, :kid)", sq.PP{
"request_id": requestid,
"method": data.Method,
"uri": data.URI,
"user_agent": data.UserAgent,
"authentication": data.Authentication,
"request_body": data.RequestBody,
"request_body_size": data.RequestBodySize,
"request_content_type": data.RequestContentType,
"remote_ip": data.RemoteIP,
"userid": data.UserID,
"permissions": data.Permissions,
"response_statuscode": data.ResponseStatuscode,
"response_body_size": data.ResponseBodySize,
"response_body": data.ResponseBody,
"response_content_type": data.ResponseContentType,
"retry_count": data.RetryCount,
"panicked": data.Panicked,
"panic_str": data.PanicStr,
"processing_time": data.ProcessingTime,
"timestamp_created": now.UnixMilli(),
"timestamp_start": data.TimestampStart,
"timestamp_finish": data.TimestampFinish,
"kid": data.KeyID,
})
_, err := sq.InsertSingle(ctx, db.db, "requests", entity)
if err != nil {
return models.RequestLogDB{}, err
return models.RequestLog{}, err
}
return models.RequestLogDB{
RequestID: requestid,
Method: data.Method,
URI: data.URI,
UserAgent: data.UserAgent,
Authentication: data.Authentication,
RequestBody: data.RequestBody,
RequestBodySize: data.RequestBodySize,
RequestContentType: data.RequestContentType,
RemoteIP: data.RemoteIP,
UserID: data.UserID,
Permissions: data.Permissions,
ResponseStatuscode: data.ResponseStatuscode,
ResponseBodySize: data.ResponseBodySize,
ResponseBody: data.ResponseBody,
ResponseContentType: data.ResponseContentType,
RetryCount: data.RetryCount,
Panicked: data.Panicked,
PanicStr: data.PanicStr,
ProcessingTime: data.ProcessingTime,
TimestampCreated: now.UnixMilli(),
TimestampStart: data.TimestampStart,
TimestampFinish: data.TimestampFinish,
KeyID: data.KeyID,
}, nil
return entity.Model(), nil
}
func (db *Database) Cleanup(ctx context.Context, count int, duration time.Duration) (int64, error) {

View File

@ -94,7 +94,7 @@ func (j *RequestLogCollectorJob) insertLog(requestid models.RequestID, rl models
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := j.app.Database.Requests.InsertRequestLog(ctx, requestid, rl.DB())
_, err := j.app.Database.Requests.InsertRequestLog(ctx, requestid, rl)
if err != nil {
return err
}

View File

@ -80,9 +80,7 @@ type ChannelDB struct {
DisplayName string `db:"display_name"`
DescriptionName *string `db:"description_name"`
SubscribeKey string `db:"subscribe_key"`
SendKey string `db:"send_key"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampLastRead *int64 `db:"timestamp_lastread"`
TimestampLastSent *int64 `db:"timestamp_lastsent"`
MessagesSent int `db:"messages_sent"`
}