Browse Source

add err := and delete postgresql initdb

main
SliverHorn 4 years ago
parent
commit
439ffe64d5
  1. 2
      server/cmd/datas/apis.go
  2. 2
      server/cmd/datas/authorities.go
  3. 2
      server/cmd/datas/authority_Ids.go
  4. 2
      server/cmd/datas/authority_menus.go
  5. 7
      server/cmd/datas/casbins.go
  6. 2
      server/cmd/datas/customers.go
  7. 36
      server/cmd/datas/dictionaries.go
  8. 2
      server/cmd/datas/dictionary_details.go
  9. 2
      server/cmd/datas/files.go
  10. 56
      server/cmd/datas/init.go
  11. 2
      server/cmd/datas/menus.go
  12. 2
      server/cmd/datas/users.go
  13. 4
      server/cmd/gva/initdb.go

2
server/cmd/datas/apis.go

@ -84,7 +84,7 @@ func InitSysApi(db *gorm.DB) (err error) {
color.Danger.Println("sys_apis表的初始数据已存在!")
return nil
}
if tx.Create(&Apis).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&Apis).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

2
server/cmd/datas/authorities.go

@ -20,7 +20,7 @@ func InitSysAuthority(db *gorm.DB) (err error) {
color.Danger.Println("sys_authorities表的初始数据已存在!")
return nil
}
if tx.Create(&Authorities).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&Authorities).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

2
server/cmd/datas/authority_Ids.go

@ -24,7 +24,7 @@ func InitSysDataAuthorityId(db *gorm.DB) (err error) {
color.Danger.Println("sys_data_authority_id表的初始数据已存在!")
return nil
}
if tx.Create(&DataAuthorityId).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&DataAuthorityId).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

2
server/cmd/datas/authority_menus.go

@ -72,7 +72,7 @@ func InitSysAuthorityMenus(db *gorm.DB) (err error) {
color.Danger.Println("sys_authority_menus表的初始数据已存在!")
return nil
}
if tx.Create(&AuthorityMenus).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&AuthorityMenus).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

7
server/cmd/datas/casbins.go

