Upgrade half or gorm.

This commit is contained in:
lishuang
2022-03-15 01:19:44 +08:00
parent 329751350d
commit 69412300b6
16 changed files with 154 additions and 102 deletions

View File

@ -1,6 +1,7 @@
package rest
import (
"fmt"
"math"
"time"
)
@ -31,7 +32,8 @@ type Base struct {
}
func (this *Base) TableName() string {
panic("you should overwrite TableName()")
panic(fmt.Sprintf("you should overwrite TableName() in %v", this))
return ""
}
//pager

View File

@ -5,7 +5,7 @@ import (
"github.com/eyebluecn/tank/code/tool/builder"
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/uuid"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
"time"
)
@ -69,7 +69,7 @@ func (this *BridgeDao) PlainPage(page int, pageSize int, shareUuid string, sortA
var conditionDB *gorm.DB
conditionDB = core.CONTEXT.GetDB().Model(&Bridge{}).Where(wp.Query, wp.Args...)
count := 0
var count int64 = 0
db := conditionDB.Count(&count)
this.PanicError(db.Error)
@ -77,7 +77,7 @@ func (this *BridgeDao) PlainPage(page int, pageSize int, shareUuid string, sortA
db = conditionDB.Order(this.GetSortString(sortArray)).Offset(page * pageSize).Limit(pageSize).Find(&bridges)
this.PanicError(db.Error)
return count, bridges
return int(count), bridges
}
//get pager

View File

@ -4,7 +4,7 @@ import (
"github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/builder"
"github.com/eyebluecn/tank/code/tool/uuid"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
"time"
)
@ -61,14 +61,14 @@ func (this *DashboardDao) Page(page int, pageSize int, dt string, sortArray []bu
var conditionDB *gorm.DB
conditionDB = core.CONTEXT.GetDB().Model(&Dashboard{}).Where(wp.Query, wp.Args...)
count := 0
var count int64 = 0
db := conditionDB.Count(&count)
this.PanicError(db.Error)
var dashboards []*Dashboard
db = conditionDB.Order(this.GetSortString(sortArray)).Offset(page * pageSize).Limit(pageSize).Find(&dashboards)
this.PanicError(db.Error)
pager := NewPager(page, pageSize, count, dashboards)
pager := NewPager(page, pageSize, int(count), dashboards)
return pager
}

View File

@ -4,7 +4,7 @@ import (
"github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/builder"
"github.com/eyebluecn/tank/code/tool/result"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
"github.com/eyebluecn/tank/code/tool/uuid"
"time"
@ -48,14 +48,14 @@ func (this *FootprintDao) Page(page int, pageSize int, userUuid string, sortArra
var conditionDB *gorm.DB
conditionDB = core.CONTEXT.GetDB().Model(&Footprint{}).Where(wp.Query, wp.Args...)
count := 0
var count int64 = 0
db := conditionDB.Count(&count)
this.PanicError(db.Error)
var footprints []*Footprint
db = conditionDB.Order(this.GetSortString(sortArray)).Offset(page * pageSize).Limit(pageSize).Find(&footprints)
this.PanicError(db.Error)
pager := NewPager(page, pageSize, count, footprints)
pager := NewPager(page, pageSize, int(count), footprints)
return pager
}

View File

@ -7,7 +7,7 @@ import (
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util"
"github.com/eyebluecn/tank/code/tool/uuid"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
"os"
"path/filepath"
"time"
@ -101,14 +101,14 @@ func (this *ImageCacheDao) Page(page int, pageSize int, userUuid string, matterU
var conditionDB *gorm.DB
conditionDB = core.CONTEXT.GetDB().Model(&ImageCache{}).Where(wp.Query, wp.Args...)
count := 0
var count int64 = 0
db := conditionDB.Count(&count)
this.PanicError(db.Error)
var imageCaches []*ImageCache
db = conditionDB.Order(this.GetSortString(sortArray)).Offset(page * pageSize).Limit(pageSize).Find(&imageCaches)
this.PanicError(db.Error)
pager := NewPager(page, pageSize, count, imageCaches)
pager := NewPager(page, pageSize, int(count), imageCaches)
return pager
}

View File

@ -6,12 +6,19 @@ import (
"github.com/eyebluecn/tank/code/tool/builder"
"github.com/eyebluecn/tank/code/tool/i18n"
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/third"
"github.com/eyebluecn/tank/code/tool/util"
"github.com/eyebluecn/tank/code/tool/uuid"
"github.com/jinzhu/gorm"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"log"
"net/http"
"os"
"regexp"
"strconv"
"sync"
"time"
)
@ -62,16 +69,16 @@ func (this *InstallController) Init() {
this.tableNames = []IBase{
&Dashboard{},
&Bridge{},
&DownloadToken{},
&Footprint{},
&ImageCache{},
&Matter{},
&Preference{},
&Session{},
&Share{},
&UploadToken{},
&User{},
//&Bridge{},
//&DownloadToken{},
//&Footprint{},
//&ImageCache{},
//&Matter{},
//&Preference{},
//&Session{},
//&Share{},
//&UploadToken{},
//&User{},
}
}
@ -110,11 +117,19 @@ func (this *InstallController) openDbConnection(writer http.ResponseWriter, requ
this.logger.Info("Connect MySQL %s", mysqlUrl)
var err error = nil
db, err := gorm.Open("mysql", mysqlUrl)
this.PanicError(err)
dbLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // slow SQL 1s
LogLevel: logger.Info, // log level
IgnoreRecordNotFoundError: true, // ignore ErrRecordNotFound
Colorful: false, // colorful print
},
)
db.LogMode(false)
var err error = nil
db, err := gorm.Open(mysql.Open(mysqlUrl), &gorm.Config{Logger: dbLogger})
this.PanicError(err)
return db
@ -123,32 +138,40 @@ func (this *InstallController) openDbConnection(writer http.ResponseWriter, requ
func (this *InstallController) closeDbConnection(db *gorm.DB) {
if db != nil {
err := db.Close()
sqlDB, err := db.DB()
if err != nil {
this.logger.Error("occur error when close db. %v", err)
core.LOGGER.Error("occur error when get *sql.DB %s", err.Error())
}
err = sqlDB.Close()
if err != nil {
core.LOGGER.Error("occur error when closing db %s", err.Error())
}
}
}
func (this *InstallController) getTableMeta(gormDb *gorm.DB, entity IBase) (bool, []*gorm.StructField, []*gorm.StructField) {
// (exists, allFields, missingFields)
func (this *InstallController) getTableMeta(gormDb *gorm.DB, schemaName string, entity IBase) (bool, []*schema.Field, []*schema.Field) {
db := gormDb.Unscoped()
scope := db.NewScope(entity)
//get all useful fields from model.
entitySchema, err := schema.Parse(entity, &sync.Map{}, schema.NamingStrategy{})
this.PanicError(err)
tableName := scope.TableName()
modelStruct := scope.GetModelStruct()
allFields := modelStruct.StructFields
var missingFields = make([]*gorm.StructField, 0)
tableName := entitySchema.Table
allFields := entitySchema.Fields
if !scope.Dialect().HasTable(tableName) {
var missingFields = make([]*schema.Field, 0)
if !gormDb.Migrator().HasTable(tableName) {
missingFields = append(missingFields, allFields...)
return false, allFields, missingFields
} else {
for _, field := range allFields {
if !scope.Dialect().HasColumn(tableName, field.DBName) {
if field.IsNormal {
//tag with `gorm:"-"` will be ""
if field.DBName != "" {
database := gormDb.Migrator().CurrentDatabase()
if !third.MysqlMigratorHasColumn(gormDb, database, tableName, field.DBName) {
missingFields = append(missingFields, field)
}
}
@ -159,12 +182,12 @@ func (this *InstallController) getTableMeta(gormDb *gorm.DB, entity IBase) (bool
}
func (this *InstallController) getTableMetaList(db *gorm.DB) []*InstallTableInfo {
func (this *InstallController) getTableMetaList(db *gorm.DB, mysqlSchema string) []*InstallTableInfo {
var installTableInfos []*InstallTableInfo
for _, iBase := range this.tableNames {
exist, allFields, missingFields := this.getTableMeta(db, iBase)
exist, allFields, missingFields := this.getTableMeta(db, mysqlSchema, iBase)
installTableInfos = append(installTableInfos, &InstallTableInfo{
Name: iBase.TableName(),
TableExist: exist,
@ -204,24 +227,27 @@ func (this *InstallController) Verify(writer http.ResponseWriter, request *http.
defer this.closeDbConnection(db)
this.logger.Info("Ping DB")
err := db.DB().Ping()
phyDb, err := db.DB()
this.PanicError(err)
err = phyDb.Ping()
this.PanicError(err)
return this.Success("OK")
}
func (this *InstallController) TableInfoList(writer http.ResponseWriter, request *http.Request) *result.WebResult {
mysqlSchema := request.FormValue("mysqlSchema")
db := this.openDbConnection(writer, request)
defer this.closeDbConnection(db)
return this.Success(this.getTableMetaList(db))
return this.Success(this.getTableMetaList(db, mysqlSchema))
}
func (this *InstallController) CreateTable(writer http.ResponseWriter, request *http.Request) *result.WebResult {
var installTableInfos []*InstallTableInfo
mysqlSchema := request.FormValue("mysqlSchema")
db := this.openDbConnection(writer, request)
defer this.closeDbConnection(db)
@ -229,9 +255,11 @@ func (this *InstallController) CreateTable(writer http.ResponseWriter, request *
//complete the missing fields or create table. use utf8 charset
db1 := db.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(iBase)
this.PanicError(db1.Error)
if db1.Error() != "" {
panic(result.BadRequest(`migrate table error`))
}
exist, allFields, missingFields := this.getTableMeta(db, iBase)
exist, allFields, missingFields := this.getTableMeta(db, mysqlSchema, iBase)
installTableInfos = append(installTableInfos, &InstallTableInfo{
Name: iBase.TableName(),
TableExist: exist,
@ -365,7 +393,7 @@ func (this *InstallController) Finish(writer http.ResponseWriter, request *http.
defer this.closeDbConnection(db)
//Recheck the integrity of tables.
tableMetaList := this.getTableMetaList(db)
tableMetaList := this.getTableMetaList(db, mysqlSchema)
this.validateTableMetaList(tableMetaList)
//At least one admin

View File

@ -1,13 +1,15 @@
package rest
import "github.com/jinzhu/gorm"
import (
"gorm.io/gorm/schema"
)
/**
* table meta info.
*/
type InstallTableInfo struct {
Name string `json:"name"`
TableExist bool `json:"tableExist"`
AllFields []*gorm.StructField `json:"allFields"`
MissingFields []*gorm.StructField `json:"missingFields"`
Name string `json:"name"`
TableExist bool `json:"tableExist"`
AllFields []*schema.Field `json:"allFields"`
MissingFields []*schema.Field `json:"missingFields"`
}

View File

@ -6,7 +6,7 @@ import (
"github.com/eyebluecn/tank/code/tool/result"
"github.com/eyebluecn/tank/code/tool/util"
"github.com/eyebluecn/tank/code/tool/uuid"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
"math"
"os"
"time"
@ -148,7 +148,7 @@ func (this *MatterDao) CheckByUuidAndUserUuid(uuid string, userUuid string) *Mat
func (this *MatterDao) CountByUserUuidAndPuuidAndDirAndName(userUuid string, puuid string, dir bool, name string) int {
var matter Matter
var count int
var count int64
var wp = &builder.WherePair{}
@ -172,7 +172,7 @@ func (this *MatterDao) CountByUserUuidAndPuuidAndDirAndName(userUuid string, puu
Count(&count)
this.PanicError(db.Error)
return count
return int(count)
}
func (this *MatterDao) FindByUserUuidAndPuuidAndDirAndName(userUuid string, puuid string, dir string, name string) *Matter {
@ -310,7 +310,7 @@ func (this *MatterDao) PlainPage(
conditionDB = core.CONTEXT.GetDB().Model(&Matter{}).Where(wp.Query, wp.Args...)
}
count := 0
var count int64 = 0
db := conditionDB.Count(&count)
this.PanicError(db.Error)
@ -318,7 +318,7 @@ func (this *MatterDao) PlainPage(
db = conditionDB.Order(this.GetSortString(sortArray)).Offset(page * pageSize).Limit(pageSize).Find(&matters)
this.PanicError(db.Error)
return count, matters
return int(count), matters
}
func (this *MatterDao) Page(page int, pageSize int, puuid string, userUuid string, name string, dir string, deleted string, extensions []string, sortArray []builder.OrderPair) *Pager {
@ -387,7 +387,7 @@ func (this *MatterDao) Save(matter *Matter) *Matter {
//download time add 1
func (this *MatterDao) TimesIncrement(matterUuid string) {
db := core.CONTEXT.GetDB().Model(&Matter{}).Where("uuid = ?", matterUuid).Update(map[string]interface{}{"times": gorm.Expr("times + 1"), "visit_time": time.Now()})
db := core.CONTEXT.GetDB().Model(&Matter{}).Where("uuid = ?", matterUuid).Updates(map[string]interface{}{"times": gorm.Expr("times + 1"), "visit_time": time.Now()})
this.PanicError(db.Error)
}
@ -454,7 +454,7 @@ func (this *MatterDao) Delete(matter *Matter) {
func (this *MatterDao) SoftDelete(matter *Matter) {
//soft delete from db.
db := core.CONTEXT.GetDB().Model(&Matter{}).Where("uuid = ?", matter.Uuid).Update(map[string]interface{}{"deleted": true, "delete_time": time.Now()})
db := core.CONTEXT.GetDB().Model(&Matter{}).Where("uuid = ?", matter.Uuid).Updates(map[string]interface{}{"deleted": true, "delete_time": time.Now()})
this.PanicError(db.Error)
}
@ -463,7 +463,7 @@ func (this *MatterDao) SoftDelete(matter *Matter) {
func (this *MatterDao) Recovery(matter *Matter) {
//recovery from db.
db := core.CONTEXT.GetDB().Model(&Matter{}).Where("uuid = ?", matter.Uuid).Update(map[string]interface{}{"deleted": false, "delete_time": time.Now()})
db := core.CONTEXT.GetDB().Model(&Matter{}).Where("uuid = ?", matter.Uuid).Updates(map[string]interface{}{"deleted": false, "delete_time": time.Now()})
this.PanicError(db.Error)
}

View File

@ -4,7 +4,7 @@ import (
"github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/builder"
"github.com/eyebluecn/tank/code/tool/result"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
"github.com/eyebluecn/tank/code/tool/uuid"
"time"
@ -56,7 +56,7 @@ func (this *ShareDao) PlainPage(page int, pageSize int, userUuid string, sortArr
var conditionDB *gorm.DB
conditionDB = core.CONTEXT.GetDB().Model(&Share{}).Where(wp.Query, wp.Args...)
count := 0
var count int64 = 0
db := conditionDB.Count(&count)
this.PanicError(db.Error)
@ -64,7 +64,7 @@ func (this *ShareDao) PlainPage(page int, pageSize int, userUuid string, sortArr
db = conditionDB.Order(this.GetSortString(sortArray)).Offset(page * pageSize).Limit(pageSize).Find(&shares)
this.PanicError(db.Error)
return count, shares
return int(count), shares
}
func (this *ShareDao) Create(share *Share) *Share {

View File

@ -94,7 +94,7 @@ func (this *UserDao) PlainPage(page int, pageSize int, username string, status s
wp = wp.And(&builder.WherePair{Query: "status = ?", Args: []interface{}{status}})
}
count := 0
var count int64 = 0
db := core.CONTEXT.GetDB().Model(&User{}).Where(wp.Query, wp.Args...).Count(&count)
this.PanicError(db.Error)
@ -108,7 +108,7 @@ func (this *UserDao) PlainPage(page int, pageSize int, username string, status s
this.PanicError(db.Error)
return count, users
return int(count), users
}
//handle user page by page.
@ -136,13 +136,13 @@ func (this *UserDao) PageHandle(username string, status string, fun func(user *U
}
func (this *UserDao) CountByUsername(username string) int {
var count int
var count int64
db := core.CONTEXT.GetDB().
Model(&User{}).
Where("username = ?", username).
Count(&count)
this.PanicError(db.Error)
return count
return int(count)
}
func (this *UserDao) Save(user *User) *User {