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:
parent
b22be7f594
commit
9302eba971
129 changed files with 1112 additions and 1022 deletions
|
@ -351,7 +351,7 @@ func (w *Webhook) EventsArray() []string {
|
|||
|
||||
// CreateWebhook creates a new web hook.
|
||||
func CreateWebhook(w *Webhook) error {
|
||||
return createWebhook(db.DefaultContext().Engine(), w)
|
||||
return createWebhook(db.GetEngine(db.DefaultContext), w)
|
||||
}
|
||||
|
||||
func createWebhook(e db.Engine, w *Webhook) error {
|
||||
|
@ -363,7 +363,7 @@ func createWebhook(e db.Engine, w *Webhook) error {
|
|||
// getWebhook uses argument bean as query condition,
|
||||
// ID must be specified and do not assign unnecessary fields.
|
||||
func getWebhook(bean *Webhook) (*Webhook, error) {
|
||||
has, err := db.DefaultContext().Engine().Get(bean)
|
||||
has, err := db.GetEngine(db.DefaultContext).Get(bean)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
|
@ -434,17 +434,17 @@ func listWebhooksByOpts(e db.Engine, opts *ListWebhookOptions) ([]*Webhook, erro
|
|||
|
||||
// ListWebhooksByOpts return webhooks based on options
|
||||
func ListWebhooksByOpts(opts *ListWebhookOptions) ([]*Webhook, error) {
|
||||
return listWebhooksByOpts(db.DefaultContext().Engine(), opts)
|
||||
return listWebhooksByOpts(db.GetEngine(db.DefaultContext), opts)
|
||||
}
|
||||
|
||||
// CountWebhooksByOpts count webhooks based on options and ignore pagination
|
||||
func CountWebhooksByOpts(opts *ListWebhookOptions) (int64, error) {
|
||||
return db.DefaultContext().Engine().Where(opts.toCond()).Count(&Webhook{})
|
||||
return db.GetEngine(db.DefaultContext).Where(opts.toCond()).Count(&Webhook{})
|
||||
}
|
||||
|
||||
// GetDefaultWebhooks returns all admin-default webhooks.
|
||||
func GetDefaultWebhooks() ([]*Webhook, error) {
|
||||
return getDefaultWebhooks(db.DefaultContext().Engine())
|
||||
return getDefaultWebhooks(db.GetEngine(db.DefaultContext))
|
||||
}
|
||||
|
||||
func getDefaultWebhooks(e db.Engine) ([]*Webhook, error) {
|
||||
|
@ -457,7 +457,7 @@ func getDefaultWebhooks(e db.Engine) ([]*Webhook, error) {
|
|||
// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
|
||||
func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
|
||||
webhook := &Webhook{ID: id}
|
||||
has, err := db.DefaultContext().Engine().
|
||||
has, err := db.GetEngine(db.DefaultContext).
|
||||
Where("repo_id=? AND org_id=?", 0, 0).
|
||||
Get(webhook)
|
||||
if err != nil {
|
||||
|
@ -470,7 +470,7 @@ func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
|
|||
|
||||
// GetSystemWebhooks returns all admin system webhooks.
|
||||
func GetSystemWebhooks() ([]*Webhook, error) {
|
||||
return getSystemWebhooks(db.DefaultContext().Engine())
|
||||
return getSystemWebhooks(db.GetEngine(db.DefaultContext))
|
||||
}
|
||||
|
||||
func getSystemWebhooks(e db.Engine) ([]*Webhook, error) {
|
||||
|
@ -482,20 +482,20 @@ func getSystemWebhooks(e db.Engine) ([]*Webhook, error) {
|
|||
|
||||
// UpdateWebhook updates information of webhook.
|
||||
func UpdateWebhook(w *Webhook) error {
|
||||
_, err := db.DefaultContext().Engine().ID(w.ID).AllCols().Update(w)
|
||||
_, err := db.GetEngine(db.DefaultContext).ID(w.ID).AllCols().Update(w)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateWebhookLastStatus updates last status of webhook.
|
||||
func UpdateWebhookLastStatus(w *Webhook) error {
|
||||
_, err := db.DefaultContext().Engine().ID(w.ID).Cols("last_status").Update(w)
|
||||
_, err := db.GetEngine(db.DefaultContext).ID(w.ID).Cols("last_status").Update(w)
|
||||
return err
|
||||
}
|
||||
|
||||
// deleteWebhook uses argument bean as query condition,
|
||||
// ID must be specified and do not assign unnecessary fields.
|
||||
func deleteWebhook(bean *Webhook) (err error) {
|
||||
sess := db.DefaultContext().NewSession()
|
||||
sess := db.NewSession(db.DefaultContext)
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
return err
|
||||
|
@ -530,7 +530,7 @@ func DeleteWebhookByOrgID(orgID, id int64) error {
|
|||
|
||||
// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
|
||||
func DeleteDefaultSystemWebhook(id int64) error {
|
||||
sess := db.DefaultContext().NewSession()
|
||||
sess := db.NewSession(db.DefaultContext)
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
|
@ -713,7 +713,7 @@ func (t *HookTask) simpleMarshalJSON(v interface{}) string {
|
|||
// HookTasks returns a list of hook tasks by given conditions.
|
||||
func HookTasks(hookID int64, page int) ([]*HookTask, error) {
|
||||
tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
|
||||
return tasks, db.DefaultContext().Engine().
|
||||
return tasks, db.GetEngine(db.DefaultContext).
|
||||
Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).
|
||||
Where("hook_id=?", hookID).
|
||||
Desc("id").
|
||||
|
@ -723,7 +723,7 @@ func HookTasks(hookID int64, page int) ([]*HookTask, error) {
|
|||
// CreateHookTask creates a new hook task,
|
||||
// it handles conversion from Payload to PayloadContent.
|
||||
func CreateHookTask(t *HookTask) error {
|
||||
return createHookTask(db.DefaultContext().Engine(), t)
|
||||
return createHookTask(db.GetEngine(db.DefaultContext), t)
|
||||
}
|
||||
|
||||
func createHookTask(e db.Engine, t *HookTask) error {
|
||||
|
@ -739,14 +739,14 @@ func createHookTask(e db.Engine, t *HookTask) error {
|
|||
|
||||
// UpdateHookTask updates information of hook task.
|
||||
func UpdateHookTask(t *HookTask) error {
|
||||
_, err := db.DefaultContext().Engine().ID(t.ID).AllCols().Update(t)
|
||||
_, err := db.GetEngine(db.DefaultContext).ID(t.ID).AllCols().Update(t)
|
||||
return err
|
||||
}
|
||||
|
||||
// FindUndeliveredHookTasks represents find the undelivered hook tasks
|
||||
func FindUndeliveredHookTasks() ([]*HookTask, error) {
|
||||
tasks := make([]*HookTask, 0, 10)
|
||||
if err := db.DefaultContext().Engine().Where("is_delivered=?", false).Find(&tasks); err != nil {
|
||||
if err := db.GetEngine(db.DefaultContext).Where("is_delivered=?", false).Find(&tasks); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tasks, nil
|
||||
|
@ -755,7 +755,7 @@ func FindUndeliveredHookTasks() ([]*HookTask, error) {
|
|||
// FindRepoUndeliveredHookTasks represents find the undelivered hook tasks of one repository
|
||||
func FindRepoUndeliveredHookTasks(repoID int64) ([]*HookTask, error) {
|
||||
tasks := make([]*HookTask, 0, 5)
|
||||
if err := db.DefaultContext().Engine().Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
|
||||
if err := db.GetEngine(db.DefaultContext).Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tasks, nil
|
||||
|
@ -767,7 +767,7 @@ func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType,
|
|||
|
||||
if cleanupType == OlderThan {
|
||||
deleteOlderThan := time.Now().Add(-olderThan).UnixNano()
|
||||
deletes, err := db.DefaultContext().Engine().
|
||||
deletes, err := db.GetEngine(db.DefaultContext).
|
||||
Where("is_delivered = ? and delivered < ?", true, deleteOlderThan).
|
||||
Delete(new(HookTask))
|
||||
if err != nil {
|
||||
|
@ -776,7 +776,7 @@ func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType,
|
|||
log.Trace("Deleted %d rows from hook_task", deletes)
|
||||
} else if cleanupType == PerWebhook {
|
||||
hookIDs := make([]int64, 0, 10)
|
||||
err := db.DefaultContext().Engine().Table("webhook").
|
||||
err := db.GetEngine(db.DefaultContext).Table("webhook").
|
||||
Where("id > 0").
|
||||
Cols("id").
|
||||
Find(&hookIDs)
|
||||
|
@ -801,7 +801,7 @@ func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType,
|
|||
func deleteDeliveredHookTasksByWebhook(hookID int64, numberDeliveriesToKeep int) error {
|
||||
log.Trace("Deleting hook_task rows for webhook %d, keeping the most recent %d deliveries", hookID, numberDeliveriesToKeep)
|
||||
deliveryDates := make([]int64, 0, 10)
|
||||
err := db.DefaultContext().Engine().Table("hook_task").
|
||||
err := db.GetEngine(db.DefaultContext).Table("hook_task").
|
||||
Where("hook_task.hook_id = ? AND hook_task.is_delivered = ? AND hook_task.delivered is not null", hookID, true).
|
||||
Cols("hook_task.delivered").
|
||||
Join("INNER", "webhook", "hook_task.hook_id = webhook.id").
|
||||
|
@ -813,7 +813,7 @@ func deleteDeliveredHookTasksByWebhook(hookID int64, numberDeliveriesToKeep int)
|
|||
}
|
||||
|
||||
if len(deliveryDates) > 0 {
|
||||
deletes, err := db.DefaultContext().Engine().
|
||||
deletes, err := db.GetEngine(db.DefaultContext).
|
||||
Where("hook_id = ? and is_delivered = ? and delivered <= ?", hookID, true, deliveryDates[0]).
|
||||
Delete(new(HookTask))
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue