feat: 系统升级支持数据库自动迁移,避免手动执行升级脚本

This commit is contained in:
meilin.huang
2025-02-13 21:11:23 +08:00
parent efb2b7368c
commit aa393590b2
78 changed files with 1965 additions and 1827 deletions

View File

@@ -3,16 +3,15 @@ package config
import "mayfly-go/pkg/logx"
type Mysql struct {
AutoMigration bool `mapstructure:"auto-migration" json:"autoMigration" yaml:"auto-migration"`
Host string `mapstructure:"path" json:"host" yaml:"host"`
Config string `mapstructure:"config" json:"config" yaml:"config"`
Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"`
Username string `mapstructure:"username" json:"username" yaml:"username"`
Password string `mapstructure:"password" json:"password" yaml:"password"`
MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"`
MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"`
LogMode bool `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"`
LogZap string `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"`
Host string `mapstructure:"path" json:"host" yaml:"host"`
Config string `mapstructure:"config" json:"config" yaml:"config"`
Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"`
Username string `mapstructure:"username" json:"username" yaml:"username"`
Password string `mapstructure:"password" json:"password" yaml:"password"`
MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"`
MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"`
LogMode bool `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"`
LogZap string `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"`
}
func (m *Mysql) Default() {

View File

@@ -43,7 +43,7 @@ type ModelI interface {
}
type IdModel struct {
Id uint64 `json:"id"`
Id uint64 `json:"id" gorm:"primarykey;AUTO_INCREMENT"`
}
func (m *IdModel) SetId(id uint64) {
@@ -69,7 +69,7 @@ func (m *IdModel) LogicDelete() bool {
// 含有删除字段模型
type DeletedModel struct {
IdModel
IsDeleted int8 `json:"-" gorm:"column:is_deleted;default:0"`
IsDeleted int8 `json:"-" gorm:"column:is_deleted;not null;default:0;"`
DeleteTime *time.Time `json:"-"`
}
@@ -87,9 +87,9 @@ func (m *DeletedModel) LogicDelete() bool {
// CreateModelNLD 含有创建等信息,但不包含逻辑删除信息
type CreateModelNLD struct {
IdModel
CreateTime *time.Time `json:"createTime"`
CreatorId uint64 `json:"creatorId"`
Creator string `json:"creator"`
CreateTime *time.Time `json:"createTime" gorm:"not null;"`
CreatorId uint64 `json:"creatorId" gorm:"not null;"`
Creator string `json:"creator" gorm:"size:32;not null;"`
}
func (m *CreateModelNLD) FillBaseInfo(idGenType IdGenType, account *LoginAccount) {
@@ -109,9 +109,9 @@ func (m *CreateModelNLD) FillBaseInfo(idGenType IdGenType, account *LoginAccount
// 含有删除、创建字段模型
type CreateModel struct {
DeletedModel
CreateTime *time.Time `json:"createTime"`
CreatorId uint64 `json:"creatorId"`
Creator string `json:"creator"`
CreateTime *time.Time `json:"createTime" gorm:"not null;"`
CreatorId uint64 `json:"creatorId" gorm:"not null;"`
Creator string `json:"creator" gorm:"size:32;not null;"`
}
func (m *CreateModel) FillBaseInfo(idGenType IdGenType, account *LoginAccount) {
@@ -132,9 +132,9 @@ func (m *CreateModel) FillBaseInfo(idGenType IdGenType, account *LoginAccount) {
type ModelNLD struct {
CreateModelNLD
UpdateTime *time.Time `json:"updateTime"`
ModifierId uint64 `json:"modifierId"`
Modifier string `json:"modifier"`
UpdateTime *time.Time `json:"updateTime" gorm:"not null;"`
ModifierId uint64 `json:"modifierId" gorm:"not null;"`
Modifier string `json:"modifier" gorm:"size:32;not null;"`
}
// 设置基础信息. 如创建时间,修改时间,创建者,修改者信息
@@ -164,9 +164,9 @@ func (m *ModelNLD) FillBaseInfo(idGenType IdGenType, account *LoginAccount) {
type Model struct {
CreateModel
UpdateTime *time.Time `json:"updateTime"`
ModifierId uint64 `json:"modifierId"`
Modifier string `json:"modifier"`
UpdateTime *time.Time `json:"updateTime" gorm:"not null;"`
ModifierId uint64 `json:"modifierId" gorm:"not null;"`
Modifier string `json:"modifier" gorm:"size:32;not null;"`
}
// 设置基础信息. 如创建时间,修改时间,创建者,修改者信息
@@ -223,7 +223,7 @@ func (s Slice[T]) Value() (driver.Value, error) {
// 带有额外其他信息字段的结构体
type ExtraData struct {
Extra Map[string, any] `json:"extra"`
Extra Map[string, any] `json:"extra" gorm:"type:varchar(1000)"`
}
// SetExtraValue 设置额外信息字段值

View File

@@ -3,7 +3,7 @@ package starter
import (
"context"
"mayfly-go/initialize"
"mayfly-go/migrations"
"mayfly-go/migration"
"mayfly-go/pkg/config"
"mayfly-go/pkg/global"
"mayfly-go/pkg/logx"
@@ -37,8 +37,8 @@ func RunWebServer() {
initRedis()
// 数据库升级操作
if err := migrations.RunMigrations(global.Db); err != nil {
logx.Panicf("数据库升级失败: %v", err)
if err := migration.RunMigrations(global.Db); err != nil {
logx.Panicf("db migration failed: %v", err)
}
// 参数校验器初始化、如错误提示中文转译等