🔖 chore: Modify the key field data type in the channel table.

This commit is contained in:
MartialBE 2024-05-15 22:15:27 +08:00
parent d85924aa95
commit ef63fbfd31
No known key found for this signature in database
GPG Key ID: 27C0267EC84B0A5C
5 changed files with 52 additions and 5 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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"`

View File

@ -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

45
model/migrate.go Normal file
View File

@ -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()
}