league #1

Merged
h merged 41 commits from league into master 2026-02-15 19:59:31 +11:00
4 changed files with 18 additions and 22 deletions
Showing only changes of commit 6d5983c376 - Show all commits

View File

@@ -38,10 +38,7 @@ func (u *User) UpdateDiscordToken(ctx context.Context, tx bun.Tx, token *discord
}
err := Insert(tx, discordToken).
On("CONFLICT (discord_id) DO UPDATE").
Set("access_token = EXCLUDED.access_token").
Set("refresh_token = EXCLUDED.refresh_token").
Set("expires_at = EXCLUDED.expires_at").
ConflictUpdate([]string{"discord_id"}, "access_token", "refresh_token", "expires_at").
Exec(ctx)
if err != nil {
return errors.Wrap(err, "db.Insert")

View File

@@ -2,6 +2,7 @@ package db
import (
"context"
"fmt"
"net/http"
"strings"
@@ -47,17 +48,18 @@ func InsertMultiple[T any](tx bun.Tx, models []*T) *inserter[T] {
}
}
// On adds .On handling for upserts
// Example: .On("(discord_id) DO UPDATE")
func (i *inserter[T]) On(query string) *inserter[T] {
i.q = i.q.On(query)
func (i *inserter[T]) ConflictNothing(conflicts ...string) *inserter[T] {
fieldstr := strings.Join(conflicts, ", ")
i.q = i.q.On(fmt.Sprintf("CONFLICT (%s) DO NOTHING", fieldstr))
return i
}
// Set adds a SET clause for upserts (use with OnConflict)
// Example: .Set("access_token = EXCLUDED.access_token")
func (i *inserter[T]) Set(query string, args ...any) *inserter[T] {
i.q = i.q.Set(query, args...)
func (i *inserter[T]) ConflictUpdate(conflicts []string, columns ...string) *inserter[T] {
fieldstr := strings.Join(conflicts, ", ")
i.q = i.q.On(fmt.Sprintf("CONFLICT (%s) DO UPDATE", fieldstr))
for _, column := range columns {
i.q = i.q.Set(fmt.Sprintf("%s = EXCLUDED.%s", column, column))
}
return i
}

View File

@@ -127,7 +127,7 @@ func AddPermissionToRole(ctx context.Context, tx bun.Tx, roleID, permissionID in
PermissionID: permissionID,
}
err := Insert(tx, rolePerm).
On("CONFLICT (role_id, permission_id) DO NOTHING").
ConflictNothing("role_id", "permission_id").
Exec(ctx)
if err != nil {
return errors.Wrap(err, "db.Insert")
@@ -145,13 +145,12 @@ func RemovePermissionFromRole(ctx context.Context, tx bun.Tx, roleID, permission
return errors.New("permissionID must be positive")
}
_, err := tx.NewDelete().
Model((*RolePermission)(nil)).
err := DeleteItem[RolePermission](tx).
Where("role_id = ?", roleID).
Where("permission_id = ?", permissionID).
Exec(ctx)
Delete(ctx)
if err != nil {
return errors.Wrap(err, "tx.NewDelete")
return errors.Wrap(err, "DeleteItem")
}
return nil

View File

@@ -30,8 +30,7 @@ func AssignRole(ctx context.Context, tx bun.Tx, userID, roleID int) error {
RoleID: roleID,
}
err := Insert(tx, userRole).
On("CONFLICT (user_id, role_id) DO NOTHING").
Exec(ctx)
ConflictNothing("user_id", "role_id").Exec(ctx)
if err != nil {
return errors.Wrap(err, "db.Insert")
}
@@ -48,11 +47,10 @@ func RevokeRole(ctx context.Context, tx bun.Tx, userID, roleID int) error {
return errors.New("roleID must be positive")
}
_, err := tx.NewDelete().
Model((*UserRole)(nil)).
err := DeleteItem[UserRole](tx).
Where("user_id = ?", userID).
Where("role_id = ?", roleID).
Exec(ctx)
Delete(ctx)
if err != nil {
return errors.Wrap(err, "tx.NewDelete")
}