修复初始化已知bug

This commit is contained in:
linwenxiang
2020-09-14 21:52:08 +08:00
parent ca4ed89536
commit 3d5a2ea9f7
9 changed files with 88 additions and 56 deletions
+2 -3
View File
@@ -3,12 +3,10 @@ package models
import (
orm "go-admin/common/global"
"go-admin/tools"
"gorm.io/gorm"
)
type SysFileDir struct {
gorm.Model
Id int `json:"id" gorm:"type:int(11);primary_key;auto_increment;"` //
Id int `json:"id"`
Label string `json:"label" gorm:"type:varchar(255);"` // 名称
PId int `json:"pId" gorm:"type:int(11);"` // 父id
Sort int `json:"sort" gorm:""` //排序
@@ -18,6 +16,7 @@ type SysFileDir struct {
Children []SysFileDir `json:"children" gorm:"-"`
DataScope string `json:"dataScope" gorm:"-"`
Params string `json:"params" gorm:"-"`
BaseModel
}
func (SysFileDir) TableName() string {
+1 -1
View File
@@ -5,7 +5,7 @@ import (
)
type SysFileInfo struct {
Id int `json:"id" gorm:"type:int(11);primary_key;auto_increment;"` // id
Id int `json:"id"` // id
Type string `json:"type" gorm:"type:varchar(255);"` // 文件类型
Name string `json:"name" gorm:"type:varchar(255);"` // 文件名称
Size string `json:"size" gorm:"type:int(11);"` // 文件大小
+16 -7
View File
@@ -1,19 +1,23 @@
package migration
import (
"fmt"
"github.com/spf13/cast"
"gorm.io/gorm"
"log"
"path/filepath"
"sort"
"strconv"
"sync"
)
var Migrate = &Migration{
version: make(map[string]func(db *gorm.DB, version string) error),
version: make(map[int]func(db *gorm.DB, version string) error),
}
type Migration struct {
db *gorm.DB
version map[string]func(db *gorm.DB, version string) error
version map[int]func(db *gorm.DB, version string) error
mutex sync.Mutex
}
@@ -25,22 +29,22 @@ func (e *Migration) SetDb(db *gorm.DB) {
e.db = db
}
func (e *Migration) SetVersion(k string, f func(db *gorm.DB, version string) error) {
func (e *Migration) SetVersion(k int, f func(db *gorm.DB, version string) error) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.version[k] = f
}
func (e *Migration) Migrate() {
versions := make([]string, 0)
versions := make([]int, 0)
for k := range e.version {
versions = append(versions, k)
}
sort.StringsAreSorted(versions)
log.Println(versions)
sort.IntsAreSorted(versions)
var err error
var count int64
for _, v := range versions {
fmt.Println(v)
err = e.db.Debug().Table("sys_migration").Where("version = ?", v).Count(&count).Error
if err != nil {
log.Fatalln(err)
@@ -50,9 +54,14 @@ func (e *Migration) Migrate() {
count = 0
continue
}
err = (e.version[v])(e.db.Debug(), v)
err = (e.version[v])(e.db.Debug(), strconv.Itoa(v))
if err != nil {
log.Fatalln(err)
}
}
}
func GetFilename(s string) int {
s = filepath.Base(s)
return cast.ToInt(s[:13])
}
@@ -1,20 +1,19 @@
package version
import (
"runtime"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/app/admin/models/tools"
"go-admin/cmd/migrate/migration"
common "go-admin/common/models"
"gorm.io/gorm"
"path/filepath"
"runtime"
)
func init() {
_, fileName, _, _ := runtime.Caller(0)
fileName = filepath.Base(fileName)
fileName = fileName[:len(fileName)-3]
migration.Migrate.SetVersion(fileName, _1599190683659Tables)
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1599190683659Tables)
}
func _1599190683659Tables(db *gorm.DB, version string) error {
@@ -1,19 +1,18 @@
package version
import (
"runtime"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/cmd/migrate/migration"
common "go-admin/common/models"
"gorm.io/gorm"
"path/filepath"
"runtime"
)
func init() {
_, fileName, _, _ := runtime.Caller(0)
fileName = filepath.Base(fileName)
fileName = fileName[:len(fileName)-3]
migration.Migrate.SetVersion(fileName, _1599190683670Test)
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1599190683670Test)
}
func _1599190683670Test(db *gorm.DB, version string) error {
@@ -23,7 +22,6 @@ func _1599190683670Test(db *gorm.DB, version string) error {
}
return tx.Create(&common.Migration{
Version: version,
}).Error
@@ -1,19 +1,18 @@
package version
import (
"runtime"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/cmd/migrate/migration"
common "go-admin/common/models"
"gorm.io/gorm"
"path/filepath"
"runtime"
)
func init() {
_, fileName, _, _ := runtime.Caller(0)
fileName = filepath.Base(fileName)
fileName = fileName[:len(fileName)-3]
migration.Migrate.SetVersion(fileName, _1599190683680Test)
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1599190683680Test)
}
func _1599190683680Test(db *gorm.DB, version string) error {
@@ -22,7 +21,7 @@ func _1599190683680Test(db *gorm.DB, version string) error {
list := []models.Menu{
{MenuId: 496, MenuName: "Sources", Title: "资源管理", Icon: "network", Path: "/sources", Paths: "/0/496", MenuType: "M", Action: "无", Permission: "", ParentId: 0, NoCache: true, Breadcrumb: "", Component: "Layout", Sort: 3, Visible: "0", CreateBy: "1", UpdateBy: "1", IsFrame: "1"},
{MenuId: 497, MenuName: "File", Title: "文件管理", Icon: "documentation", Path: "filemanage", Paths: "/0/496/497", MenuType: "C", Action: "", Permission: "", ParentId: 496, NoCache: true, Breadcrumb: "", Component: "/filemanage/index", Sort: 0, Visible: "0", CreateBy: "1", UpdateBy: "1", IsFrame: "1"},
{MenuId: 497, MenuName: "File", Title: "文件管理", Icon: "documentation", Path: "file-manage", Paths: "/0/496/497", MenuType: "C", Action: "", Permission: "", ParentId: 496, NoCache: true, Breadcrumb: "", Component: "/fileManage/index", Sort: 0, Visible: "0", CreateBy: "1", UpdateBy: "1", IsFrame: "1"},
{MenuId: 498, MenuName: "", Title: "内容管理", Icon: "pass", Path: "/content", Paths: "/0/498", MenuType: "M", Action: "无", Permission: "", ParentId: 0, NoCache: true, Breadcrumb: "", Component: "Layout", Sort: 4, Visible: "0", CreateBy: "1", UpdateBy: "1", IsFrame: "1"},
{MenuId: 499, MenuName: "SysCategory", Title: "分类", Icon: "pass", Path: "syscategory", Paths: "/0/498/499", MenuType: "C", Action: "无", Permission: "syscategory:syscategory:list", ParentId: 498, NoCache: true, Breadcrumb: "", Component: "/syscategory/index", Sort: 0, Visible: "0", CreateBy: "1", UpdateBy: "1", IsFrame: "0"},
{MenuId: 500, MenuName: "", Title: "分页获取分类", Icon: "pass", Path: "", Paths: "/0/498/499/500", MenuType: "F", Action: "无", Permission: "syscategory:syscategory:query", ParentId: 499, NoCache: true, Breadcrumb: "", Component: "", Sort: 0, Visible: "0", CreateBy: "1", UpdateBy: "1", IsFrame: "0"},
@@ -0,0 +1,36 @@
package version
import (
"runtime"
"gorm.io/gorm"
"go-admin/app/admin/models"
"go-admin/cmd/migrate/migration"
common "go-admin/common/models"
)
func init() {
_, fileName, _, _ := runtime.Caller(0)
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1600089797118Migrate)
}
func _1600089797118Migrate(db *gorm.DB, version string) error {
return db.Transaction(func(tx *gorm.DB) error {
f := &models.SysFileDir{
Label: "根目录",
PId: 0,
Sort: 0,
Path: "",
CreateBy: "1",
UpdateBy: "1",
}
err := tx.Create(f).Error
if err != nil {
return err
}
return tx.Create(&common.Migration{
Version: version,
}).Error
})
}
+1
View File
@@ -25,6 +25,7 @@ require (
github.com/shamsher31/goimgext v1.0.0 // indirect
github.com/shamsher31/goimgtype v1.0.0
github.com/shirou/gopsutil v2.20.7+incompatible
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.1
github.com/swaggo/gin-swagger v1.2.0
+1 -10
View File
@@ -50,21 +50,12 @@ type {{.ClassName}}Control struct {
{{- else if eq .GoField "CreatedAt" -}}
{{- else if eq .GoField "UpdatedAt" -}}
{{- else if eq .GoField "DeletedAt" -}}
{{- else if eq .GoField "CreateBy" }}
{{.GoField}} uint `json:"{{.JsonField}}"` // {{.ColumnComment}}
{{- else if eq .GoField "UpdateBy" }}
{{- else if eq .GoField "CreateBy" -}}
{{.GoField}} uint `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
{{- else if eq .GoField "UpdateBy" -}}
{{.GoField}} uint `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
{{- else }}
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}"` // {{.ColumnComment}}
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
{{end -}}
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` {{end -}}
{{- end }}
}