feat:回滚自动化生产代码
This commit is contained in:
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model"
|
||||
"gin-vue-admin/model/request"
|
||||
@@ -143,6 +144,35 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
|
||||
return err
|
||||
}
|
||||
err = utils.AutoInjectionCode(initializeRouterFilePath, "Routers", "router.Init"+autoCode.StructName+"Router(PrivateGroup)")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
injectionCodeMeta := strings.Builder{}
|
||||
injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeGormFilePath, "MysqlTables", "model."+autoCode.StructName+"{},"))
|
||||
injectionCodeMeta.WriteString(";")
|
||||
injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeRouterFilePath, "Routers", "router.Init"+autoCode.StructName+"Router(PrivateGroup)"))
|
||||
|
||||
// 保存生成信息
|
||||
bf := strings.Builder{}
|
||||
for _, data := range dataList {
|
||||
if len(data.autoMoveFilePath) != 0 {
|
||||
bf.WriteString(data.autoMoveFilePath)
|
||||
bf.WriteString(";")
|
||||
}
|
||||
}
|
||||
|
||||
if autoCode.TableName != "" {
|
||||
err = CreateAutoCodeHistory(bf.String(),
|
||||
injectionCodeMeta.String(),
|
||||
autoCode.TableName,
|
||||
)
|
||||
} else {
|
||||
err = CreateAutoCodeHistory(bf.String(),
|
||||
injectionCodeMeta.String(),
|
||||
autoCode.StructName,
|
||||
)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -215,6 +245,10 @@ func GetColumn(tableName string, dbName string) (err error, Columns []request.Co
|
||||
return err, Columns
|
||||
}
|
||||
|
||||
func DropTable(tableName string) error {
|
||||
return global.GVA_DB.Exec("DROP TABLE " + tableName).Error
|
||||
}
|
||||
|
||||
//@author: [SliverHorn](https://github.com/SliverHorn)
|
||||
//@author: [songzhibin97](https://github.com/songzhibin97)
|
||||
//@function: addAutoMoveFile
|
||||
@@ -361,10 +395,10 @@ func getNeedList(autoCode *model.AutoCodeStruct) (dataList []tplData, fileList [
|
||||
firstDot := strings.Index(origFileName, ".")
|
||||
if firstDot != -1 {
|
||||
var fileName string
|
||||
if origFileName[firstDot:] !=".go"{
|
||||
fileName = autoCode.PackageName+origFileName[firstDot:]
|
||||
}else{
|
||||
fileName = autoCode.HumpPackageName+origFileName[firstDot:]
|
||||
if origFileName[firstDot:] != ".go" {
|
||||
fileName = autoCode.PackageName + origFileName[firstDot:]
|
||||
} else {
|
||||
fileName = autoCode.HumpPackageName + origFileName[firstDot:]
|
||||
}
|
||||
|
||||
dataList[index].autoCodePath = filepath.Join(autoPath, trimBase[:lastSeparator], autoCode.PackageName,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -131,6 +131,7 @@ func InitDB(conf request.InitDB) error {
|
||||
model.ExaSimpleUploader{},
|
||||
model.ExaCustomer{},
|
||||
model.SysOperationRecord{},
|
||||
model.SysAutoCodeHistory{},
|
||||
)
|
||||
if err != nil {
|
||||
global.GVA_DB = nil
|
||||
|
||||
Reference in New Issue
Block a user