@ -153,16 +153,11 @@ var Carbines = []gormadapter.CasbinRule{
func InitCasbinModel(db *gorm.DB) (err error) {
return db.Transaction(func(tx *gorm.DB) error {
if !tx.Migrator().HasTable("casbin_rule") {
if err := tx.Migrator().CreateTable(&gormadapter.CasbinRule{}); err != nil {
return err
}
}
if tx.Where("p_type = ? AND v0 IN ?", "p", []string{"888", "8881", "9528"}).Find(&[]gormadapter.CasbinRule{}).RowsAffected == 142 {
color.Danger.Println("casbin_rule表的初始数据已存在!")
return nil
}
if tx.Create(&Carbines).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&Carbines).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

2
server/cmd/datas/customers.go

@ -19,7 +19,7 @@ func InitExaCustomer(db *gorm.DB) (err error) {
color.Danger.Println("exa_customers表的初始数据已存在!")
return nil
}
if tx.Create(&Customers).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&Customers).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

36
server/cmd/datas/dictionaries.go

@ -9,15 +9,6 @@ import (
"gorm.io/gorm"
)
type SysDictionaryToPostgresql struct {
global.GVA_MODEL
Name string `json:"name" form:"name" gorm:"column:name;comment:字典名(中)"`
Type string `json:"type" form:"type" gorm:"column:type;comment:字典名(英)"`
Status *bool `json:"status" form:"status" gorm:"column:status;comment:状态"`
Description string `json:"description" form:"description" gorm:"column:description;comment:'描述'"`
SysDictionaryDetails []model.SysDictionaryDetail `json:"sysDictionaryDetails" form:"sysDictionaryDetails"`
}
func InitSysDictionary(db *gorm.DB) (err error) {
var status = new(bool)
*status = true
@ -34,32 +25,9 @@ func InitSysDictionary(db *gorm.DB) (err error) {
color.Danger.Println("sys_dictionaries表的初始数据已存在!")
return nil
}
if tx.Create(&Dictionaries).Error != nil { // 遇到错误时回滚事务
return err
}
return nil
})
}
func InitSysDictionaryToPostgresql(db *gorm.DB) (err error) {
status := new(bool)
*status = true
Dictionaries := []SysDictionaryToPostgresql{
{GVA_MODEL: global.GVA_MODEL{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "性别", Type: "sex", Status: status, Description: "性别字典"},
{GVA_MODEL: global.GVA_MODEL{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库int类型", Type: "int", Status: status, Description: "int类型对应的数据库类型"},
{GVA_MODEL: global.GVA_MODEL{ID: 3, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库时间日期类型", Type: "time.Time", Status: status, Description: "数据库时间日期类型"},
{GVA_MODEL: global.GVA_MODEL{ID: 4, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库浮点型", Type: "float64", Status: status, Description: "数据库浮点型"},
{GVA_MODEL: global.GVA_MODEL{ID: 5, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库字符串", Type: "string", Status: status, Description: "数据库字符串"},
{GVA_MODEL: global.GVA_MODEL{ID: 6, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库bool类型", Type: "bool", Status: status, Description: "数据库bool类型"},
}
return db.Transaction(func(tx *gorm.DB) error {
if tx.Where("id IN ?", []int{1, 6}).Find(&[]model.SysDictionary{}).RowsAffected == 2 {
color.Danger.Println("sys_dictionaries表的初始数据已存在!")
return nil
}
if tx.Create(&Dictionaries).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&Dictionaries).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil
})
}
}

2
server/cmd/datas/dictionary_details.go

@ -42,7 +42,7 @@ func InitSysDictionaryDetail(db *gorm.DB) (err error) {
color.Danger.Println("sys_dictionary_details表的初始数据已存在!")
return nil
}
if tx.Create(&DictionaryDetail).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&DictionaryDetail).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

2
server/cmd/datas/files.go

@ -20,7 +20,7 @@ func InitExaFileUploadAndDownload(db *gorm.DB) (err error) {
color.Danger.Println("exa_file_upload_and_downloads表的初始数据已存在!")
return nil
}
if tx.Create(&Files).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&Files).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

56
server/cmd/datas/init.go

@ -30,54 +30,6 @@ func InitMysqlData(db *gorm.DB) {
}
func InitMysqlTables(db *gorm.DB) {
var err error
err = db.AutoMigrate(
model.SysApi{},
model.SysUser{},
model.ExaFile{},
model.ExaCustomer{},
model.SysBaseMenu{},
model.SysWorkflow{},
model.SysAuthority{},
model.JwtBlacklist{},
model.ExaFileChunk{},
model.SysDictionary{},
model.ExaSimpleUploader{},
model.SysOperationRecord{},
model.SysWorkflowStepInfo{},
model.SysDictionaryDetail{},
model.SysBaseMenuParameter{},
model.ExaFileUploadAndDownload{},
)
if err != nil {
color.Warn.Printf("[Mysql]-->初始化数据表失败,err: %v\n", err)
os.Exit(0)
}
color.Info.Println("[Mysql]-->初始化数据表成功")
}
func InitPostgresqlData(db *gorm.DB) {
var err error
err = InitSysApi(db)
err = InitSysUser(db)
err = InitExaCustomer(db)
err = InitCasbinModel(db)
err = InitSysAuthority(db)
err = InitSysBaseMenus(db)
err = InitAuthorityMenu(db)
err = InitSysAuthorityMenus(db)
err = InitSysDataAuthorityId(db)
err = InitSysDictionaryDetail(db)
err = InitExaFileUploadAndDownload(db)
err = InitSysDictionaryToPostgresql(db)
if err != nil {
color.Error.Printf("[Postgresql]-->初始化数据失败,err: %v\n", err)
os.Exit(0)
}
color.Info.Println("[Postgresql]-->初始化数据成功")
}
func InitPostgresqlTables(db *gorm.DB) {
var err error
if !db.Migrator().HasTable("casbin_rule") {
err = db.Migrator().CreateTable(&gormadapter.CasbinRule{})
@ -92,17 +44,17 @@ func InitPostgresqlTables(db *gorm.DB) {
model.SysAuthority{},
model.JwtBlacklist{},
model.ExaFileChunk{},
model.SysDictionary{},
model.ExaSimpleUploader{},
model.SysOperationRecord{},
model.SysWorkflowStepInfo{},
model.SysDictionaryDetail{},
model.SysBaseMenuParameter{},
model.ExaFileUploadAndDownload{},
SysDictionaryToPostgresql{},
)
if err != nil {
color.Error.Printf("[Postgresql]-->初始化数据表失败,err: %v\n", err)
color.Warn.Printf("[Mysql]-->初始化数据表失败,err: %v\n", err)
os.Exit(0)
}
color.Info.Println("[Postgresql]-->初始化数据表成功")
}
color.Info.Println("[Mysql]-->初始化数据表成功")
}

2
server/cmd/datas/menus.go

@ -45,7 +45,7 @@ func InitSysBaseMenus(db *gorm.DB) (err error) {
color.Danger.Println("sys_base_menus表的初始数据已存在!")
return nil
}
if tx.Create(&BaseMenus).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&BaseMenus).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

2
server/cmd/datas/users.go

@ -21,7 +21,7 @@ func InitSysUser(db *gorm.DB) (err error) {
color.Danger.Println("sys_users表的初始数据已存在!")
return nil
}
if tx.Create(&Users).Error != nil { // 遇到错误时回滚事务
if err := tx.Create(&Users).Error; err != nil { // 遇到错误时回滚事务
return err
}
return nil

4
server/cmd/gva/initdb.go

@ -47,8 +47,7 @@ var initdbCmd = &cobra.Command{
datas.InitMysqlTables(db)
datas.InitMysqlData(db)
case "postgresql":
datas.InitPostgresqlTables(db)
datas.InitPostgresqlData(db)
color.Info.Println("postgresql功能开发中")
case "sqlite":
color.Info.Println("sqlite功能开发中")
case "sqlserver":
@ -56,7 +55,6 @@ var initdbCmd = &cobra.Command{
default:
datas.InitMysqlTables(db)
datas.InitMysqlData(db)
color.Info.Println("sqlserver功能开发中")
}
frame, _ := cmd.Flags().GetString("frame")
if frame == "gf" {

Loading…
Cancel
Save