-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace all instances of db.DefaultContext
with a custom context
#27065
Comments
I will try to fix it at some places |
I opened a first PR. More will follow. btw: If someone wants to use the GitHub search, here's a Link. |
Part of #27065 This reduces the usage of `db.DefaultContext`. I think I've got enough files for the first PR. When this is merged, I will continue working on this. Considering how many files this PR affect, I hope it won't take to long to merge, so I don't end up in the merge conflict hell. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
@delvh How should I deal with functions that are used by templates? |
Yes, those are unfortunately impossible yet. |
Part of #27065 --------- Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Part of go-gitea#27065 --------- Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Part of #27065 This PR touches functions used in templates. As templates are not static typed, errors are harder to find, but I hope I catch it all. I think some tests from other persons do not hurt.
Part of go-gitea#27065 This PR touches functions used in templates. As templates are not static typed, errors are harder to find, but I hope I catch it all. I think some tests from other persons do not hurt.
Part of #27065 --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: delvh <dev.lh@web.de>
Part of #27065 --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This can be closed now |
All remaining 47 instances are inherently unfixable? |
I think left db.DefaultContext is not easy to change and we can close this issue. |
Problem
Early on, people often used
db.DefaultContext
whenever they did not have access to to acontext.Context
(from the Go standard library).Nowadays, we see the problems from this approach as it means that data is added to transactions it doesn't belong, or requests hang forever waiting for some condition to be met.
Solution
As such, any database call should be started from a fitting context that is not
db.DefaultContext
.The only places where
db.DefaultContext
is still allowed, is inside tests (files ending withtest.go
).Any other occurrence should be replaced with an additional
ctx context.Context
function parameter (that should be the first function parameter).Where to get the
ctx
param?If you don't know where to get it from, in the worst case, you need to pass the context down from the route handler (
routers/
package, i.e.routers/web/web.go
) until you reach the respective function.How to find all occurrences in need of conversion?
On Posix systems (MacOS, Linux, BSD), simply execute the following in a shell in the root directory of the gitea source code:
or if you have
ag
installed:Alternatively, you can use whatever graphical tool you like. This works on all systems.
The text was updated successfully, but these errors were encountered: