songzhibin97
3 years ago
9 changed files with 177 additions and 7 deletions
-
18server/api/v1/sys_auto_code.go
-
4server/model/sys_auto_code.go
-
13server/model/sys_autocode_history.go
-
1server/router/sys_auto_code.go
-
34server/service/sys_auto_code.go
-
58server/service/sys_autocode_history.go
-
1server/service/sys_initdb.go
-
4server/utils/file_operations.go
-
41server/utils/injectionCode.go
@ -0,0 +1,13 @@ |
|||||
|
package model |
||||
|
|
||||
|
import "gin-vue-admin/global" |
||||
|
|
||||
|
// 自动迁移代码记录,用于回滚,重放使用
|
||||
|
|
||||
|
type SysAutoCodeHistory struct { |
||||
|
global.GVA_MODEL |
||||
|
TableName string |
||||
|
AutoCodeMeta string `gorm:"type:text"` // 其他meta信息 path;path
|
||||
|
InjectionMeta string `gorm:"type:text"` // 注入的内容 RouterPath@functionName@RouterString;
|
||||
|
Flag int // 表示对应状态 0 代表创建, 1 代表回滚 ...
|
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
package service |
||||
|
|
||||
|
import ( |
||||
|
"errors" |
||||
|
"gin-vue-admin/global" |
||||
|
"gin-vue-admin/model" |
||||
|
"gin-vue-admin/utils" |
||||
|
"strings" |
||||
|
|
||||
|
"go.uber.org/zap" |
||||
|
) |
||||
|
|
||||
|
// CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
|
||||
|
func CreateAutoCodeHistory(autoCodeMeta string, injectionMeta string, tableName string) error { |
||||
|
return global.GVA_DB.Create(&model.SysAutoCodeHistory{ |
||||
|
AutoCodeMeta: autoCodeMeta, |
||||
|
InjectionMeta: injectionMeta, |
||||
|
TableName: tableName, |
||||
|
}).Error |
||||
|
} |
||||
|
|
||||
|
func RollBack(id uint) error { |
||||
|
md := model.SysAutoCodeHistory{} |
||||
|
if err := global.GVA_DB.First(&md, id).Error; err != nil { |
||||
|
return err |
||||
|
} |
||||
|
// 切分数据
|
||||
|
err, dbNames := GetTables(global.GVA_CONFIG.Mysql.Dbname) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
// 删除表
|
||||
|
for _, name := range dbNames { |
||||
|
if strings.Contains(strings.ToUpper(strings.Replace(name.TableName, "_", "", -1)), strings.ToUpper(md.TableName)) { |
||||
|
// 删除表
|
||||
|
if err = DropTable(name.TableName); err != nil { |
||||
|
global.GVA_LOG.Error("ClearTag DropTable:", zap.Error(err)) |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
// 删除文件
|
||||
|
|
||||
|
for _, path := range strings.Split(md.AutoCodeMeta, ";") { |
||||
|
_ = utils.DeLFile(path) |
||||
|
} |
||||
|
// 清除注入
|
||||
|
for _, v := range strings.Split(md.InjectionMeta, ";") { |
||||
|
// RouterPath@functionName@RouterString
|
||||
|
meta := strings.Split(v, "@") |
||||
|
if len(meta) != 3 { |
||||
|
return errors.New("split InjectionMeta Err") |
||||
|
} |
||||
|
_ = utils.AutoClearCode(meta[0], meta[2]) |
||||
|
} |
||||
|
md.Flag = 1 |
||||
|
return global.GVA_DB.Save(&md).Error |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue