1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +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

@@ -40,7 +40,7 @@ func (t *TrackedTime) AfterLoad() {
// LoadAttributes load Issue, User
func (t *TrackedTime) LoadAttributes() (err error) {
return t.loadAttributes(db.DefaultContext().Engine())
return t.loadAttributes(db.GetEngine(db.DefaultContext))
}
func (t *TrackedTime) loadAttributes(e db.Engine) (err error) {
@@ -131,12 +131,12 @@ func getTrackedTimes(e db.Engine, options *FindTrackedTimesOptions) (trackedTime
// GetTrackedTimes returns all tracked times that fit to the given options.
func GetTrackedTimes(opts *FindTrackedTimesOptions) (TrackedTimeList, error) {
return getTrackedTimes(db.DefaultContext().Engine(), opts)
return getTrackedTimes(db.GetEngine(db.DefaultContext), opts)
}
// CountTrackedTimes returns count of tracked times that fit to the given options.
func CountTrackedTimes(opts *FindTrackedTimesOptions) (int64, error) {
sess := db.DefaultContext().Engine().Where(opts.toCond())
sess := db.GetEngine(db.DefaultContext).Where(opts.toCond())
if opts.RepositoryID > 0 || opts.MilestoneID > 0 {
sess = sess.Join("INNER", "issue", "issue.id = tracked_time.issue_id")
}
@@ -149,12 +149,12 @@ func getTrackedSeconds(e db.Engine, opts FindTrackedTimesOptions) (trackedSecond
// GetTrackedSeconds return sum of seconds
func GetTrackedSeconds(opts FindTrackedTimesOptions) (int64, error) {
return getTrackedSeconds(db.DefaultContext().Engine(), opts)
return getTrackedSeconds(db.GetEngine(db.DefaultContext), opts)
}
// AddTime will add the given time (in seconds) to the issue
func AddTime(user *User, issue *Issue, amount int64, created time.Time) (*TrackedTime, error) {
sess := db.DefaultContext().NewSession()
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if err := sess.Begin(); err != nil {
@@ -230,7 +230,7 @@ func TotalTimes(options *FindTrackedTimesOptions) (map[*User]string, error) {
// DeleteIssueUserTimes deletes times for issue
func DeleteIssueUserTimes(issue *Issue, user *User) error {
sess := db.DefaultContext().NewSession()
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if err := sess.Begin(); err != nil {
@@ -268,7 +268,7 @@ func DeleteIssueUserTimes(issue *Issue, user *User) error {
// DeleteTime delete a specific Time
func DeleteTime(t *TrackedTime) error {
sess := db.DefaultContext().NewSession()
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if err := sess.Begin(); err != nil {
@@ -318,7 +318,7 @@ func deleteTime(e db.Engine, t *TrackedTime) error {
// GetTrackedTimeByID returns raw TrackedTime without loading attributes by id
func GetTrackedTimeByID(id int64) (*TrackedTime, error) {
time := new(TrackedTime)
has, err := db.DefaultContext().Engine().ID(id).Get(time)
has, err := db.GetEngine(db.DefaultContext).ID(id).Get(time)
if err != nil {
return nil, err
} else if !has {