diff --git a/go.mod b/go.mod index 6e4afad65..458edc0cc 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( filippo.io/edwards25519 v1.1.0 // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/go-gormigrate/gormigrate/v2 v2.1.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jonboulle/clockwork v0.4.0 // indirect diff --git a/go.sum b/go.sum index 85532af65..629b0b485 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SU github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= github.com/go-co-op/gocron/v2 v2.2.9 h1:aoKosYWSSdXFLecjFWX1i8+R6V7XdZb8sB2ZKAY5Yis= github.com/go-co-op/gocron/v2 v2.2.9/go.mod h1:mZx3gMSlFnb97k3hRqX3+GdlG3+DUwTh6B8fnsTScXg= +github.com/go-gormigrate/gormigrate/v2 v2.1.2 h1:F/d1hpHbRAvKezziV2CC5KUE82cVe9zTgHSBoOOZ4CY= +github.com/go-gormigrate/gormigrate/v2 v2.1.2/go.mod h1:9nHVX6z3FCMCQPA7PThGcA55t22yKQfK/Dnsf5i7hUo= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= diff --git a/model/channel.go b/model/channel.go index 7f6eb615c..6469b536d 100644 --- a/model/channel.go +++ b/model/channel.go @@ -11,7 +11,7 @@ import ( type Channel struct { Id int `json:"id"` Type int `json:"type" form:"type" gorm:"default:0"` - Key string `json:"key" form:"key" gorm:"type:varchar(767);not null;index"` + Key string `json:"key" form:"key" gorm:"type:text"` Status int `json:"status" form:"status" gorm:"default:1"` Name string `json:"name" form:"name" gorm:"index"` Weight *uint `json:"weight" gorm:"default:1"` diff --git a/model/main.go b/model/main.go index a826f6e78..6e5ca2244 100644 --- a/model/main.go +++ b/model/main.go @@ -104,10 +104,9 @@ func InitDB() (err error) { return nil } common.SysLog("database migration started") - // err = MigrateDB(DB) - // if err != nil { - // return err - // } + + migration(DB) + err = db.AutoMigrate(&Channel{}) if err != nil { return err diff --git a/model/migrate.go b/model/migrate.go new file mode 100644 index 000000000..148e4880a --- /dev/null +++ b/model/migrate.go @@ -0,0 +1,45 @@ +package model + +import ( + "one-api/common" + + "github.com/go-gormigrate/gormigrate/v2" + "gorm.io/gorm" +) + +func removeKeyIndexMigration() *gormigrate.Migration { + return &gormigrate.Migration{ + ID: "202405152141", + Migrate: func(tx *gorm.DB) error { + dialect := tx.Dialector.Name() + if dialect == "sqlite" { + return nil + } + + if !tx.Migrator().HasIndex(&Channel{}, "idx_channels_key") { + return nil + } + + err := tx.Migrator().DropIndex(&Channel{}, "idx_channels_key") + if err != nil { + common.SysLog("remove idx_channels_key Failure: " + err.Error()) + } + return nil + }, + Rollback: func(tx *gorm.DB) error { + return nil + }, + } +} + +func migration(db *gorm.DB) error { + // 如果是第一次运行 直接跳过 + if !db.Migrator().HasTable("channels") { + return nil + } + + m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{ + removeKeyIndexMigration(), + }) + return m.Migrate() +}