Skip to content

Commit

Permalink
🔖 chore: Modify the key field data type in the channel table.
Browse files Browse the repository at this point in the history
  • Loading branch information
MartialBE committed May 15, 2024
1 parent d85924a commit ef63fbf
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion model/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
7 changes: 3 additions & 4 deletions model/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions model/migrate.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit ef63fbf

Please sign in to comment.