修复初始化已知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
+8 -9
View File
@@ -3,21 +3,20 @@ package models
import ( import (
orm "go-admin/common/global" orm "go-admin/common/global"
"go-admin/tools" "go-admin/tools"
"gorm.io/gorm"
) )
type SysFileDir struct { type SysFileDir struct {
gorm.Model Id int `json:"id"`
Id int `json:"id" gorm:"type:int(11);primary_key;auto_increment;"` // Label string `json:"label" gorm:"type:varchar(255);"` // 名称
Label string `json:"label" gorm:"type:varchar(255);"` // 名称 PId int `json:"pId" gorm:"type:int(11);"` // 父id
PId int `json:"pId" gorm:"type:int(11);"` // 父id Sort int `json:"sort" gorm:""` //排序
Sort int `json:"sort" gorm:""` //排序 Path string `json:"path" gorm:"size:255;"` //
Path string `json:"path" gorm:"size:255;"` // CreateBy string `json:"createBy" gorm:"type:varchar(128);"` // 创建人
CreateBy string `json:"createBy" gorm:"type:varchar(128);"` // 创建 UpdateBy string `json:"updateBy" gorm:"type:varchar(128);"` // 编辑
UpdateBy string `json:"updateBy" gorm:"type:varchar(128);"` // 编辑人
Children []SysFileDir `json:"children" gorm:"-"` Children []SysFileDir `json:"children" gorm:"-"`
DataScope string `json:"dataScope" gorm:"-"` DataScope string `json:"dataScope" gorm:"-"`
Params string `json:"params" gorm:"-"` Params string `json:"params" gorm:"-"`
BaseModel
} }
func (SysFileDir) TableName() string { func (SysFileDir) TableName() string {
+10 -10
View File
@@ -5,16 +5,16 @@ import (
) )
type SysFileInfo struct { 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);"` // 文件类型 Type string `json:"type" gorm:"type:varchar(255);"` // 文件类型
Name string `json:"name" gorm:"type:varchar(255);"` // 文件名称 Name string `json:"name" gorm:"type:varchar(255);"` // 文件名称
Size string `json:"size" gorm:"type:int(11);"` // 文件大小 Size string `json:"size" gorm:"type:int(11);"` // 文件大小
PId int `json:"pId" gorm:"type:int(11);"` // 目录id PId int `json:"pId" gorm:"type:int(11);"` // 目录id
Source string `json:"source" gorm:"type:varchar(255);"` // 文件源 Source string `json:"source" gorm:"type:varchar(255);"` // 文件源
Url string `json:"url" gorm:"type:varchar(255);"` // 文件路径 Url string `json:"url" gorm:"type:varchar(255);"` // 文件路径
FullUrl string `json:"fullUrl" gorm:"type:varchar(255);"` // 文件全路径 FullUrl string `json:"fullUrl" gorm:"type:varchar(255);"` // 文件全路径
CreateBy string `json:"createBy" gorm:"type:varchar(128);"` // 创建人 CreateBy string `json:"createBy" gorm:"type:varchar(128);"` // 创建人
UpdateBy string `json:"updateBy" gorm:"type:varchar(128);"` // 编辑人 UpdateBy string `json:"updateBy" gorm:"type:varchar(128);"` // 编辑人
DataScope string `json:"dataScope" gorm:"-"` DataScope string `json:"dataScope" gorm:"-"`
Params string `json:"params" gorm:"-"` Params string `json:"params" gorm:"-"`
BaseModel BaseModel
+16 -7
View File
@@ -1,19 +1,23 @@
package migration package migration
import ( import (
"fmt"
"github.com/spf13/cast"
"gorm.io/gorm" "gorm.io/gorm"
"log" "log"
"path/filepath"
"sort" "sort"
"strconv"
"sync" "sync"
) )
var Migrate = &Migration{ 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 { type Migration struct {
db *gorm.DB 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 mutex sync.Mutex
} }
@@ -25,22 +29,22 @@ func (e *Migration) SetDb(db *gorm.DB) {
e.db = 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() e.mutex.Lock()
defer e.mutex.Unlock() defer e.mutex.Unlock()
e.version[k] = f e.version[k] = f
} }
func (e *Migration) Migrate() { func (e *Migration) Migrate() {
versions := make([]string, 0) versions := make([]int, 0)
for k := range e.version { for k := range e.version {
versions = append(versions, k) versions = append(versions, k)
} }
sort.StringsAreSorted(versions) sort.IntsAreSorted(versions)
log.Println(versions)
var err error var err error
var count int64 var count int64
for _, v := range versions { for _, v := range versions {
fmt.Println(v)
err = e.db.Debug().Table("sys_migration").Where("version = ?", v).Count(&count).Error err = e.db.Debug().Table("sys_migration").Where("version = ?", v).Count(&count).Error
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
@@ -50,9 +54,14 @@ func (e *Migration) Migrate() {
count = 0 count = 0
continue continue
} }
err = (e.version[v])(e.db.Debug(), v) err = (e.version[v])(e.db.Debug(), strconv.Itoa(v))
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
} }
} }
func GetFilename(s string) int {
s = filepath.Base(s)
return cast.ToInt(s[:13])
}
@@ -1,20 +1,19 @@
package version package version
import ( import (
"runtime"
"gorm.io/gorm"
"go-admin/app/admin/models" "go-admin/app/admin/models"
"go-admin/app/admin/models/tools" "go-admin/app/admin/models/tools"
"go-admin/cmd/migrate/migration" "go-admin/cmd/migrate/migration"
common "go-admin/common/models" common "go-admin/common/models"
"gorm.io/gorm"
"path/filepath"
"runtime"
) )
func init() { func init() {
_, fileName, _, _ := runtime.Caller(0) _, fileName, _, _ := runtime.Caller(0)
fileName = filepath.Base(fileName) migration.Migrate.SetVersion(migration.GetFilename(fileName), _1599190683659Tables)
fileName = fileName[:len(fileName)-3]
migration.Migrate.SetVersion(fileName, _1599190683659Tables)
} }
func _1599190683659Tables(db *gorm.DB, version string) error { func _1599190683659Tables(db *gorm.DB, version string) error {
@@ -1,19 +1,18 @@
package version package version
import ( import (
"runtime"
"gorm.io/gorm"
"go-admin/app/admin/models" "go-admin/app/admin/models"
"go-admin/cmd/migrate/migration" "go-admin/cmd/migrate/migration"
common "go-admin/common/models" common "go-admin/common/models"
"gorm.io/gorm"
"path/filepath"
"runtime"
) )
func init() { func init() {
_, fileName, _, _ := runtime.Caller(0) _, fileName, _, _ := runtime.Caller(0)
fileName = filepath.Base(fileName) migration.Migrate.SetVersion(migration.GetFilename(fileName), _1599190683670Test)
fileName = fileName[:len(fileName)-3]
migration.Migrate.SetVersion(fileName, _1599190683670Test)
} }
func _1599190683670Test(db *gorm.DB, version string) error { 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{ return tx.Create(&common.Migration{
Version: version, Version: version,
}).Error }).Error
@@ -1,19 +1,18 @@
package version package version
import ( import (
"runtime"
"gorm.io/gorm"
"go-admin/app/admin/models" "go-admin/app/admin/models"
"go-admin/cmd/migrate/migration" "go-admin/cmd/migrate/migration"
common "go-admin/common/models" common "go-admin/common/models"
"gorm.io/gorm"
"path/filepath"
"runtime"
) )
func init() { func init() {
_, fileName, _, _ := runtime.Caller(0) _, fileName, _, _ := runtime.Caller(0)
fileName = filepath.Base(fileName) migration.Migrate.SetVersion(migration.GetFilename(fileName), _1599190683680Test)
fileName = fileName[:len(fileName)-3]
migration.Migrate.SetVersion(fileName, _1599190683680Test)
} }
func _1599190683680Test(db *gorm.DB, version string) error { func _1599190683680Test(db *gorm.DB, version string) error {
@@ -22,7 +21,7 @@ func _1599190683680Test(db *gorm.DB, version string) error {
list := []models.Menu{ 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: 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: 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: 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"}, {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/goimgext v1.0.0 // indirect
github.com/shamsher31/goimgtype v1.0.0 github.com/shamsher31/goimgtype v1.0.0
github.com/shirou/gopsutil v2.20.7+incompatible 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/cobra v1.0.0
github.com/spf13/viper v1.7.1 github.com/spf13/viper v1.7.1
github.com/swaggo/gin-swagger v1.2.0 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 "CreatedAt" -}}
{{- else if eq .GoField "UpdatedAt" -}} {{- else if eq .GoField "UpdatedAt" -}}
{{- else if eq .GoField "DeletedAt" -}} {{- 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}}"` {{.GoField}} uint `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
{{- else }} {{- else }}
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}"` // {{.ColumnComment}} {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
{{end -}} {{end -}}
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` {{end -}}
{{- end }} {{- end }}
} }