Skip to content

Commit d5a98a2

Browse files
lunnytechknowlogick
authored andcommitted
Add support of utf8mb4 for mysql (#6992)
1 parent 181b7c9 commit d5a98a2

File tree

9 files changed

+34
-10
lines changed

9 files changed

+34
-10
lines changed

custom/conf/app.ini.sample

+3
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ PASSWD =
260260
; For Postgres, either "disable" (default), "require", or "verify-full"
261261
; For MySQL, either "false" (default), "true", or "skip-verify"
262262
SSL_MODE = disable
263+
; For MySQL only, either "utf8" or "utf8mb4", default is "utf8".
264+
; NOTICE: for "utf8mb4" you must use MySQL InnoDB > 5.6. Gitea is unable to check this.
265+
CHARSET = utf8
263266
; For "sqlite3" and "tidb", use an absolute path when you start gitea as service
264267
PATH = data/gitea.db
265268
; For "sqlite3" only. Query timeout

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

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
160160
- `USER`: **root**: Database username.
161161
- `PASSWD`: **\<empty\>**: Database user password. Use \`your password\` for quoting if you use special characters in the password.
162162
- `SSL_MODE`: **disable**: For PostgreSQL and MySQL only.
163+
- `CHARSET`: **utf8**: For MySQL only, either "utf8" or "utf8mb4", default is "utf8". NOTICE: for "utf8mb4" you must use MySQL InnoDB > 5.6. Gitea is unable to check this.
163164
- `PATH`: **data/gitea.db**: For SQLite3 only, the database file path.
164165
- `LOG_SQL`: **true**: Log the executed SQL.
165166
- `DB_RETRIES`: **10**: How many ORM init / DB connect attempts allowed.

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ menu:
7878
- `NAME`: 数据库名称。
7979
- `USER`: 数据库用户名。
8080
- `PASSWD`: 数据库用户密码。
81-
- `SSL_MODE`: PostgreSQL数据库是否启用SSL模式。
81+
- `SSL_MODE`: MySQL 或 PostgreSQL数据库是否启用SSL模式。
82+
- `CHARSET`: **utf8**: 仅当数据库为 MySQL 时有效, 可以为 "utf8" 或 "utf8mb4"。注意:如果使用 "utf8mb4",你的 MySQL InnoDB 版本必须在 5.6 以上。
8283
- `PATH`: Tidb 或者 SQLite3 数据文件存放路径。
8384
- `LOG_SQL`: **true**: 显示生成的SQL,默认为真。
8485

models/models.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ var (
5959

6060
// DbCfg holds the database settings
6161
DbCfg struct {
62-
Type, Host, Name, User, Passwd, Path, SSLMode string
63-
Timeout int
62+
Type, Host, Name, User, Passwd, Path, SSLMode, Charset string
63+
Timeout int
6464
}
6565

6666
// EnableSQLite3 use SQLite3
@@ -160,6 +160,7 @@ func LoadConfigs() {
160160
DbCfg.Passwd = sec.Key("PASSWD").String()
161161
}
162162
DbCfg.SSLMode = sec.Key("SSL_MODE").MustString("disable")
163+
DbCfg.Charset = sec.Key("CHARSET").In("utf8", []string{"utf8", "utf8mb4"})
163164
DbCfg.Path = sec.Key("PATH").MustString(filepath.Join(setting.AppDataPath, "gitea.db"))
164165
DbCfg.Timeout = sec.Key("SQLITE_TIMEOUT").MustInt(500)
165166
}
@@ -222,8 +223,8 @@ func getEngine() (*xorm.Engine, error) {
222223
if tls == "disable" { // allow (Postgres-inspired) default value to work in MySQL
223224
tls = "false"
224225
}
225-
connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=utf8&parseTime=true&tls=%s",
226-
DbCfg.User, DbCfg.Passwd, connType, DbCfg.Host, DbCfg.Name, Param, tls)
226+
connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s",
227+
DbCfg.User, DbCfg.Passwd, connType, DbCfg.Host, DbCfg.Name, Param, DbCfg.Charset, tls)
227228
case "postgres":
228229
connStr = getPostgreSQLConnectionString(DbCfg.Host, DbCfg.User, DbCfg.Passwd, DbCfg.Name, Param, DbCfg.SSLMode)
229230
case "mssql":

modules/auth/user_form.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type InstallForm struct {
2323
DbPasswd string
2424
DbName string
2525
SSLMode string
26+
Charset string `binding:"Required;In(utf8,utf8mb4)"`
2627
DbPath string
2728

2829
AppName string `binding:"Required" locale:"install.app_name"`

options/locale/locale_en-US.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ host = Host
8686
user = Username
8787
password = Password
8888
db_name = Database Name
89-
db_helper = Note to MySQL users: please use the InnoDB storage engine and the 'utf8_general_ci' character set.
89+
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 .
9090
ssl_mode = SSL
91+
charset = Charset
9192
path = Path
9293
sqlite_helper = File path for the SQLite3 database.<br>Enter an absolute path if you run Gitea as a service.
9394
err_empty_db_path = The SQLite3 database path cannot be empty.

public/js/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,14 @@ function initInstall() {
587587
var tidbDefault = 'data/gitea_tidb';
588588

589589
var dbType = $(this).val();
590-
if (dbType === "SQLite3" || dbType === "TiDB") {
590+
if (dbType === "SQLite3") {
591591
$('#sql_settings').hide();
592592
$('#pgsql_settings').hide();
593+
$('#mysql_settings').hide();
593594
$('#sqlite_settings').show();
594595

595596
if (dbType === "SQLite3" && $('#db_path').val() == tidbDefault) {
596597
$('#db_path').val(sqliteDefault);
597-
} else if (dbType === "TiDB" && $('#db_path').val() == sqliteDefault) {
598-
$('#db_path').val(tidbDefault);
599598
}
600599
return;
601600
}
@@ -610,6 +609,7 @@ function initInstall() {
610609
$('#sql_settings').show();
611610

612611
$('#pgsql_settings').toggle(dbType === "PostgreSQL");
612+
$('#mysql_settings').toggle(dbType === "MySQL");
613613
$.each(dbDefaults, function(_type, defaultHost) {
614614
if ($('#db_host').val() == defaultHost) {
615615
$('#db_host').val(dbDefaults[dbType]);

routers/install.go

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
150150
models.DbCfg.Passwd = form.DbPasswd
151151
models.DbCfg.Name = form.DbName
152152
models.DbCfg.SSLMode = form.SSLMode
153+
models.DbCfg.Charset = form.Charset
153154
models.DbCfg.Path = form.DbPath
154155

155156
if (models.DbCfg.Type == "sqlite3") &&

templates/install.tmpl

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</div>
2929
</div>
3030

31-
<div id="sql_settings" class="{{if or (eq .CurDbOption "SQLite3") (eq .CurDbOption "TiDB")}}hide{{end}}">
31+
<div id="sql_settings" class="{{if or (eq .CurDbOption "SQLite3")}}hide{{end}}">
3232
<div class="inline required field {{if .Err_DbSetting}}error{{end}}">
3333
<label for="db_host">{{.i18n.Tr "install.host"}}</label>
3434
<input id="db_host" name="db_host" value="{{.db_host}}">
@@ -64,6 +64,21 @@
6464
</div>
6565
</div>
6666

67+
<div id="mysql_settings" class="{{if not (eq .CurDbOption "MySQL")}}hide{{end}}">
68+
<div class="inline required field">
69+
<label>{{.i18n.Tr "install.charset"}}</label>
70+
<div class="ui selection database type dropdown">
71+
<input type="hidden" name="charset" value="{{if .charset}}{{.charset}}{{else}}utf8{{end}}">
72+
<div class="default text">utf8</div>
73+
<i class="dropdown icon"></i>
74+
<div class="menu">
75+
<div class="item" data-value="utf8">utf8</div>
76+
<div class="item" data-value="utf8mb4">utf8mb4</div>
77+
</div>
78+
</div>
79+
</div>
80+
</div>
81+
6782
<div id="sqlite_settings" class="{{if not (or (eq .CurDbOption "SQLite3") (eq .CurDbOption "TiDB"))}}hide{{end}}">
6883
<div class="inline required field {{if or .Err_DbPath .Err_DbSetting}}error{{end}}">
6984
<label for="db_path">{{.i18n.Tr "install.path"}}</label>

0 commit comments

Comments
 (0)