Skip to content

Check mysql charset, if it's utf8, display an error log #24392

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ services:
- name: mysql
image: mysql:5.7
pull: always
entrypoint: [ "mysqld" ]
command: [ "--character-set-server=utf8mb4", "--collation-server=utf8mb4_general_ci" ]
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: test
Expand Down Expand Up @@ -484,6 +486,8 @@ services:
- name: mysql8
image: mysql:8
pull: always
entrypoint: [ "mysqld" ]
command: [ "--character-set-server=utf8mb4", "--collation-server=utf8mb4_general_ci" ]
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: testgitea
Expand Down
24 changes: 24 additions & 0 deletions models/db/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"reflect"
"strings"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

"xorm.io/xorm"
Expand Down Expand Up @@ -147,6 +148,25 @@ func InitEngine(ctx context.Context) error {
return nil
}

func checkCharset(ctx context.Context) error {
if !setting.Database.Type.IsMySQL() {
return nil
}
var dbCharset string
_, err := GetEngine(ctx).SQL("SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = ?",
setting.Database.Name).Get(&dbCharset)
if err != nil {
return err
}
if dbCharset != setting.Database.Charset {
return fmt.Errorf("database setting charset is %s but in fact it's %s", setting.Database.Charset, dbCharset)
}
if dbCharset == "utf8" {
log.Error("database charset is utf8 which is deprecated, please convert it to utf8mb4 and change your app.ini [database]charset")
}
Copy link
Member

@silverwind silverwind Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also match for utf8mb3 which is an alias to utf8 and interpolate charset into the message.

Reference: https://dev.mysql.com/doc/refman/8.0/en/charset-unicode-utf8.html

return nil
}

// SetDefaultEngine sets the default engine for db
func SetDefaultEngine(ctx context.Context, eng *xorm.Engine) {
x = eng
Expand Down Expand Up @@ -182,6 +202,10 @@ func InitEngineWithMigration(ctx context.Context, migrateFunc func(*xorm.Engine)
return err
}

if err := checkCharset(ctx); err != nil {
return err
}

// We have to run migrateFunc here in case the user is re-running installation on a previously created DB.
// If we do not then table schemas will be changed and there will be conflicts when the migrations run properly.
//
Expand Down