From 86a625cb405f0fc77a2ce1541c0e6deb203f26e4 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 15 Sep 2022 17:14:03 +0800 Subject: [PATCH] fix: set `CHARSET` to `utf8mb4` if use mysql --- internal/bootstrap/db.go | 3 +++ internal/db/db.go | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/bootstrap/db.go b/internal/bootstrap/db.go index 58d5209ba01..f80724ab687 100644 --- a/internal/bootstrap/db.go +++ b/internal/bootstrap/db.go @@ -53,6 +53,9 @@ func InitDB() { dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&tls=%s", database.User, database.Password, database.Host, database.Port, database.Name, database.SSLMode) dB, err = gorm.Open(mysql.Open(dsn), gormConfig) + if err == nil { + dB = dB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4") + } } case "postgres": { diff --git a/internal/db/db.go b/internal/db/db.go index 42be109d7b9..e836e01c574 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -7,10 +7,10 @@ import ( "gorm.io/gorm" ) -var db gorm.DB +var db *gorm.DB func Init(d *gorm.DB) { - db = *d + db = d err := db.AutoMigrate(new(model.Storage), new(model.User), new(model.Meta), new(model.SettingItem)) if err != nil { log.Fatalf("failed migrate database: %s", err.Error())