goploy/model/Model.go

149 lines
2.7 KiB
Go
Raw Normal View History

2020-08-04 14:28:25 +08:00
package model
import (
"database/sql"
2021-07-17 11:12:17 +08:00
"embed"
2020-08-04 14:28:25 +08:00
"errors"
2021-07-17 11:12:17 +08:00
"github.com/hashicorp/go-version"
2021-08-04 19:38:06 +08:00
"github.com/zhenorzz/goploy/utils"
2020-08-04 14:28:25 +08:00
"log"
"net/url"
"os"
2021-07-17 11:12:17 +08:00
"path"
"sort"
2020-08-04 14:28:25 +08:00
"strconv"
"strings"
)
2021-07-17 11:12:17 +08:00
//go:embed sql
var sqlFile embed.FS
2020-08-04 14:28:25 +08:00
// Pagination struct
type Pagination struct {
Page uint64 `json:"page"`
Rows uint64 `json:"rows"`
Total uint64 `json:"total"`
}
// state type
const (
Fail = iota
Success
)
// state type
const (
Disable = iota
Enable
)
2020-09-25 20:05:25 +08:00
// review state type
const (
PENDING = iota
APPROVE
DENY
)
2020-08-04 14:28:25 +08:00
// DB init when the program start
var DB *sql.DB
2020-08-15 13:38:06 +08:00
// Init DB
2020-08-04 14:28:25 +08:00
func Init() {
dbType := os.Getenv("DB_TYPE")
dbConn := os.Getenv("DB_CONN")
var err error
DB, err = sql.Open(dbType, dbConn)
if err != nil {
log.Fatal(err)
}
}
// PaginationFrom param return pagination struct
func PaginationFrom(param url.Values) (Pagination, error) {
page, err := strconv.ParseUint(param.Get("page"), 10, 64)
if err != nil {
return Pagination{}, errors.New("invalid page")
}
rows, err := strconv.ParseUint(param.Get("rows"), 10, 64)
if err != nil {
return Pagination{}, errors.New("invalid rows")
}
pagination := Pagination{Page: page, Rows: rows}
return pagination, nil
}
2020-08-04 20:00:21 +08:00
// ImportSQL -
2021-07-17 11:12:17 +08:00
func ImportSQL(db *sql.DB, sqlPath string) error {
sqlContent, err := sqlFile.ReadFile(sqlPath)
if err != nil {
return err
}
for _, query := range strings.Split(string(sqlContent), ";") {
2021-08-04 19:38:06 +08:00
query = utils.ClearNewline(query)
2020-08-04 14:28:25 +08:00
if len(query) == 0 {
continue
}
_, err := db.Exec(query)
if err != nil {
return err
}
}
2021-07-17 11:12:17 +08:00
return nil
}
2020-08-04 14:28:25 +08:00
2021-07-17 11:12:17 +08:00
func Update(targetVerStr string) error {
systemConfig, err := SystemConfig{
Key: "version",
}.GetDataByKey()
if err != nil {
return err
}
if systemConfig.Value == "" {
2021-08-20 11:26:37 +08:00
systemConfig.Value = "0.0.1"
2021-07-17 11:12:17 +08:00
}
currentVer, err := version.NewVersion(systemConfig.Value)
if err != nil {
return err
}
targetVer, err := version.NewVersion(targetVerStr)
if err != nil {
return err
}
if ret := currentVer.Compare(targetVer); ret == 0 {
2021-08-20 11:26:37 +08:00
return nil
2021-07-17 11:12:17 +08:00
} else if ret == 1 {
return errors.New("currentVer greater than targetVer")
}
sqlEntries, err := sqlFile.ReadDir("sql")
if err != nil {
return err
}
var vers []*version.Version
for _, entry := range sqlEntries {
filename := entry.Name()
ver, err := version.NewVersion(filename[0 : len(filename)-len(path.Ext(filename))])
if err != nil {
2020-08-04 14:28:25 +08:00
continue
}
2021-07-17 11:12:17 +08:00
vers = append(vers, ver)
}
sort.Sort(version.Collection(vers))
for _, ver := range vers {
if currentVer.LessThan(ver) && targetVer.GreaterThanOrEqual(ver) {
if err := ImportSQL(DB, "sql/"+ver.String()+".sql"); err != nil {
return err
}
2020-08-04 14:28:25 +08:00
}
}
2021-08-20 11:26:37 +08:00
println(`Update app success`)
2021-07-17 11:12:17 +08:00
systemConfig.Value = targetVerStr
return systemConfig.EditRowByKey()
2020-08-04 14:28:25 +08:00
}