Skip to content

Commit 2df7563

Browse files
authored
Recommend/convert to use case-sensitive collation for MySQL/MSSQL (#28662)
Mainly for MySQL/MSSQL. It is important for Gitea to use case-sensitive database charset collation. If the database is using a case-insensitive collation, Gitea will show startup error/warning messages, and show the errors/warnings on the admin panel's Self-Check page. Make `gitea doctor convert` work for MySQL to convert the collations of database & tables & columns. * Fix #28131 ## ⚠️ BREAKING ⚠️ It is not quite breaking, but it's highly recommended to convert the database&table&column to a consistent and case-sensitive collation.
1 parent a80debc commit 2df7563

File tree

21 files changed

+439
-186
lines changed

21 files changed

+439
-186
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _test
1111
.idea
1212
# Goland's output filename can not be set manually
1313
/go_build_*
14+
/gitea_*
1415

1516
# MS VSCode
1617
.vscode

cmd/doctor_convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func runDoctorConvert(ctx *cli.Context) error {
3737

3838
switch {
3939
case setting.Database.Type.IsMySQL():
40-
if err := db.ConvertUtf8ToUtf8mb4(); err != nil {
41-
log.Fatal("Failed to convert database from utf8 to utf8mb4: %v", err)
40+
if err := db.ConvertDatabaseTable(); err != nil {
41+
log.Fatal("Failed to convert database & table: %v", err)
4242
return err
4343
}
4444
fmt.Println("Converted successfully, please confirm your database's character set is now utf8mb4")

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ NAME = gitea
351351
USER = root
352352
;PASSWD = ;Use PASSWD = `your password` for quoting if you use special characters in the password.
353353
;SSL_MODE = false ; either "false" (default), "true", or "skip-verify"
354+
;CHARSET_COLLATION = ; Empty as default, Gitea will try to find a case-sensitive collation. Don't change it unless you clearly know what you need.
354355
;;
355356
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
356357
;;
@@ -382,6 +383,7 @@ USER = root
382383
;NAME = gitea
383384
;USER = SA
384385
;PASSWD = MwantsaSecurePassword1
386+
;CHARSET_COLLATION = ; Empty as default, Gitea will try to find a case-sensitive collation. Don't change it unless you clearly know what you need.
385387
;;
386388
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387389
;;

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ The following configuration set `Content-Type: application/vnd.android.package-a
430430
- `NAME`: **gitea**: Database name.
431431
- `USER`: **root**: Database username.
432432
- `PASSWD`: **_empty_**: Database user password. Use \`your password\` or """your password""" for quoting if you use special characters in the password.
433+
- `CHARSET_COLLATION`: **_empty_**: (MySQL/MSSQL only) Gitea expects to use a case-sensitive collation for database. Leave it empty to use the default collation decided by the Gitea. Don't change it unless you clearly know what you need.
433434
- `SCHEMA`: **_empty_**: For PostgreSQL only, schema to use if different from "public". The schema must exist beforehand,
434435
the user must have creation privileges on it, and the user search path must be set to the look into the schema first
435436
(e.g. `ALTER USER user SET SEARCH_PATH = schema_name,"$user",public;`).

docs/content/help/faq.en-us.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -371,24 +371,6 @@ If you are receiving an error line containing `Error 1071: Specified key was too
371371
then you are attempting to run Gitea on tables which use the ISAM engine. While this may have worked by chance in previous versions of Gitea, it has never been officially supported and
372372
you must use InnoDB. You should run `ALTER TABLE table_name ENGINE=InnoDB;` for each table in the database.
373373

374-
If you are using MySQL 5, another possible fix is
375-
376-
```mysql
377-
SET GLOBAL innodb_file_format=Barracuda;
378-
SET GLOBAL innodb_file_per_table=1;
379-
SET GLOBAL innodb_large_prefix=1;
380-
```
381-
382-
## Why Are Emoji Broken On MySQL
383-
384-
Unfortunately MySQL's `utf8` charset does not completely allow all possible UTF-8 characters, in particular Emoji.
385-
They created a new charset and collation called `utf8mb4` that allows for emoji to be stored but tables which use
386-
the `utf8` charset, and connections which use the `utf8` charset will not use this.
387-
388-
Please run `gitea doctor convert`, or run `ALTER DATABASE database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;`
389-
for the database_name and run `ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;`
390-
for each table in the database.
391-
392374
## Why are Emoji displaying only as placeholders or in monochrome
393375

394376
Gitea requires the system or browser to have one of the supported Emoji fonts installed, which are Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji and Twemoji Mozilla. Generally, the operating system should already provide one of these fonts, but especially on Linux, it may be necessary to install them manually.

docs/content/help/faq.zh-cn.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -375,25 +375,6 @@ Gitea 提供了一个子命令`gitea migrate`来初始化数据库,然后您
375375
的错误行,则表示您正在尝试在使用 ISAM 引擎的表上运行 Gitea。尽管在先前版本的 Gitea 中可能是凑巧能够工作的,但它从未得到官方支持,
376376
您必须使用 InnoDB。您应该对数据库中的每个表运行`ALTER TABLE table_name ENGINE=InnoDB;`
377377

378-
如果您使用的是 MySQL 5,另一个可能的修复方法是:
379-
380-
```mysql
381-
SET GLOBAL innodb_file_format=Barracuda;
382-
SET GLOBAL innodb_file_per_table=1;
383-
SET GLOBAL innodb_large_prefix=1;
384-
```
385-
386-
## 为什么 MySQL 上的 Emoji 显示错误
387-
388-
不幸的是,MySQL 的`utf8`字符集不完全允许所有可能的 UTF-8 字符,特别是 Emoji。
389-
他们创建了一个名为 `utf8mb4`的字符集和校对规则,允许存储 Emoji,但使用
390-
utf8 字符集的表和连接将不会使用它。
391-
392-
请运行 `gitea doctor convert` 或对数据库运行 `ALTER DATABASE database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;`
393-
并对每个表运行 `ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;`
394-
395-
您还需要将`app.ini`文件中的数据库字符集设置为`CHARSET=utf8mb4`
396-
397378
## 为什么 Emoji 只显示占位符或单色图像
398379

399380
Gitea 需要系统或浏览器安装其中一个受支持的 Emoji 字体,例如 Apple Color Emoji、Segoe UI Emoji、Segoe UI Symbol、Noto Color Emoji 和 Twemoji Mozilla。通常,操作系统应该已经提供了其中一个字体,但特别是在 Linux 上,可能需要手动安装它们。

docs/content/installation/database-preparation.en-us.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ Note: All steps below requires that the database engine of your choice is instal
6161

6262
Replace username and password above as appropriate.
6363

64-
4. Create database with UTF-8 charset and collation. Make sure to use `utf8mb4` charset instead of `utf8` as the former supports all Unicode characters (including emojis) beyond _Basic Multilingual Plane_. Also, collation chosen depending on your expected content. When in doubt, use either `unicode_ci` or `general_ci`.
64+
4. Create database with UTF-8 charset and case-sensitive collation.
65+
66+
`utf8mb4_bin` is a common collation for both MySQL/MariaDB.
67+
When Gitea starts, it will try to find a better collation (`utf8mb4_0900_as_cs` or `uca1400_as_cs`) and alter the database if it is possible.
68+
If you would like to use other collation, you can set `[database].CHARSET_COLLATION` in the `app.ini` file.
6569

6670
```sql
67-
CREATE DATABASE giteadb CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
71+
CREATE DATABASE giteadb CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin';
6872
```
6973

7074
Replace database name as appropriate.

docs/content/installation/database-preparation.zh-cn.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ menu:
5959

6060
根据需要替换上述用户名和密码。
6161

62-
4. 使用 UTF-8 字符集和排序规则创建数据库。确保使用 `**utf8mb4**` 字符集,而不是 `utf8`,因为前者支持 _Basic Multilingual Plane_ 之外的所有 Unicode 字符(包括表情符号)。排序规则根据您预期的内容选择。如果不确定,可以使用 `unicode_ci` 或 `general_ci`。
62+
4. 使用 UTF-8 字符集和大小写敏感的排序规则创建数据库。
63+
64+
Gitea 启动后会尝试把数据库修改为更合适的字符集,如果你想指定自己的字符集规则,可以在 app.ini 中设置 `[database].CHARSET_COLLATION`。
6365

6466
```sql
65-
CREATE DATABASE giteadb CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
67+
CREATE DATABASE giteadb CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin';
6668
```
6769

6870
根据需要替换数据库名称。

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ require (
121121
mvdan.cc/xurls/v2 v2.5.0
122122
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
123123
xorm.io/builder v0.3.13
124-
xorm.io/xorm v1.3.6
124+
xorm.io/xorm v1.3.7-0.20240101024435-4992cba040fe
125125
)
126126

127127
require (

0 commit comments

Comments
 (0)