253 lines
8.3 KiB
Go
253 lines
8.3 KiB
Go
package patch
|
|
|
|
import (
|
|
"github.com/perfect-panel/ppanel-server/internal/model/ads"
|
|
"github.com/perfect-panel/ppanel-server/internal/model/application"
|
|
"github.com/perfect-panel/ppanel-server/internal/model/auth"
|
|
"github.com/perfect-panel/ppanel-server/internal/model/order"
|
|
"github.com/perfect-panel/ppanel-server/internal/model/payment"
|
|
"github.com/perfect-panel/ppanel-server/internal/model/server"
|
|
"github.com/perfect-panel/ppanel-server/internal/model/system"
|
|
"github.com/perfect-panel/ppanel-server/internal/model/user"
|
|
"github.com/perfect-panel/ppanel-server/pkg/logger"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func Migrate02000(db *gorm.DB) error {
|
|
version := "0.2.0(02000)"
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if err := initDeviceConfig(tx); err != nil {
|
|
logMigrationError("Setting Device Config", err)
|
|
return err
|
|
}
|
|
logMigrationSuccess("Setting Device Config")
|
|
|
|
if !tx.Migrator().HasTable(&ads.Ads{}) {
|
|
if err := createAdsTable(tx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := updatePaymentTable(tx); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := tx.Migrator().AutoMigrate(&order.Order{}); err != nil {
|
|
logMigrationError("Auto Migrate Order", err)
|
|
return err
|
|
}
|
|
|
|
return updateSystemVersion(tx, version)
|
|
})
|
|
}
|
|
|
|
func Migrate02001(db *gorm.DB) error {
|
|
version := "0.2.0(02001)"
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if tx.Model(&system.System{}).Where("`category` = 'site' AND `key` = 'CustomData'").Find(&system.System{}).RowsAffected == 0 {
|
|
if err := tx.Save(&system.System{
|
|
Category: "site",
|
|
Key: "CustomData",
|
|
Value: "{\"website\":\"\",\"contacts\":{\"email\":\"\",\"telephone\":\"\",\"address\":\"\"},\"community\":{\"telegram\":\"\",\"twitter\":\"\",\"discord\":\"\",\"instagram\":\"\",\"linkedin\":\"\",\"facebook\":\"\",\"github\":\"\"}}",
|
|
Type: "string",
|
|
Desc: "Custom data",
|
|
}).Error; err != nil {
|
|
logMigrationError("create custom data system config", err)
|
|
return err
|
|
}
|
|
}
|
|
return updateSystemVersion(tx, version)
|
|
})
|
|
}
|
|
|
|
func Migrate02002(db *gorm.DB) error {
|
|
version := "0.2.0(02002)"
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Model(&system.System{}).Where("`category` = 'site' AND `key` = 'CustomData'").UpdateColumn("type", "string").Error; err != nil {
|
|
return err
|
|
}
|
|
return updateSystemVersion(tx, version)
|
|
})
|
|
}
|
|
|
|
func Migrate02003(db *gorm.DB) error {
|
|
version := "0.2.0(02003)"
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if err := addColumnIfNotExists(tx, &order.Order{}, "payment_id"); err != nil {
|
|
return err
|
|
}
|
|
if err := addColumnIfNotExists(tx, &payment.Payment{}, "platform"); err != nil {
|
|
return err
|
|
}
|
|
if err := dropColumnIfExists(tx, &payment.Payment{}, "mark"); err != nil {
|
|
return err
|
|
}
|
|
if err := addColumnIfNotExists(tx, &payment.Payment{}, "description"); err != nil {
|
|
return err
|
|
}
|
|
if err := addColumnIfNotExists(tx, &payment.Payment{}, "token"); err != nil {
|
|
return err
|
|
}
|
|
return updateSystemVersion(tx, version)
|
|
})
|
|
}
|
|
|
|
func Migrate02007(db *gorm.DB) error {
|
|
version := "0.2.0(02007)"
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if err := recreateTable(tx, &server.RuleGroup{}); err != nil {
|
|
return err
|
|
}
|
|
return updateSystemVersion(tx, version)
|
|
})
|
|
}
|
|
|
|
func Migrate02008(db *gorm.DB) error {
|
|
version := "0.2.0(02008)"
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if exists := tx.Migrator().HasColumn(&application.ApplicationConfig{}, "invitation_link"); !exists {
|
|
if err := tx.Migrator().AddColumn(&application.ApplicationConfig{}, "invitation_link"); err != nil {
|
|
logger.Errorw("Migrate 02008 failed", logger.Field("action", "add invitation_link column"), logger.Field("error", err.Error()))
|
|
return err
|
|
}
|
|
logger.Infow("Migrate 02008 success", logger.Field("action", "add invitation_link column"))
|
|
}
|
|
|
|
if exists := tx.Migrator().HasTable(&user.DeviceOnlineRecord{}); !exists {
|
|
if err := tx.Migrator().CreateTable(&user.DeviceOnlineRecord{}); err != nil {
|
|
logger.Errorw("Migrate 02008 failed", logger.Field("action", "create device_online_record table"), logger.Field("error", err.Error()))
|
|
return err
|
|
}
|
|
}
|
|
return tx.Model(&system.System{}).Where("`category` = 'system' AND `key` = 'Version'").Update("value", version).Error
|
|
})
|
|
}
|
|
|
|
func Migrate02009(db *gorm.DB) error {
|
|
version := "0.2.0(02009)"
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if err := addColumnIfNotExists(tx, &user.Subscribe{}, "finished_at"); err != nil {
|
|
logger.Errorw("Migrate 02009 failed", logger.Field("action", "subscribe table add finished_at column"), logger.Field("error", err.Error()))
|
|
return err
|
|
}
|
|
return updateSystemVersion(tx, version)
|
|
})
|
|
}
|
|
|
|
func Migrate02010(db *gorm.DB) error {
|
|
version := "0.2.0(02010)"
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if err := addColumnIfNotExists(tx, &application.ApplicationConfig{}, "kr_website_id"); err != nil {
|
|
logger.Errorw("Migrate 02010 failed", logger.Field("action", "application_config table add kr_website_id column"), logger.Field("error", err.Error()))
|
|
return err
|
|
}
|
|
return updateSystemVersion(tx, version)
|
|
})
|
|
}
|
|
|
|
func Migrate02011(db *gorm.DB) error {
|
|
version := "0.2.0(02011)"
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if err := addColumnIfNotExists(tx, &user.Subscribe{}, "used_period"); err != nil {
|
|
logger.Errorw("Migrate 02011 failed", logger.Field("action", "user.Subscribe table add used_period column"), logger.Field("error", err.Error()))
|
|
return err
|
|
}
|
|
if err := addColumnIfNotExists(tx, &user.Subscribe{}, "total_period"); err != nil {
|
|
logger.Errorw("Migrate 02011 failed", logger.Field("action", "user.Subscribe table add total_period column"), logger.Field("error", err.Error()))
|
|
return err
|
|
}
|
|
return updateSystemVersion(tx, version)
|
|
})
|
|
}
|
|
|
|
func initDeviceConfig(db *gorm.DB) error {
|
|
cfg := new(auth.DeviceConfig)
|
|
return db.Model(&auth.Auth{}).Where("method = ?", "device").Update("config", cfg.Marshal()).Error
|
|
}
|
|
|
|
func createAdsTable(tx *gorm.DB) error {
|
|
if err := tx.Migrator().CreateTable(&ads.Ads{}); err != nil {
|
|
logMigrationError("Create Table Ads", err)
|
|
return err
|
|
}
|
|
logMigrationSuccess("Create Table Ads")
|
|
return tx.Model(&system.System{}).Save(&system.System{
|
|
Category: "ad",
|
|
Key: "WebAD",
|
|
Value: "false",
|
|
Type: "bool",
|
|
Desc: "Display ad on the web",
|
|
}).Error
|
|
}
|
|
|
|
func updatePaymentTable(tx *gorm.DB) error {
|
|
if err := tx.Exec("DROP TABLE IF EXISTS `payment`").Error; err != nil {
|
|
logMigrationError("Drop Payment Table", err)
|
|
}
|
|
if err := tx.AutoMigrate(&payment.Payment{}); err != nil {
|
|
logMigrationError("Auto Migrate Payment", err)
|
|
return err
|
|
}
|
|
enable := true
|
|
return tx.Model(&payment.Payment{}).Create(&payment.Payment{
|
|
Id: -1,
|
|
Name: "",
|
|
Platform: "balance",
|
|
Icon: "",
|
|
Domain: "",
|
|
Config: "",
|
|
FeeMode: 0,
|
|
FeePercent: 0,
|
|
FeeAmount: 0,
|
|
Enable: &enable,
|
|
}).Error
|
|
}
|
|
|
|
func updateSystemVersion(tx *gorm.DB, version string) error {
|
|
return tx.Model(&system.System{}).Where("`category` = 'system' AND `key` = 'Version'").Update("value", version).Error
|
|
}
|
|
|
|
func addColumnIfNotExists(tx *gorm.DB, model interface{}, columnName string) error {
|
|
if exists := tx.Migrator().HasColumn(model, columnName); !exists {
|
|
if err := tx.Migrator().AddColumn(model, columnName); err != nil {
|
|
logMigrationError("add "+columnName+" column", err)
|
|
return err
|
|
}
|
|
logMigrationSuccess("add " + columnName + " column")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func dropColumnIfExists(tx *gorm.DB, model interface{}, columnName string) error {
|
|
if exists := tx.Migrator().HasColumn(model, columnName); exists {
|
|
if err := tx.Migrator().DropColumn(model, columnName); err != nil {
|
|
logMigrationError("del "+columnName+" column", err)
|
|
return err
|
|
}
|
|
logMigrationSuccess("del " + columnName + " column")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func recreateTable(tx *gorm.DB, model interface{}) error {
|
|
if exists := tx.Migrator().HasTable(model); exists {
|
|
if err := tx.Migrator().DropTable(model); err != nil {
|
|
logMigrationError("drop table", err)
|
|
return err
|
|
}
|
|
}
|
|
if err := tx.Migrator().CreateTable(model); err != nil {
|
|
logMigrationError("create table", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func logMigrationError(action string, err error) {
|
|
logger.Errorw("Migration failed", logger.Field("action", action), logger.Field("error", err.Error()))
|
|
}
|
|
|
|
func logMigrationSuccess(action string) {
|
|
logger.Infow("Migration success", logger.Field("action", action))
|
|
}
|