mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-02 15:30:25 +08:00
feat: 优化数据库迁移与添加老表迁移
This commit is contained in:
@@ -10,9 +10,9 @@ type AuthCert struct {
|
||||
model.Model
|
||||
|
||||
Name string `json:"name"`
|
||||
AuthMethod int8 `json:"authMethod"` // 1.密码 2.秘钥
|
||||
Password string `json:"password"` // 密码or私钥
|
||||
Passphrase string `json:"passphrase"` // 私钥口令
|
||||
AuthMethod int8 `json:"authMethod"` // 1.密码 2.秘钥
|
||||
Password string `json:"password" gorm:"column:password;type:varchar(4200)"` // 密码or私钥
|
||||
Passphrase string `json:"passphrase"` // 私钥口令
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ type MachineScript struct {
|
||||
Name string `json:"name"`
|
||||
MachineId uint64 `json:"machineId"` // 机器id
|
||||
Type int `json:"type"`
|
||||
Description string `json:"description"` // 脚本描述
|
||||
Params string `json:"params"` // 参数列表json
|
||||
Script string `json:"script"` // 脚本内容
|
||||
Description string `json:"description"` // 脚本描述
|
||||
Params string `json:"params"` // 参数列表json
|
||||
Script string `json:"script" gorm:"column:script;type:text"` // 脚本内容
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ type Config struct {
|
||||
model.Model
|
||||
Name string `json:"name"` // 配置名
|
||||
Key string `json:"key"` // 配置key
|
||||
Params string `json:"params"`
|
||||
Value string `json:"value"`
|
||||
Params string `json:"params" gorm:"column:params;type:varchar(1000)"`
|
||||
Value string `json:"value" gorm:"column:value;type:varchar(1000)"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ type SysLog struct {
|
||||
|
||||
Type int8 `json:"type"`
|
||||
Description string `json:"description"`
|
||||
ReqParam string `json:"reqParam"` // 请求参数
|
||||
Resp string `json:"resp"` // 响应结构
|
||||
ReqParam string `json:"reqParam" gorm:"column:req_param;type:varchar(1000)"` // 请求参数
|
||||
Resp string `json:"resp" gorm:"column:resp;type:varchar(1000)"` // 响应结构
|
||||
}
|
||||
|
||||
func (a *SysLog) TableName() string {
|
||||
|
||||
@@ -3,6 +3,13 @@ package migrations
|
||||
import (
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"gorm.io/gorm"
|
||||
entity2 "mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/internal/machine/domain/entity"
|
||||
entity3 "mayfly-go/internal/mongo/domain/entity"
|
||||
entity6 "mayfly-go/internal/msg/domain/entity"
|
||||
entity4 "mayfly-go/internal/redis/domain/entity"
|
||||
entity5 "mayfly-go/internal/sys/domain/entity"
|
||||
entity7 "mayfly-go/internal/tag/domain/entity"
|
||||
)
|
||||
|
||||
// T2022 TODO 在此之前的数据库表结构初始化, 目前先使用mayfly-go.sql文件初始化数据库结构
|
||||
@@ -10,6 +17,87 @@ func T2022() *gormigrate.Migration {
|
||||
return &gormigrate.Migration{
|
||||
ID: "2022",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
if err := tx.AutoMigrate(&entity.AuthCert{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity.Machine{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity.MachineFile{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity.MachineMonitor{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity.MachineScript{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity.MachineCronJob{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity.MachineCronJobExec{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity.MachineCronJobRelate{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.AutoMigrate(&entity2.Db{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity2.DbSql{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity2.DbSqlExec{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.AutoMigrate(&entity3.Mongo{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.AutoMigrate(&entity4.Redis{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.AutoMigrate(&entity5.Account{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity5.AccountRole{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity5.Config{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity5.SysLog{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity5.Resource{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity5.Role{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity5.RoleResource{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.AutoMigrate(&entity6.Msg{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity7.TagTree{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity7.TagTreeTeam{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity7.Team{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.AutoMigrate(&entity7.TeamMember{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
|
||||
@@ -15,16 +15,9 @@ func T20230720() *gormigrate.Migration {
|
||||
ID: "20230319",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
// 添加路由权限
|
||||
now := time.Now()
|
||||
res := &entity.Resource{
|
||||
Model: model.Model{
|
||||
DeletedModel: model.DeletedModel{Id: 130},
|
||||
CreateTime: &now,
|
||||
CreatorId: 1,
|
||||
Creator: "admin",
|
||||
UpdateTime: &now,
|
||||
ModifierId: 1,
|
||||
Modifier: "admin",
|
||||
DeletedModel: model.DeletedModel{Id: 133},
|
||||
},
|
||||
Pid: 4,
|
||||
UiPath: "sys/auth",
|
||||
@@ -37,20 +30,14 @@ func T20230720() *gormigrate.Migration {
|
||||
"\"icon\":\"User\",\"isKeepAlive\":true," +
|
||||
"\"routeName\":\"AuthInfo\"}",
|
||||
}
|
||||
if err := tx.Save(res).Error; err != nil {
|
||||
if err := insertResource(tx, res); err != nil {
|
||||
return err
|
||||
}
|
||||
res = &entity.Resource{
|
||||
Model: model.Model{
|
||||
DeletedModel: model.DeletedModel{Id: 131},
|
||||
CreateTime: &now,
|
||||
CreatorId: 1,
|
||||
Creator: "admin",
|
||||
UpdateTime: &now,
|
||||
ModifierId: 1,
|
||||
Modifier: "admin",
|
||||
DeletedModel: model.DeletedModel{Id: 134},
|
||||
},
|
||||
Pid: 130,
|
||||
Pid: 133,
|
||||
UiPath: "sys/auth/base",
|
||||
Type: 2,
|
||||
Status: 1,
|
||||
@@ -59,12 +46,12 @@ func T20230720() *gormigrate.Migration {
|
||||
Weight: 10000000,
|
||||
Meta: "null",
|
||||
}
|
||||
if err := tx.Save(res).Error; err != nil {
|
||||
if err := insertResource(tx, res); err != nil {
|
||||
return err
|
||||
}
|
||||
// 加大params字段长度
|
||||
if err := tx.Exec("alter table " + (&entity.Config{}).TableName() +
|
||||
" modify column params varchar(1000)").Error; err != nil {
|
||||
now := time.Now()
|
||||
if err := tx.AutoMigrate(&entity.Config{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Save(&entity.Config{
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"gorm.io/gorm"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/rediscli"
|
||||
"time"
|
||||
)
|
||||
@@ -43,3 +45,24 @@ func run(db *gorm.DB, fs ...func() *gormigrate.Migration) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertResource(tx *gorm.DB, res *entity.Resource) error {
|
||||
now := time.Now()
|
||||
res.CreateTime = &now
|
||||
res.CreatorId = 1
|
||||
res.Creator = "admin"
|
||||
res.UpdateTime = &now
|
||||
res.ModifierId = 1
|
||||
res.Modifier = "admin"
|
||||
if err := tx.Save(res).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Save(&entity.RoleResource{
|
||||
DeletedModel: model.DeletedModel{},
|
||||
RoleId: 1,
|
||||
ResourceId: res.Id,
|
||||
CreateTime: &now,
|
||||
CreatorId: 1,
|
||||
Creator: "admin",
|
||||
}).Error
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ func GetByIdIn(model any, list any, ids []uint64, orderBy ...string) {
|
||||
// 若 error不为nil,则为不存在该记录
|
||||
// @param model 数据库映射实体模型
|
||||
func GetBy(model any, cols ...string) error {
|
||||
return global.Db.Select(cols).Where(model).Scopes(UndeleteScope).First(model).Error
|
||||
return global.Db.Debug().Select(cols).Where(model).Scopes(UndeleteScope).First(model).Error
|
||||
}
|
||||
|
||||
// 根据model指定条件统计数量
|
||||
|
||||
@@ -15,7 +15,7 @@ const (
|
||||
// 含有删除字段模型
|
||||
type DeletedModel struct {
|
||||
Id uint64 `json:"id"`
|
||||
IsDeleted int8 `json:"-"`
|
||||
IsDeleted int8 `json:"-" gorm:"column:is_deleted;default:0"`
|
||||
DeleteTime *time.Time `json:"-"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user