1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-19 00:38:36 +00:00

DBContext is just a Context (#17100)

* DBContext is just a Context

This PR removes some of the specialness from the DBContext and makes it context
This allows us to simplify the GetEngine code to wrap around any context in future
and means that we can change our loadRepo(e Engine) functions to simply take contexts.

Signed-off-by: Andrew Thornton <art27@cantab.net>

* fix unit tests

Signed-off-by: Andrew Thornton <art27@cantab.net>

* another place that needs to set the initial context

Signed-off-by: Andrew Thornton <art27@cantab.net>

* avoid race

Signed-off-by: Andrew Thornton <art27@cantab.net>

* change attachment error

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
zeripath
2021-09-23 16:45:36 +01:00
committed by GitHub
parent b22be7f594
commit 9302eba971
129 changed files with 1112 additions and 1022 deletions

View File

@@ -127,17 +127,17 @@ func getNotifications(e db.Engine, options *FindNotificationOptions) (nl Notific
// GetNotifications returns all notifications that fit to the given options.
func GetNotifications(opts *FindNotificationOptions) (NotificationList, error) {
return getNotifications(db.DefaultContext().Engine(), opts)
return getNotifications(db.GetEngine(db.DefaultContext), opts)
}
// CountNotifications count all notifications that fit to the given options and ignore pagination.
func CountNotifications(opts *FindNotificationOptions) (int64, error) {
return db.DefaultContext().Engine().Where(opts.ToCond()).Count(&Notification{})
return db.GetEngine(db.DefaultContext).Where(opts.ToCond()).Count(&Notification{})
}
// CreateRepoTransferNotification creates notification for the user a repository was transferred to
func CreateRepoTransferNotification(doer, newOwner *User, repo *Repository) error {
sess := db.DefaultContext().NewSession()
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
@@ -179,7 +179,7 @@ func CreateRepoTransferNotification(doer, newOwner *User, repo *Repository) erro
// for each watcher, or updates it if already exists
// receiverID > 0 just send to reciver, else send to all watcher
func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID, receiverID int64) error {
sess := db.DefaultContext().NewSession()
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
@@ -352,7 +352,7 @@ func getIssueNotification(e db.Engine, userID, issueID int64) (*Notification, er
// NotificationsForUser returns notifications for a given user and status
func NotificationsForUser(user *User, statuses []NotificationStatus, page, perPage int) (NotificationList, error) {
return notificationsForUser(db.DefaultContext().Engine(), user, statuses, page, perPage)
return notificationsForUser(db.GetEngine(db.DefaultContext), user, statuses, page, perPage)
}
func notificationsForUser(e db.Engine, user *User, statuses []NotificationStatus, page, perPage int) (notifications []*Notification, err error) {
@@ -375,7 +375,7 @@ func notificationsForUser(e db.Engine, user *User, statuses []NotificationStatus
// CountUnread count unread notifications for a user
func CountUnread(user *User) int64 {
return countUnread(db.DefaultContext().Engine(), user.ID)
return countUnread(db.GetEngine(db.DefaultContext), user.ID)
}
func countUnread(e db.Engine, userID int64) int64 {
@@ -389,7 +389,7 @@ func countUnread(e db.Engine, userID int64) int64 {
// LoadAttributes load Repo Issue User and Comment if not loaded
func (n *Notification) LoadAttributes() (err error) {
return n.loadAttributes(db.DefaultContext().Engine())
return n.loadAttributes(db.GetEngine(db.DefaultContext))
}
func (n *Notification) loadAttributes(e db.Engine) (err error) {
@@ -451,12 +451,12 @@ func (n *Notification) loadUser(e db.Engine) (err error) {
// GetRepo returns the repo of the notification
func (n *Notification) GetRepo() (*Repository, error) {
return n.Repository, n.loadRepo(db.DefaultContext().Engine())
return n.Repository, n.loadRepo(db.GetEngine(db.DefaultContext))
}
// GetIssue returns the issue of the notification
func (n *Notification) GetIssue() (*Issue, error) {
return n.Issue, n.loadIssue(db.DefaultContext().Engine())
return n.Issue, n.loadIssue(db.GetEngine(db.DefaultContext))
}
// HTMLURL formats a URL-string to the notification
@@ -521,7 +521,7 @@ func (nl NotificationList) LoadRepos() (RepositoryList, []int, error) {
if left < limit {
limit = left
}
rows, err := db.DefaultContext().Engine().
rows, err := db.GetEngine(db.DefaultContext).
In("id", repoIDs[:limit]).
Rows(new(Repository))
if err != nil {
@@ -597,7 +597,7 @@ func (nl NotificationList) LoadIssues() ([]int, error) {
if left < limit {
limit = left
}
rows, err := db.DefaultContext().Engine().
rows, err := db.GetEngine(db.DefaultContext).
In("id", issueIDs[:limit]).
Rows(new(Issue))
if err != nil {
@@ -683,7 +683,7 @@ func (nl NotificationList) LoadComments() ([]int, error) {
if left < limit {
limit = left
}
rows, err := db.DefaultContext().Engine().
rows, err := db.GetEngine(db.DefaultContext).
In("id", commentIDs[:limit]).
Rows(new(Comment))
if err != nil {
@@ -723,7 +723,7 @@ func (nl NotificationList) LoadComments() ([]int, error) {
// GetNotificationCount returns the notification count for user
func GetNotificationCount(user *User, status NotificationStatus) (int64, error) {
return getNotificationCount(db.DefaultContext().Engine(), user, status)
return getNotificationCount(db.GetEngine(db.DefaultContext), user, status)
}
func getNotificationCount(e db.Engine, user *User, status NotificationStatus) (count int64, err error) {
@@ -746,7 +746,7 @@ func GetUIDsAndNotificationCounts(since, until timeutil.TimeStamp) ([]UserIDCoun
`WHERE user_id IN (SELECT user_id FROM notification WHERE updated_unix >= ? AND ` +
`updated_unix < ?) AND status = ? GROUP BY user_id`
var res []UserIDCount
return res, db.DefaultContext().Engine().SQL(sql, since, until, NotificationStatusUnread).Find(&res)
return res, db.GetEngine(db.DefaultContext).SQL(sql, since, until, NotificationStatusUnread).Find(&res)
}
func setIssueNotificationStatusReadIfUnread(e db.Engine, userID, issueID int64) error {
@@ -778,7 +778,7 @@ func setRepoNotificationStatusReadIfUnread(e db.Engine, userID, repoID int64) er
// SetNotificationStatus change the notification status
func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) (*Notification, error) {
notification, err := getNotificationByID(db.DefaultContext().Engine(), notificationID)
notification, err := getNotificationByID(db.GetEngine(db.DefaultContext), notificationID)
if err != nil {
return notification, err
}
@@ -789,13 +789,13 @@ func SetNotificationStatus(notificationID int64, user *User, status Notification
notification.Status = status
_, err = db.DefaultContext().Engine().ID(notificationID).Update(notification)
_, err = db.GetEngine(db.DefaultContext).ID(notificationID).Update(notification)
return notification, err
}
// GetNotificationByID return notification by ID
func GetNotificationByID(notificationID int64) (*Notification, error) {
return getNotificationByID(db.DefaultContext().Engine(), notificationID)
return getNotificationByID(db.GetEngine(db.DefaultContext), notificationID)
}
func getNotificationByID(e db.Engine, notificationID int64) (*Notification, error) {
@@ -817,7 +817,7 @@ func getNotificationByID(e db.Engine, notificationID int64) (*Notification, erro
// UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
func UpdateNotificationStatuses(user *User, currentStatus, desiredStatus NotificationStatus) error {
n := &Notification{Status: desiredStatus, UpdatedBy: user.ID}
_, err := db.DefaultContext().Engine().
_, err := db.GetEngine(db.DefaultContext).
Where("user_id = ? AND status = ?", user.ID, currentStatus).
Cols("status", "updated_by", "updated_unix").
Update(n)