Files
open-im-server/pkg/common/db/relation/file_model.go
T

51 lines
1.5 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package relation
2022-05-10 10:44:43 +08:00
import (
2023-01-11 16:23:16 +08:00
"gorm.io/gorm"
2022-05-10 10:44:43 +08:00
"time"
)
2023-01-11 16:23:16 +08:00
var AppDB *gorm.DB
2023-01-13 18:00:18 +08:00
type AppVersion struct {
Version string `gorm:"column:version;size:64" json:"version"`
Type int `gorm:"column:type;primary_key" json:"type"`
UpdateTime int `gorm:"column:update_time" json:"update_time"`
ForceUpdate bool `gorm:"column:force_update" json:"force_update"`
FileName string `gorm:"column:file_name" json:"file_name"`
YamlName string `gorm:"column:yaml_name" json:"yaml_name"`
UpdateLog string `gorm:"column:update_log" json:"update_log"`
}
func (AppVersion) TableName() string {
return "app_version"
}
2022-05-11 10:19:58 +08:00
func UpdateAppVersion(appType int, version string, forceUpdate bool, fileName, yamlName, updateLog string) error {
2022-05-10 17:50:22 +08:00
updateTime := int(time.Now().Unix())
2023-01-04 17:22:55 +08:00
app := AppVersion{
2022-05-10 17:50:22 +08:00
Version: version,
Type: appType,
UpdateTime: updateTime,
FileName: fileName,
YamlName: yamlName,
ForceUpdate: forceUpdate,
2022-05-11 10:19:58 +08:00
UpdateLog: updateLog,
2022-05-10 10:44:43 +08:00
}
2023-01-11 16:23:16 +08:00
result := AppDB.Model(AppVersion{}).Where("type = ?", appType).Updates(map[string]interface{}{"force_update": forceUpdate,
2022-05-11 10:19:58 +08:00
"version": version, "update_time": int(time.Now().Unix()), "file_name": fileName, "yaml_name": yamlName, "type": appType, "update_log": updateLog})
2022-05-10 10:44:43 +08:00
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
2023-01-11 16:23:16 +08:00
err := AppDB.Create(&app).Error
2022-05-10 10:44:43 +08:00
return err
}
return nil
}
2023-01-04 17:22:55 +08:00
func GetNewestVersion(appType int) (*AppVersion, error) {
app := AppVersion{}
2023-01-11 16:23:16 +08:00
return &app, AppDB.Model(AppVersion{}).First(&app, appType).Error
2022-05-10 10:44:43 +08:00
}