jojo/models/db
Mathieu Fenniak 28dbbf44cf [v11.0/forgejo] fix: add forgejo doctor cleanup-commit-status command (#10686) (#10783)
**Backport:** #10686

(cherry picked from commit 270317a3ad)

```
NAME:
   forgejo doctor cleanup-commit-status - Cleanup extra records in commit_status table

USAGE:
   forgejo doctor cleanup-commit-status

DESCRIPTION:
   Forgejo suffered from a bug which caused the creation of more entries in the
   "commit_status" table than necessary. This operation removes the redundant
   data caused by the bug. Removing this data is almost always safe.
   These reundant records can be accessed by users through the API, making it
   possible, but unlikely, that removing it could have an impact to
   integrating services (API: /repos/{owner}/{repo}/commits/{ref}/statuses).

   It is safe to run while Forgejo is online.

   On very large Forgejo instances, the performance of operation will improve
   if the buffer-size option is used with large values. Approximately 130 MB of
   memory is required for every 100,000 records in the buffer.

   Bug reference: https://codeberg.org/forgejo/forgejo/issues/10671

OPTIONS:
   --help, -h                       show help
   --custom-path string, -C string  Set custom path (defaults to '{WorkPath}/custom')
   --config string, -c string       Set custom config file (defaults to '{WorkPath}/custom/conf/app.ini')
   --work-path string, -w string    Set Forgejo's working path (defaults to the directory of the Forgejo binary)
   --verbose, -V                    Show process details
   --dry-run                        Report statistics from the operation but do not modify the database
   --buffer-size int                Record count per query while iterating records; larger values are typically faster but use more memory (default: 100000)
   --delete-chunk-size int          Number of records to delete per DELETE query (default: 1000)
```

The cleanup effectively performs `SELECT * FROM commit_status ORDER BY repo_id, sha, context, index, id`, and iterates through the records.  Whenever `index, id` changes without the other fields changing, then it's a useless record that can be deleted.  The major complication is doing that at scale without bringing the entire database table into memory, which is performed through a new iteration method `IterateByKeyset`.

Manually tested against a 455,303 record table in PostgreSQL, MySQL, and SQLite, which was reduced to 10,781 records, dropping 97.5% of the records.

Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10783
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
2026-01-13 16:17:06 +01:00
..
install [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
paginator [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
collation.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
common.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
consistency.go Implement FSFE REUSE for golang files (#21840) 2022-11-27 18:20:29 +00:00
context.go Small refactor to reduce unnecessary database queries and remove duplicated functions (#33779) 2025-03-09 15:05:24 +01:00
context_committer_test.go chore: use errors.New to replace fmt.Errorf with no parameters (#32800) 2024-12-15 09:15:57 +01:00
context_test.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
convert.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
engine.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
engine_test.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
error.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
index.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
index_test.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
iterate.go [v11.0/forgejo] fix: add forgejo doctor cleanup-commit-status command (#10686) (#10783) 2026-01-13 16:17:06 +01:00
iterate_test.go [v11.0/forgejo] fix: add forgejo doctor cleanup-commit-status command (#10686) (#10783) 2026-01-13 16:17:06 +01:00
list.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
list_test.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
log.go [v11.0/forgejo] chore(cleanup): suppress non actionable XORM warnings (#8022) 2025-05-30 13:43:41 +02:00
main_test.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
name.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
search.go [Refactor] Unify repo search order by logic (#30876) 2024-06-16 13:42:58 +02:00
sequence.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00
sql_postgres_with_schema.go [v11.0/forgejo] chore: branding import path (#7354) 2025-03-27 20:13:05 +00:00