add gva error message
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package datas
|
||||
|
||||
import (
|
||||
"github.com/gookit/color"
|
||||
"time"
|
||||
|
||||
"gin-vue-admin/model"
|
||||
@@ -9,10 +10,10 @@ import (
|
||||
|
||||
type SysDictionaryToPostgresql struct {
|
||||
gorm.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:'描述'"`
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -28,6 +29,10 @@ func InitSysDictionary(db *gorm.DB) (err error) {
|
||||
{Model: gorm.Model{ID: 6, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库bool类型", Type: "bool", Status: status, Desc: "数据库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 { // 遇到错误时回滚事务
|
||||
return err
|
||||
}
|
||||
@@ -38,8 +43,7 @@ func InitSysDictionary(db *gorm.DB) (err error) {
|
||||
func InitSysDictionaryToPostgresql(db *gorm.DB) (err error) {
|
||||
status := new(bool)
|
||||
*status = true
|
||||
tx := db.Begin() // 开始事务
|
||||
insert := []SysDictionaryToPostgresql{
|
||||
Dictionaries := []SysDictionaryToPostgresql{
|
||||
{Model: gorm.Model{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "性别", Type: "sex", Status: status, Description: "性别字典"},
|
||||
{Model: gorm.Model{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库int类型", Type: "int", Status: status, Description: "int类型对应的数据库类型"},
|
||||
{Model: gorm.Model{ID: 3, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库时间日期类型", Type: "time.Time", Status: status, Description: "数据库时间日期类型"},
|
||||
@@ -47,8 +51,14 @@ func InitSysDictionaryToPostgresql(db *gorm.DB) (err error) {
|
||||
{Model: gorm.Model{ID: 5, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库字符串", Type: "string", Status: status, Description: "数据库字符串"},
|
||||
{Model: gorm.Model{ID: 6, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库bool类型", Type: "bool", Status: status, Description: "数据库bool类型"},
|
||||
}
|
||||
if tx.Create(&insert).Error != nil { // 遇到错误时回滚事务
|
||||
tx.Rollback()
|
||||
}
|
||||
return tx.Commit().Error
|
||||
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 { // 遇到错误时回滚事务
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user