Skip to content

Commit

Permalink
feat: 优化迁移
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jun 24, 2024
1 parent 0970376 commit 05674ee
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
20 changes: 2 additions & 18 deletions app/providers/database_service_provider.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package providers

import (
"fmt"

"github.com/goravel/framework/contracts/database/seeder"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/database/gorm"
"github.com/goravel/framework/facades"

"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/pkg/migrate"
)

type DatabaseServiceProvider struct {
Expand All @@ -20,19 +18,5 @@ func (receiver *DatabaseServiceProvider) Register(app foundation.Application) {

func (receiver *DatabaseServiceProvider) Boot(app foundation.Application) {
facades.Seeder().Register([]seeder.Seeder{})
if err := facades.Orm().Query().(*gorm.QueryImpl).Instance().AutoMigrate(
&models.Cert{},
&models.CertDNS{},
&models.CertUser{},
&models.Cron{},
&models.Database{},
&models.Monitor{},
&models.Plugin{},
&models.Setting{},
&models.Task{},
&models.User{},
&models.Website{},
); err != nil {
panic(fmt.Sprintf("Failed to migrate database: %v", err))
}
migrate.Migrate(facades.Orm().Query().(*gorm.QueryImpl).Instance())
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.22
require (
github.com/docker/docker v26.1.4+incompatible
github.com/docker/go-connections v0.5.0
github.com/go-gormigrate/gormigrate/v2 v2.1.2
github.com/go-resty/resty/v2 v2.13.1
github.com/go-sql-driver/mysql v1.8.1
github.com/gookit/validate v1.5.2
github.com/goravel/framework v1.14.1-0.20240618022250-731b8d9930a3
github.com/goravel/gin v1.2.1
Expand All @@ -26,6 +28,7 @@ require (
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.24.0
golang.org/x/net v0.26.0
gorm.io/gorm v1.25.10
)

require (
Expand Down Expand Up @@ -87,7 +90,6 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
Expand Down Expand Up @@ -203,7 +205,6 @@ require (
gorm.io/driver/mysql v1.5.7 // indirect
gorm.io/driver/postgres v1.5.9 // indirect
gorm.io/driver/sqlserver v1.5.3 // indirect
gorm.io/gorm v1.25.10 // indirect
gorm.io/plugin/dbresolver v1.5.2 // indirect
gotest.tools/v3 v3.5.0 // indirect
modernc.org/libc v1.37.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
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-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down
22 changes: 22 additions & 0 deletions pkg/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package migrate

import (
"fmt"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

func Migrate(db *gorm.DB) {
options := &gormigrate.Options{
TableName: "new_migrations",
IDColumnName: "id",
IDColumnSize: 255,
}
migrator := gormigrate.New(db, options, []*gormigrate.Migration{
Init,
})
if err := migrator.Migrate(); err != nil {
panic(fmt.Sprintf("Failed to migrate database: %v", err))
}
}
42 changes: 42 additions & 0 deletions pkg/migrate/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package migrate

import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"

"github.com/TheTNB/panel/app/models"
)

var Init = &gormigrate.Migration{
ID: "20240624-init",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(
&models.Cert{},
&models.CertDNS{},
&models.CertUser{},
&models.Cron{},
&models.Database{},
&models.Monitor{},
&models.Plugin{},
&models.Setting{},
&models.Task{},
&models.User{},
&models.Website{},
)
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable(
&models.Cert{},
&models.CertDNS{},
&models.CertUser{},
&models.Cron{},
&models.Database{},
&models.Monitor{},
&models.Plugin{},
&models.Setting{},
&models.Task{},
&models.User{},
&models.Website{},
)
},
}

0 comments on commit 05674ee

Please sign in to comment.