mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-12-07 14:09:47 +00:00
This PR migrates the unmaintaiend `lib/pq` library to `jackc/pgx`, which is the de-facto standard lib in go for postgres connections these days. Some implementation notes: We register both `pgx` and `postgresschema` driver names (for backward comp). We can't register `postgres` as this one is still used by `lib/pq` imported by `go-chi/session`, which is in use when users go for the "postgres" session type in the "Session config. It is questionable if anyone is really using the "postgres" driver option in the session config - but for consistency, it would be good to also migrate to `pgx` there, especially as the code lives within Forgejo under [go-chi/session](https://code.forgejo.org/go-chi/session). `pgx` supports multi-host notation in the connection string. New tests have been added therefore. `pgx` also allows for connection string parameters such as `?default_query_exec_mode=simple_protocol`. This should possibly allow running with `pgbouncer` "transaction" mode instead of "session", which could substantially enhance Postgres query handling. ## Checklist ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10219 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org> Co-authored-by: pat-s <patrick.schratz@gmail.com> Co-committed-by: pat-s <patrick.schratz@gmail.com>
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package v1_18
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"forgejo.org/models/issues"
|
|
|
|
"xorm.io/builder"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
func UpdateOpenMilestoneCounts(x *xorm.Engine) error {
|
|
var openMilestoneIDs []int64
|
|
err := x.Table("milestone").Select("id").Where(builder.Neq{"is_closed": true}).Find(&openMilestoneIDs)
|
|
if err != nil {
|
|
return fmt.Errorf("error selecting open milestone IDs: %w", err)
|
|
}
|
|
|
|
for _, id := range openMilestoneIDs {
|
|
_, err := x.ID(id).
|
|
SetExpr("num_issues", builder.Select("count(*)").From("issue").Where(
|
|
builder.Eq{"milestone_id": id},
|
|
)).
|
|
SetExpr("num_closed_issues", builder.Select("count(*)").From("issue").Where(
|
|
builder.Eq{
|
|
"milestone_id": id,
|
|
"is_closed": true,
|
|
},
|
|
)).
|
|
Update(&issues.Milestone{})
|
|
if err != nil {
|
|
return fmt.Errorf("error updating issue counts in milestone %d: %w", id, err)
|
|
}
|
|
_, err = x.Exec("UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?",
|
|
id,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("error setting completeness on milestone %d: %w", id, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|