Skip to content

Commit cb3173a

Browse files
authored
Use "utf8mb4" for MySQL by default (#25432)
TBH, I don't see much difference from `Remove "CHARSET" config option for MySQL, always use "utf8mb4"` #25413 Close #25413
1 parent ffe0894 commit cb3173a

File tree

5 files changed

+11
-36
lines changed

5 files changed

+11
-36
lines changed

modules/setting/database.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ func LoadDBSetting() {
6060
func loadDBSetting(rootCfg ConfigProvider) {
6161
sec := rootCfg.Section("database")
6262
Database.Type = DatabaseType(sec.Key("DB_TYPE").String())
63-
defaultCharset := "utf8"
64-
65-
if Database.Type.IsMySQL() {
66-
defaultCharset = "utf8mb4"
67-
}
6863

6964
Database.Host = sec.Key("HOST").String()
7065
Database.Name = sec.Key("NAME").String()
@@ -74,9 +69,10 @@ func loadDBSetting(rootCfg ConfigProvider) {
7469
}
7570
Database.Schema = sec.Key("SCHEMA").String()
7671
Database.SSLMode = sec.Key("SSL_MODE").MustString("disable")
77-
Database.Charset = sec.Key("CHARSET").In(defaultCharset, []string{"utf8", "utf8mb4"})
78-
if Database.Type.IsMySQL() && defaultCharset != "utf8mb4" {
79-
log.Error("Deprecated database mysql charset utf8 support, please use utf8mb4 or convert utf8 to utf8mb4.")
72+
73+
Database.Charset = sec.Key("CHARSET").MustString("utf8mb4")
74+
if Database.Type.IsMySQL() && Database.Charset != "utf8mb4" {
75+
log.Error(`Deprecated database mysql charset utf8 support, please use utf8mb4 and convert utf8 database to utf8mb4 by "gitea convert".`)
8076
}
8177

8278
Database.Path = sec.Key("PATH").MustString(filepath.Join(AppDataPath, "gitea.db"))
@@ -101,9 +97,9 @@ func loadDBSetting(rootCfg ConfigProvider) {
10197
// DBConnStr returns database connection string
10298
func DBConnStr() (string, error) {
10399
var connStr string
104-
Param := "?"
105-
if strings.Contains(Database.Name, Param) {
106-
Param = "&"
100+
paramSep := "?"
101+
if strings.Contains(Database.Name, paramSep) {
102+
paramSep = "&"
107103
}
108104
switch Database.Type {
109105
case "mysql":
@@ -116,15 +112,15 @@ func DBConnStr() (string, error) {
116112
tls = "false"
117113
}
118114
connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s",
119-
Database.User, Database.Passwd, connType, Database.Host, Database.Name, Param, Database.Charset, tls)
115+
Database.User, Database.Passwd, connType, Database.Host, Database.Name, paramSep, Database.Charset, tls)
120116
case "postgres":
121-
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, Param, Database.SSLMode)
117+
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, paramSep, Database.SSLMode)
122118
case "mssql":
123119
host, port := ParseMSSQLHostPort(Database.Host)
124120
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, Database.Name, Database.User, Database.Passwd)
125121
case "sqlite3":
126122
if !EnableSQLite3 {
127-
return "", errors.New("this binary version does not build support for SQLite3")
123+
return "", errors.New("this Gitea binary was not built with SQLite3 support")
128124
}
129125
if err := os.MkdirAll(path.Dir(Database.Path), os.ModePerm); err != nil {
130126
return "", fmt.Errorf("Failed to create directories: %w", err)
@@ -136,7 +132,7 @@ func DBConnStr() (string, error) {
136132
connStr = fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d&_txlock=immediate%s",
137133
Database.Path, Database.Timeout, journalMode)
138134
default:
139-
return "", fmt.Errorf("Unknown database type: %s", Database.Type)
135+
return "", fmt.Errorf("unknown database type: %s", Database.Type)
140136
}
141137

142138
return connStr, nil

options/locale/locale_en-US.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,9 @@ host = Host
192192
user = Username
193193
password = Password
194194
db_name = Database Name
195-
db_helper = Note to MySQL users: please use the InnoDB storage engine and if you use "utf8mb4", your InnoDB version must be greater than 5.6 .
196195
db_schema = Schema
197196
db_schema_helper = Leave blank for database default ("public").
198197
ssl_mode = SSL
199-
charset = Charset
200198
path = Path
201199
sqlite_helper = File path for the SQLite3 database.<br>Enter an absolute path if you run Gitea as a service.
202200
reinstall_error = You are trying to install into an existing Gitea database

routers/install/install.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func Install(ctx *context.Context) {
9898
form.DbName = setting.Database.Name
9999
form.DbPath = setting.Database.Path
100100
form.DbSchema = setting.Database.Schema
101-
form.Charset = setting.Database.Charset
102101

103102
curDBType := setting.Database.Type.String()
104103
var isCurDBTypeSupported bool
@@ -268,7 +267,6 @@ func SubmitInstall(ctx *context.Context) {
268267
setting.Database.Name = form.DbName
269268
setting.Database.Schema = form.DbSchema
270269
setting.Database.SSLMode = form.SSLMode
271-
setting.Database.Charset = form.Charset
272270
setting.Database.Path = form.DbPath
273271
setting.Database.LogSQL = !setting.IsProd
274272

@@ -382,7 +380,6 @@ func SubmitInstall(ctx *context.Context) {
382380
cfg.Section("database").Key("PASSWD").SetValue(setting.Database.Passwd)
383381
cfg.Section("database").Key("SCHEMA").SetValue(setting.Database.Schema)
384382
cfg.Section("database").Key("SSL_MODE").SetValue(setting.Database.SSLMode)
385-
cfg.Section("database").Key("CHARSET").SetValue(setting.Database.Charset)
386383
cfg.Section("database").Key("PATH").SetValue(setting.Database.Path)
387384
cfg.Section("database").Key("LOG_SQL").SetValue("false") // LOG_SQL is rarely helpful
388385

services/forms/user_form.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type InstallForm struct {
2727
DbPasswd string
2828
DbName string
2929
SSLMode string
30-
Charset string `binding:"Required;In(utf8,utf8mb4)"`
3130
DbPath string
3231
DbSchema string
3332

templates/install.tmpl

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
<div class="inline required field {{if .Err_DbSetting}}error{{end}}">
4545
<label for="db_name">{{.locale.Tr "install.db_name"}}</label>
4646
<input id="db_name" name="db_name" value="{{.db_name}}">
47-
<span class="help">{{.locale.Tr "install.db_helper"}}</span>
4847
</div>
4948
</div>
5049

@@ -69,20 +68,6 @@
6968
</div>
7069
</div>
7170

72-
<div class="gt-hidden" data-db-setting-for="mysql">
73-
<div class="inline required field">
74-
<label>{{.locale.Tr "install.charset"}}</label>
75-
<div class="ui selection database type dropdown">
76-
<input type="hidden" name="charset" value="{{if .charset}}{{.charset}}{{else}}utf8mb4{{end}}">
77-
<div class="default text">utf8mb4</div>
78-
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
79-
<div class="menu">
80-
<div class="item" data-value="utf8mb4">utf8mb4</div>
81-
</div>
82-
</div>
83-
</div>
84-
</div>
85-
8671
<div class="gt-hidden" data-db-setting-for="sqlite3">
8772
<div class="inline required field {{if or .Err_DbPath .Err_DbSetting}}error{{end}}">
8873
<label for="db_path">{{.locale.Tr "install.path"}}</label>

0 commit comments

Comments
 (0)