1
0
Fork 0
forked from forgejo/forgejo

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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
335 changed files with 4191 additions and 3654 deletions

View file

@ -8,6 +8,7 @@ import (
"fmt"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"xorm.io/builder"
"xorm.io/xorm"
@ -60,7 +61,11 @@ func (key *DeployKey) IsReadOnly() bool {
return key.Mode == AccessModeRead
}
func checkDeployKey(e Engine, keyID, repoID int64, name string) error {
func init() {
db.RegisterModel(new(DeployKey))
}
func checkDeployKey(e db.Engine, keyID, repoID int64, name string) error {
// Note: We want error detail, not just true or false here.
has, err := e.
Where("key_id = ? AND repo_id = ?", keyID, repoID).
@ -102,7 +107,7 @@ func addDeployKey(e *xorm.Session, keyID, repoID int64, name, fingerprint string
// HasDeployKey returns true if public key is a deploy key of given repository.
func HasDeployKey(keyID, repoID int64) bool {
has, _ := x.
has, _ := db.DefaultContext().Engine().
Where("key_id = ? AND repo_id = ?", keyID, repoID).
Get(new(DeployKey))
return has
@ -120,7 +125,7 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey
accessMode = AccessModeWrite
}
sess := x.NewSession()
sess := db.DefaultContext().NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return nil, err
@ -159,10 +164,10 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey
// GetDeployKeyByID returns deploy key by given ID.
func GetDeployKeyByID(id int64) (*DeployKey, error) {
return getDeployKeyByID(x, id)
return getDeployKeyByID(db.DefaultContext().Engine(), id)
}
func getDeployKeyByID(e Engine, id int64) (*DeployKey, error) {
func getDeployKeyByID(e db.Engine, id int64) (*DeployKey, error) {
key := new(DeployKey)
has, err := e.ID(id).Get(key)
if err != nil {
@ -175,10 +180,10 @@ func getDeployKeyByID(e Engine, id int64) (*DeployKey, error) {
// GetDeployKeyByRepo returns deploy key by given public key ID and repository ID.
func GetDeployKeyByRepo(keyID, repoID int64) (*DeployKey, error) {
return getDeployKeyByRepo(x, keyID, repoID)
return getDeployKeyByRepo(db.DefaultContext().Engine(), keyID, repoID)
}
func getDeployKeyByRepo(e Engine, keyID, repoID int64) (*DeployKey, error) {
func getDeployKeyByRepo(e db.Engine, keyID, repoID int64) (*DeployKey, error) {
key := &DeployKey{
KeyID: keyID,
RepoID: repoID,
@ -194,19 +199,19 @@ func getDeployKeyByRepo(e Engine, keyID, repoID int64) (*DeployKey, error) {
// UpdateDeployKeyCols updates deploy key information in the specified columns.
func UpdateDeployKeyCols(key *DeployKey, cols ...string) error {
_, err := x.ID(key.ID).Cols(cols...).Update(key)
_, err := db.DefaultContext().Engine().ID(key.ID).Cols(cols...).Update(key)
return err
}
// UpdateDeployKey updates deploy key information.
func UpdateDeployKey(key *DeployKey) error {
_, err := x.ID(key.ID).AllCols().Update(key)
_, err := db.DefaultContext().Engine().ID(key.ID).AllCols().Update(key)
return err
}
// DeleteDeployKey deletes deploy key from its repository authorized_keys file if needed.
func DeleteDeployKey(doer *User, id int64) error {
sess := x.NewSession()
sess := db.DefaultContext().NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
@ -217,7 +222,7 @@ func DeleteDeployKey(doer *User, id int64) error {
return sess.Commit()
}
func deleteDeployKey(sess Engine, doer *User, id int64) error {
func deleteDeployKey(sess db.Engine, doer *User, id int64) error {
key, err := getDeployKeyByID(sess, id)
if err != nil {
if IsErrDeployKeyNotExist(err) {
@ -288,10 +293,10 @@ func (opt ListDeployKeysOptions) toCond() builder.Cond {
// ListDeployKeys returns a list of deploy keys matching the provided arguments.
func ListDeployKeys(opts *ListDeployKeysOptions) ([]*DeployKey, error) {
return listDeployKeys(x, opts)
return listDeployKeys(db.DefaultContext().Engine(), opts)
}
func listDeployKeys(e Engine, opts *ListDeployKeysOptions) ([]*DeployKey, error) {
func listDeployKeys(e db.Engine, opts *ListDeployKeysOptions) ([]*DeployKey, error) {
sess := e.Where(opts.toCond())
if opts.Page != 0 {
@ -307,5 +312,5 @@ func listDeployKeys(e Engine, opts *ListDeployKeysOptions) ([]*DeployKey, error)
// CountDeployKeys returns count deploy keys matching the provided arguments.
func CountDeployKeys(opts *ListDeployKeysOptions) (int64, error) {
return x.Where(opts.toCond()).Count(&DeployKey{})
return db.DefaultContext().Engine().Where(opts.toCond()).Count(&DeployKey{})
}