forgejo/models/db
Gusted afbd05c398 fix: get new session from enginegroup instead of masterengine (#10140)
Within Codeberg we are looking into distributing the database queries, we tried forgejo/forgejo!7212 on several occasions but never got it to work.

After a long debugging session in a staging environment I was able to find two bugs that made it impossible for this feature to work: forgejo/docs!1587 which resulted in replica engines never being configured and used if you followed the documentation. The other bug is what this patch intends to fix. In order to do some database operation, you need the database engine - it will first look if one is set for the context (only useful for transactions) and otherwise create a new session of the engine from the master engine `x`. The problem is that `x` is explicitly set to be the master engine and not the engine group (that includes the replica engines) - Unless the code uses `DefaultContext`, which is almost nowhere used after some great refactoring in Gitea to use the passed context, it did not use the replica engines.

Get engine from the `DefaultContext` (which is set to the enginegroup) and create a new session from that.

20f8572b92/models/db/engine.go (L220-L231)

And `SetDefaultEngine` is called from 20f8572b92/models/db/engine.go (L212)

Where `eng` is the engine group.

## Test

1. Configure database replicas.
2. Start Forgejo.
3. Verify Forgejo loads.
4. Stop the database replicas.
5. Verify Forgejo shows 500 errors.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10140
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-11-17 14:42:56 +01:00
..
install fix: make installing Forgejo work again (#7452) 2025-04-04 03:30:19 +00:00
paginator chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
collation.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
common.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
consistency.go Implement FSFE REUSE for golang files (#21840) 2022-11-27 18:20:29 +00:00
context.go fix: get new session from enginegroup instead of masterengine (#10140) 2025-11-17 14:42:56 +01:00
context_committer_test.go fix: possible cause of invalid issue counts; cache invalidation occurs before a active transaction is committed (#10130) 2025-11-17 01:07:29 +01:00
context_test.go fix: possible cause of invalid issue counts; cache invalidation occurs before a active transaction is committed (#10130) 2025-11-17 01:07:29 +01:00
convert.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
engine.go fix: get new session from enginegroup instead of masterengine (#10140) 2025-11-17 14:42:56 +01:00
engine_test.go feat: add foreign keys to table pull_request (#9832) 2025-10-24 18:02:14 +02:00
error.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
foreign_keys.go test: introduce TruncateBeansCascade test helper to support data cleanup of foreign-key referenced tables (#9684) 2025-10-15 20:26:41 +02:00
index.go feat: improve mysqlGetNextResourceIndex to use modern MariaDB features (#8691) 2025-08-08 01:51:55 +02:00
index_test.go feat: use XORM EngineGroup instead of single Engine connection (#7212) 2025-03-30 11:34:02 +00:00
iterate.go fix: db.Iterate can miss records, can return records twice (#9657) 2025-10-12 21:47:26 +02:00
iterate_test.go fix: db.Iterate can miss records, can return records twice (#9657) 2025-10-12 21:47:26 +02:00
list.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
list_test.go feat: use XORM EngineGroup instead of single Engine connection (#7212) 2025-03-30 11:34:02 +00:00
log.go chore(cleanup): suppress non actionable XORM warnings (#8021) 2025-05-30 13:01:08 +02:00
main_test.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
name.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
search.go [Refactor] Unify repo search order by logic (#30876) 2024-06-16 13:42:58 +02:00
sequence.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
sql_postgres_with_schema.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
table_names_test.go chore: do not require empty fixtures to clean tables (#8353) 2025-06-30 23:04:16 +02:00