diff --git a/.drone.yml b/.drone.yml index 5096ce781fe20..923c209f8f9cb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 @@ -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 diff --git a/models/db/engine.go b/models/db/engine.go index 56dd209fd78ff..4a2d8452a0b47 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -12,6 +12,7 @@ import ( "reflect" "strings" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "xorm.io/xorm" @@ -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") + } + return nil +} + // SetDefaultEngine sets the default engine for db func SetDefaultEngine(ctx context.Context, eng *xorm.Engine) { x = eng @@ -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. //