1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-05-11 05:55:29 +00:00

Move db related basic functions to models/db (#17075)

* Move db related basic functions to models/db

* Fix lint

* Fix lint

* Fix test

* Fix lint

* Fix lint

* revert unnecessary change

* Fix test

* Fix wrong replace string

* Use *Context

* Correct committer spelling and fix wrong replaced words

Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
Lunny Xiao
2021-09-19 19:49:59 +08:00
committed by GitHub
parent 462306e263
commit a4bfef265d
335 changed files with 4191 additions and 3654 deletions

View File

@ -7,6 +7,7 @@ package models
import (
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
)
@ -17,9 +18,13 @@ type Session struct {
Expiry timeutil.TimeStamp // has to be Expiry to match with go-chi/session
}
func init() {
db.RegisterModel(new(Session))
}
// UpdateSession updates the session with provided id
func UpdateSession(key string, data []byte) error {
_, err := x.ID(key).Update(&Session{
_, err := db.DefaultContext().Engine().ID(key).Update(&Session{
Data: data,
Expiry: timeutil.TimeStampNow(),
})
@ -31,7 +36,7 @@ func ReadSession(key string) (*Session, error) {
session := Session{
Key: key,
}
sess := x.NewSession()
sess := db.DefaultContext().NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return nil, err
@ -55,12 +60,12 @@ func ExistSession(key string) (bool, error) {
session := Session{
Key: key,
}
return x.Get(&session)
return db.DefaultContext().Engine().Get(&session)
}
// DestroySession destroys a session
func DestroySession(key string) error {
_, err := x.Delete(&Session{
_, err := db.DefaultContext().Engine().Delete(&Session{
Key: key,
})
return err
@ -68,7 +73,7 @@ func DestroySession(key string) error {
// RegenerateSession regenerates a session from the old id
func RegenerateSession(oldKey, newKey string) (*Session, error) {
sess := x.NewSession()
sess := db.DefaultContext().NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return nil, err
@ -112,11 +117,11 @@ func RegenerateSession(oldKey, newKey string) (*Session, error) {
// CountSessions returns the number of sessions
func CountSessions() (int64, error) {
return x.Count(&Session{})
return db.DefaultContext().Engine().Count(&Session{})
}
// CleanupSessions cleans up expired sessions
func CleanupSessions(maxLifetime int64) error {
_, err := x.Where("expiry <= ?", timeutil.TimeStampNow().Add(-maxLifetime)).Delete(&Session{})
_, err := db.DefaultContext().Engine().Where("expiry <= ?", timeutil.TimeStampNow().Add(-maxLifetime)).Delete(&Session{})
return err
}