Description
Problem
Early on, people often used db.DefaultContext
whenever they did not have access to to a context.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 with test.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:
grep -irnH 'db\.DefaultContext' {routers,services,models,modules} | grep -v 'test.*\.go'
or if you have ag
installed:
ag 'db\.DefaultContext' | grep -v 'test.*\.go'
Alternatively, you can use whatever graphical tool you like. This works on all systems.