Skip to content

Use "utf8mb4" for MySQL by default #25432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions modules/setting/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ func LoadDBSetting() {
func loadDBSetting(rootCfg ConfigProvider) {
sec := rootCfg.Section("database")
Database.Type = DatabaseType(sec.Key("DB_TYPE").String())
defaultCharset := "utf8"

if Database.Type.IsMySQL() {
defaultCharset = "utf8mb4"
}

Database.Host = sec.Key("HOST").String()
Database.Name = sec.Key("NAME").String()
Expand All @@ -74,9 +69,10 @@ func loadDBSetting(rootCfg ConfigProvider) {
}
Database.Schema = sec.Key("SCHEMA").String()
Database.SSLMode = sec.Key("SSL_MODE").MustString("disable")
Database.Charset = sec.Key("CHARSET").In(defaultCharset, []string{"utf8", "utf8mb4"})
if Database.Type.IsMySQL() && defaultCharset != "utf8mb4" {
log.Error("Deprecated database mysql charset utf8 support, please use utf8mb4 or convert utf8 to utf8mb4.")

Database.Charset = sec.Key("CHARSET").MustString("utf8mb4")
if Database.Type.IsMySQL() && Database.Charset != "utf8mb4" {
log.Error(`Deprecated database mysql charset utf8 support, please use utf8mb4 and convert utf8 database to utf8mb4 by "gitea convert".`)
}

Database.Path = sec.Key("PATH").MustString(filepath.Join(AppDataPath, "gitea.db"))
Expand All @@ -101,9 +97,9 @@ func loadDBSetting(rootCfg ConfigProvider) {
// DBConnStr returns database connection string
func DBConnStr() (string, error) {
var connStr string
Param := "?"
if strings.Contains(Database.Name, Param) {
Param = "&"
paramSep := "?"
if strings.Contains(Database.Name, paramSep) {
paramSep = "&"
}
switch Database.Type {
case "mysql":
Expand All @@ -116,15 +112,15 @@ func DBConnStr() (string, error) {
tls = "false"
}
connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s",
Database.User, Database.Passwd, connType, Database.Host, Database.Name, Param, Database.Charset, tls)
Database.User, Database.Passwd, connType, Database.Host, Database.Name, paramSep, Database.Charset, tls)
case "postgres":
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, Param, Database.SSLMode)
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, paramSep, Database.SSLMode)
case "mssql":
host, port := ParseMSSQLHostPort(Database.Host)
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, Database.Name, Database.User, Database.Passwd)
case "sqlite3":
if !EnableSQLite3 {
return "", errors.New("this binary version does not build support for SQLite3")
return "", errors.New("this Gitea binary was not built with SQLite3 support")
}
if err := os.MkdirAll(path.Dir(Database.Path), os.ModePerm); err != nil {
return "", fmt.Errorf("Failed to create directories: %w", err)
Expand All @@ -136,7 +132,7 @@ func DBConnStr() (string, error) {
connStr = fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d&_txlock=immediate%s",
Database.Path, Database.Timeout, journalMode)
default:
return "", fmt.Errorf("Unknown database type: %s", Database.Type)
return "", fmt.Errorf("unknown database type: %s", Database.Type)
}

return connStr, nil
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,9 @@ host = Host
user = Username
password = Password
db_name = Database Name
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 .
db_schema = Schema
db_schema_helper = Leave blank for database default ("public").
ssl_mode = SSL
charset = Charset
path = Path
sqlite_helper = File path for the SQLite3 database.<br>Enter an absolute path if you run Gitea as a service.
reinstall_error = You are trying to install into an existing Gitea database
Expand Down
3 changes: 0 additions & 3 deletions routers/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func Install(ctx *context.Context) {
form.DbName = setting.Database.Name
form.DbPath = setting.Database.Path
form.DbSchema = setting.Database.Schema
form.Charset = setting.Database.Charset

curDBType := setting.Database.Type.String()
var isCurDBTypeSupported bool
Expand Down Expand Up @@ -268,7 +267,6 @@ func SubmitInstall(ctx *context.Context) {
setting.Database.Name = form.DbName
setting.Database.Schema = form.DbSchema
setting.Database.SSLMode = form.SSLMode
setting.Database.Charset = form.Charset
setting.Database.Path = form.DbPath
setting.Database.LogSQL = !setting.IsProd

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

Expand Down
1 change: 0 additions & 1 deletion services/forms/user_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type InstallForm struct {
DbPasswd string
DbName string
SSLMode string
Charset string `binding:"Required;In(utf8,utf8mb4)"`
DbPath string
DbSchema string

Expand Down
15 changes: 0 additions & 15 deletions templates/install.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<div class="inline required field {{if .Err_DbSetting}}error{{end}}">
<label for="db_name">{{.locale.Tr "install.db_name"}}</label>
<input id="db_name" name="db_name" value="{{.db_name}}">
<span class="help">{{.locale.Tr "install.db_helper"}}</span>
</div>
</div>

Expand All @@ -69,20 +68,6 @@
</div>
</div>

<div class="gt-hidden" data-db-setting-for="mysql">
<div class="inline required field">
<label>{{.locale.Tr "install.charset"}}</label>
<div class="ui selection database type dropdown">
<input type="hidden" name="charset" value="{{if .charset}}{{.charset}}{{else}}utf8mb4{{end}}">
<div class="default text">utf8mb4</div>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu">
<div class="item" data-value="utf8mb4">utf8mb4</div>
</div>
</div>
</div>
</div>

<div class="gt-hidden" data-db-setting-for="sqlite3">
<div class="inline required field {{if or .Err_DbPath .Err_DbSetting}}error{{end}}">
<label for="db_path">{{.locale.Tr "install.path"}}</label>
Expand Down