Skip to content

Commit

Permalink
fix: failed create record if use mysql (close #1690)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 16, 2022
1 parent 1af9f40 commit d329df7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 0 additions & 3 deletions internal/bootstrap/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ 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":
{
Expand Down
8 changes: 7 additions & 1 deletion internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"log"

"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"gorm.io/gorm"
)
Expand All @@ -11,7 +12,12 @@ var db *gorm.DB

func Init(d *gorm.DB) {
db = d
err := db.AutoMigrate(new(model.Storage), new(model.User), new(model.Meta), new(model.SettingItem))
var err error
if conf.Conf.Database.Type == "mysql" {
err = db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").AutoMigrate(new(model.Storage), new(model.User), new(model.Meta), new(model.SettingItem))
} else {
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())
}
Expand Down

0 comments on commit d329df7

Please sign in to comment.