2022-04-09 21:53:37 +08:00
|
|
|
// Copyright 2022 The Goploy Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a GPLv3-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-08-04 14:28:25 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2021-12-14 17:42:12 +08:00
|
|
|
"fmt"
|
2021-07-17 11:12:17 +08:00
|
|
|
"github.com/hashicorp/go-version"
|
2021-12-14 17:42:12 +08:00
|
|
|
"github.com/zhenorzz/goploy/config"
|
2023-05-12 10:11:38 +08:00
|
|
|
"github.com/zhenorzz/goploy/database"
|
2022-11-23 10:30:02 +08:00
|
|
|
"github.com/zhenorzz/goploy/internal/pkg"
|
2020-08-04 14:28:25 +08:00
|
|
|
"log"
|
|
|
|
"net/url"
|
2021-07-17 11:12:17 +08:00
|
|
|
"path"
|
|
|
|
"sort"
|
2020-08-04 14:28:25 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Pagination struct
|
|
|
|
type Pagination struct {
|
2022-01-08 21:35:57 +08:00
|
|
|
Page uint64 `json:"page" schema:"page"`
|
|
|
|
Rows uint64 `json:"rows" schema:"rows"`
|
|
|
|
Total uint64 `json:"total" schema:"total"`
|
2020-08-04 14:28:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2021-12-14 17:42:12 +08:00
|
|
|
dbConn := fmt.Sprintf(
|
2021-12-25 16:39:23 +08:00
|
|
|
"%s:%s@(%s:%s)/%s?charset=utf8mb4,utf8",
|
2021-12-14 17:42:12 +08:00
|
|
|
config.Toml.DB.User,
|
|
|
|
config.Toml.DB.Password,
|
|
|
|
config.Toml.DB.Host,
|
|
|
|
config.Toml.DB.Port,
|
2021-12-25 16:39:23 +08:00
|
|
|
config.Toml.DB.Database,
|
2021-12-14 17:42:12 +08:00
|
|
|
)
|
2020-08-04 14:28:25 +08:00
|
|
|
var err error
|
2023-05-17 10:27:21 +08:00
|
|
|
// err != nil, only occur in driver has not registered
|
2021-12-14 17:42:12 +08:00
|
|
|
DB, err = sql.Open(config.Toml.DB.Type, dbConn)
|
2020-08-04 14:28:25 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-05-17 10:27:21 +08:00
|
|
|
|
|
|
|
// ping db to make sure the db has connected
|
|
|
|
if err := DB.Ping(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-08-04 14:28:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-04-11 11:08:45 +08:00
|
|
|
func CreateDB(db *sql.DB, name string) error {
|
|
|
|
query := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", name)
|
|
|
|
_, err := db.Exec(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 10:30:02 +08:00
|
|
|
func UseDB(db *sql.DB, name string) error {
|
2022-04-11 11:08:45 +08:00
|
|
|
query := fmt.Sprintf("USE `%s`", name)
|
|
|
|
_, err := db.Exec(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-17 11:12:17 +08:00
|
|
|
func ImportSQL(db *sql.DB, sqlPath string) error {
|
2023-05-12 10:11:38 +08:00
|
|
|
sqlContent, err := database.File.ReadFile(sqlPath)
|
2021-07-17 11:12:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, query := range strings.Split(string(sqlContent), ";") {
|
2022-11-23 10:30:02 +08:00
|
|
|
query = pkg.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")
|
|
|
|
}
|
|
|
|
|
2023-05-12 10:11:38 +08:00
|
|
|
sqlEntries, err := database.File.ReadDir(".")
|
2021-07-17 11:12:17 +08:00
|
|
|
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) {
|
2023-05-12 10:11:38 +08:00
|
|
|
if err := ImportSQL(DB, ver.String()+database.FileExt); err != nil {
|
2021-07-17 11:12:17 +08:00
|
|
|
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
|
|
|
}
|