Skip to content

Commit c3cda9e

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: Round language stats percentage using largest remainder (go-gitea#22026) Support disabling database auto migration (go-gitea#22053)
2 parents 8eb24fd + cf27403 commit c3cda9e

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ LOG_SQL = false ; if unset defaults to true
403403
;;
404404
;; Database maximum number of open connections, default is 0 meaning no maximum
405405
;MAX_OPEN_CONNS = 0
406+
;;
407+
;; Whether execute database models migrations automatically
408+
;AUTO_MIGRATION = true
406409

407410
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
408411
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ The following configuration set `Content-Type: application/vnd.android.package-a
444444
- `MAX_OPEN_CONNS` **0**: Database maximum open connections - default is 0, meaning there is no limit.
445445
- `MAX_IDLE_CONNS` **2**: Max idle database connections on connection pool, default is 2 - this will be capped to `MAX_OPEN_CONNS`.
446446
- `CONN_MAX_LIFETIME` **0 or 3s**: Sets the maximum amount of time a DB connection may be reused - default is 0, meaning there is no limit (except on MySQL where it is 3s - see #6804 & #7071).
447+
- `AUTO_MIGRATION` **true**: Whether execute database models migrations automatically.
447448

448449
Please see #8540 & #8273 for further discussion of the appropriate values for `MAX_OPEN_CONNS`, `MAX_IDLE_CONNS` & `CONN_MAX_LIFETIME` and their
449450
relation to port exhaustion.

models/repo/language_stats.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package repo
66
import (
77
"context"
88
"math"
9+
"sort"
910
"strings"
1011

1112
"code.gitea.io/gitea/models/db"
@@ -43,29 +44,60 @@ func (stats LanguageStatList) LoadAttributes() {
4344

4445
func (stats LanguageStatList) getLanguagePercentages() map[string]float32 {
4546
langPerc := make(map[string]float32)
46-
var otherPerc float32 = 100
47+
var otherPerc float32
4748
var total int64
4849

4950
for _, stat := range stats {
5051
total += stat.Size
5152
}
5253
if total > 0 {
5354
for _, stat := range stats {
54-
perc := float32(math.Round(float64(stat.Size)/float64(total)*1000) / 10)
55+
perc := float32(float64(stat.Size) / float64(total) * 100)
5556
if perc <= 0.1 {
57+
otherPerc += perc
5658
continue
5759
}
58-
otherPerc -= perc
5960
langPerc[stat.Language] = perc
6061
}
61-
otherPerc = float32(math.Round(float64(otherPerc)*10) / 10)
6262
}
6363
if otherPerc > 0 {
6464
langPerc["other"] = otherPerc
6565
}
66+
roundByLargestRemainder(langPerc, 100)
6667
return langPerc
6768
}
6869

70+
// Rounds to 1 decimal point, target should be the expected sum of percs
71+
func roundByLargestRemainder(percs map[string]float32, target float32) {
72+
leftToDistribute := int(target * 10)
73+
74+
keys := make([]string, 0, len(percs))
75+
76+
for k, v := range percs {
77+
percs[k] = v * 10
78+
floored := math.Floor(float64(percs[k]))
79+
leftToDistribute -= int(floored)
80+
keys = append(keys, k)
81+
}
82+
83+
// Sort the keys by the largest remainder
84+
sort.SliceStable(keys, func(i, j int) bool {
85+
_, remainderI := math.Modf(float64(percs[keys[i]]))
86+
_, remainderJ := math.Modf(float64(percs[keys[j]]))
87+
return remainderI > remainderJ
88+
})
89+
90+
// Increment the values in order of largest remainder
91+
for _, k := range keys {
92+
percs[k] = float32(math.Floor(float64(percs[k])))
93+
if leftToDistribute > 0 {
94+
percs[k]++
95+
leftToDistribute--
96+
}
97+
percs[k] /= 10
98+
}
99+
}
100+
69101
// GetLanguageStats returns the language statistics for a repository
70102
func GetLanguageStats(ctx context.Context, repo *Repository) (LanguageStatList, error) {
71103
stats := make(LanguageStatList, 0, 6)

modules/doctor/dbversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
func checkDBVersion(ctx context.Context, logger log.Logger, autofix bool) error {
15+
logger.Info("Expected database version: %d", migrations.ExpectedVersion())
1516
if err := db.InitEngineWithMigration(ctx, migrations.EnsureUpToDate); err != nil {
1617
if !autofix {
1718
logger.Critical("Error: %v during ensure up to date", err)

modules/setting/database.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
MaxOpenConns int
5050
ConnMaxLifetime time.Duration
5151
IterateBufferSize int
52+
AutoMigration bool
5253
}{
5354
Timeout: 500,
5455
IterateBufferSize: 50,
@@ -105,6 +106,7 @@ func InitDBConfig() {
105106
Database.LogSQL = sec.Key("LOG_SQL").MustBool(true)
106107
Database.DBConnectRetries = sec.Key("DB_RETRIES").MustInt(10)
107108
Database.DBConnectBackoff = sec.Key("DB_RETRY_BACKOFF").MustDuration(3 * time.Second)
109+
Database.AutoMigration = sec.Key("AUTO_MIGRATION").MustBool(true)
108110
}
109111

110112
// DBConnStr returns database connection string

routers/common/db.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"code.gitea.io/gitea/models/migrations"
1313
"code.gitea.io/gitea/modules/log"
1414
"code.gitea.io/gitea/modules/setting"
15+
16+
"xorm.io/xorm"
1517
)
1618

1719
// InitDBEngine In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology
@@ -24,7 +26,7 @@ func InitDBEngine(ctx context.Context) (err error) {
2426
default:
2527
}
2628
log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
27-
if err = db.InitEngineWithMigration(ctx, migrations.Migrate); err == nil {
29+
if err = db.InitEngineWithMigration(ctx, migrateWithSetting); err == nil {
2830
break
2931
} else if i == setting.Database.DBConnectRetries-1 {
3032
return err
@@ -36,3 +38,20 @@ func InitDBEngine(ctx context.Context) (err error) {
3638
db.HasEngine = true
3739
return nil
3840
}
41+
42+
func migrateWithSetting(x *xorm.Engine) error {
43+
if setting.Database.AutoMigration {
44+
return migrations.Migrate(x)
45+
}
46+
47+
if current, err := migrations.GetCurrentDBVersion(x); err != nil {
48+
return err
49+
} else if current < 0 {
50+
// execute migrations when the database isn't initialized even if AutoMigration is false
51+
return migrations.Migrate(x)
52+
} else if expected := migrations.ExpectedVersion(); current != expected {
53+
log.Fatal(`"database.AUTO_MIGRATION" is disabled, but current database version %d is not equal to the expected version %d.`+
54+
`You can set "database.AUTO_MIGRATION" to true or migrate manually by running "gitea [--config /path/to/app.ini] migrate"`, current, expected)
55+
}
56+
return nil
57+
}

0 commit comments

Comments
 (0)