diff options
author | 2022-10-08 13:50:48 +0200 | |
---|---|---|
committer | 2022-10-08 13:50:48 +0200 | |
commit | aa07750bdb4dacdb1be39d765114915bba3fc29f (patch) | |
tree | 30e9e5052f607f8c8e4f7d518559df8706275e0f /internal/db/bundb/session.go | |
parent | [performance] cache domains after max retries in transport (#884) (diff) | |
download | gotosocial-aa07750bdb4dacdb1be39d765114915bba3fc29f.tar.xz |
[chore] Standardize database queries, use `bun.Ident()` properly (#886)
* use bun.Ident for user queries
* use bun.Ident for account queries
* use bun.Ident for media queries
* add DeleteAccount func
* remove CaseInsensitive in Where+use Ident ipv Safe
* update admin db
* update domain, use ident
* update emoji, use ident
* update instance queries, use bun.Ident
* fix media
* update mentions, use bun ident
* update relationship + tests
* use tableexpr
* add test follows to bun db test suite
* update notifications
* updatebyprimarykey => updatebyid
* fix session
* prefer explicit ID to pk
* fix little fucky wucky
* remove workaround
* use proper db func for attachment selection
* update status db
* add m2m entries in test rig
* fix up timeline
* go fmt
* fix status put issue
* update GetAccountStatuses
Diffstat (limited to 'internal/db/bundb/session.go')
-rw-r--r-- | internal/db/bundb/session.go | 33 |
1 files changed, 12 insertions, 21 deletions
diff --git a/internal/db/bundb/session.go b/internal/db/bundb/session.go index 9138072e1..b9e70a89f 100644 --- a/internal/db/bundb/session.go +++ b/internal/db/bundb/session.go @@ -21,7 +21,6 @@ package bundb import ( "context" "crypto/rand" - "errors" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" @@ -35,29 +34,22 @@ type sessionDB struct { func (s *sessionDB) GetSession(ctx context.Context) (*gtsmodel.RouterSession, db.Error) { rss := make([]*gtsmodel.RouterSession, 0, 1) - _, err := s.conn. + // get the first router session in the db or... + if err := s.conn. NewSelect(). Model(&rss). Limit(1). - Order("id DESC"). - Exec(ctx) - if err != nil { + Order("router_session.id DESC"). + Scan(ctx); err != nil { return nil, s.conn.ProcessError(err) } + // ... create a new one if len(rss) == 0 { - // no session created yet, so make one return s.createSession(ctx) } - if len(rss) != 1 { - // we asked for 1 so we should get 1 - return nil, errors.New("more than 1 router session was returned") - } - - // return the one session found - rs := rss[0] - return rs, nil + return rss[0], nil } func (s *sessionDB) createSession(ctx context.Context) (*gtsmodel.RouterSession, db.Error) { @@ -71,24 +63,23 @@ func (s *sessionDB) createSession(ctx context.Context) (*gtsmodel.RouterSession, return nil, err } - rid, err := id.NewULID() + id, err := id.NewULID() if err != nil { return nil, err } rs := >smodel.RouterSession{ - ID: rid, + ID: id, Auth: auth, Crypt: crypt, } - q := s.conn. + if _, err := s.conn. NewInsert(). - Model(rs) - - _, err = q.Exec(ctx) - if err != nil { + Model(rs). + Exec(ctx); err != nil { return nil, s.conn.ProcessError(err) } + return rs, nil } |