From 161096fbb2d149583515cadb2018635533876e49 Mon Sep 17 00:00:00 2001 From: lishuang Date: Tue, 15 Mar 2022 02:17:26 +0800 Subject: [PATCH] Finish upgrade the gorm framework. --- code/core/config.go | 4 ++ code/rest/base_model.go | 21 -------- code/rest/bridge_model.go | 17 +++---- code/rest/dashboard_model.go | 32 ++++++------ code/rest/download_token_model.go | 10 ++-- code/rest/footprint_model.go | 26 +++++----- code/rest/image_cache_dao.go | 2 +- code/rest/image_cache_model.go | 34 ++++++------- code/rest/install_controller.go | 81 +++++++++++++++++-------------- code/rest/install_model.go | 20 +++++--- code/rest/matter_dao.go | 2 +- code/rest/matter_model.go | 10 ++-- code/rest/preference_model.go | 38 +++++++-------- code/rest/session_model.go | 11 ++--- code/rest/share_model.go | 11 ++--- code/rest/upload_token_model.go | 10 ++-- code/rest/user_model.go | 11 ++--- code/support/tank_config.go | 9 ++++ code/support/tank_context.go | 5 +- 19 files changed, 167 insertions(+), 187 deletions(-) diff --git a/code/core/config.go b/code/core/config.go index 23c2767..e3bf72d 100644 --- a/code/core/config.go +++ b/code/core/config.go @@ -1,5 +1,7 @@ package core +import "gorm.io/gorm/schema" + const ( //authentication key of cookie COOKIE_AUTH_KEY = "_ak" @@ -22,6 +24,8 @@ type Config interface { MysqlUrl() string //files storage location. MatterPath() string + //table name strategy + NamingStrategy() schema.NamingStrategy //when installed by user. Write configs to tank.json FinishInstall(mysqlPort int, mysqlHost string, mysqlSchema string, mysqlUsername string, mysqlPassword string, mysqlCharset string) } diff --git a/code/rest/base_model.go b/code/rest/base_model.go index 218caf9..f58d03a 100644 --- a/code/rest/base_model.go +++ b/code/rest/base_model.go @@ -1,9 +1,7 @@ package rest import ( - "fmt" "math" - "time" ) const ( @@ -17,25 +15,6 @@ const ( EMPTY_JSON_ARRAY = "[]" ) -type IBase interface { - //name of db table - TableName() string -} - -// Mysql 5.5 only support one CURRENT_TIMESTAMP -// so we use 2018-01-01 00:00:00 as default, which is the first release date of EyeblueTank -type Base struct { - Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` - Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` - UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` - CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` -} - -func (this *Base) TableName() string { - panic(fmt.Sprintf("you should overwrite TableName() in %v", this)) - return "" -} - //pager type Pager struct { Page int `json:"page"` diff --git a/code/rest/bridge_model.go b/code/rest/bridge_model.go index 1165edc..fddb785 100644 --- a/code/rest/bridge_model.go +++ b/code/rest/bridge_model.go @@ -1,18 +1,15 @@ package rest -import ( - "github.com/eyebluecn/tank/code/core" -) +import "time" /** * the link table for Share and Matter. */ type Bridge struct { - Base - ShareUuid string `json:"shareUuid" gorm:"type:char(36)"` - MatterUuid string `json:"matterUuid" gorm:"type:char(36)"` -} - -func (this *Bridge) TableName() string { - return core.TABLE_PREFIX + "bridge" + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` + ShareUuid string `json:"shareUuid" gorm:"type:char(36)"` + MatterUuid string `json:"matterUuid" gorm:"type:char(36)"` } diff --git a/code/rest/dashboard_model.go b/code/rest/dashboard_model.go index 202dd4f..a64bea7 100644 --- a/code/rest/dashboard_model.go +++ b/code/rest/dashboard_model.go @@ -1,27 +1,25 @@ package rest -import "github.com/eyebluecn/tank/code/core" +import "time" /** * application's dashboard. */ type Dashboard struct { - Base - InvokeNum int64 `json:"invokeNum" gorm:"type:bigint(20) not null"` //api invoke num. - TotalInvokeNum int64 `json:"totalInvokeNum" gorm:"type:bigint(20) not null;default:0"` //total invoke num up to now. - Uv int64 `json:"uv" gorm:"type:bigint(20) not null;default:0"` //today's uv - TotalUv int64 `json:"totalUv" gorm:"type:bigint(20) not null;default:0"` //total uv - MatterNum int64 `json:"matterNum" gorm:"type:bigint(20) not null;default:0"` //file's num - TotalMatterNum int64 `json:"totalMatterNum" gorm:"type:bigint(20) not null;default:0"` //file's total number - FileSize int64 `json:"fileSize" gorm:"type:bigint(20) not null;default:0"` //today's file size - TotalFileSize int64 `json:"totalFileSize" gorm:"type:bigint(20) not null;default:0"` //total file's size - AvgCost int64 `json:"avgCost" gorm:"type:bigint(20) not null;default:0"` //api time cost in ms - Dt string `json:"dt" gorm:"type:varchar(45) not null;index:idx_dt"` //date -} - -// set File's table name to be `profiles` -func (this *Dashboard) TableName() string { - return core.TABLE_PREFIX + "dashboard" + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` + InvokeNum int64 `json:"invokeNum" gorm:"type:bigint(20) not null"` //api invoke num. + TotalInvokeNum int64 `json:"totalInvokeNum" gorm:"type:bigint(20) not null;default:0"` //total invoke num up to now. + Uv int64 `json:"uv" gorm:"type:bigint(20) not null;default:0"` //today's uv + TotalUv int64 `json:"totalUv" gorm:"type:bigint(20) not null;default:0"` //total uv + MatterNum int64 `json:"matterNum" gorm:"type:bigint(20) not null;default:0"` //file's num + TotalMatterNum int64 `json:"totalMatterNum" gorm:"type:bigint(20) not null;default:0"` //file's total number + FileSize int64 `json:"fileSize" gorm:"type:bigint(20) not null;default:0"` //today's file size + TotalFileSize int64 `json:"totalFileSize" gorm:"type:bigint(20) not null;default:0"` //total file's size + AvgCost int64 `json:"avgCost" gorm:"type:bigint(20) not null;default:0"` //api time cost in ms + Dt string `json:"dt" gorm:"type:varchar(45) not null;index:idx_dt"` //date } /** diff --git a/code/rest/download_token_model.go b/code/rest/download_token_model.go index ece6c0f..5088972 100644 --- a/code/rest/download_token_model.go +++ b/code/rest/download_token_model.go @@ -1,18 +1,16 @@ package rest import ( - "github.com/eyebluecn/tank/code/core" "time" ) type DownloadToken struct { - Base + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` UserUuid string `json:"userUuid" gorm:"type:char(36) not null"` MatterUuid string `json:"matterUuid" gorm:"type:char(36) not null;index:idx_mu"` ExpireTime time.Time `json:"expireTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` Ip string `json:"ip" gorm:"type:varchar(128) not null"` } - -func (this *DownloadToken) TableName() string { - return core.TABLE_PREFIX + "download_token" -} diff --git a/code/rest/footprint_model.go b/code/rest/footprint_model.go index 37f0d57..4c68f04 100644 --- a/code/rest/footprint_model.go +++ b/code/rest/footprint_model.go @@ -1,22 +1,20 @@ package rest -import "github.com/eyebluecn/tank/code/core" +import "time" /** * visit record. */ type Footprint struct { - Base - UserUuid string `json:"userUuid" gorm:"type:char(36)"` - Ip string `json:"ip" gorm:"type:varchar(128) not null"` - Host string `json:"host" gorm:"type:varchar(45) not null"` - Uri string `json:"uri" gorm:"type:varchar(255) not null"` - Params string `json:"params" gorm:"type:text"` - Cost int64 `json:"cost" gorm:"type:bigint(20) not null;default:0"` - Success bool `json:"success" gorm:"type:tinyint(1) not null;default:0"` -} - -// set File's table name to be `profiles` -func (this *Footprint) TableName() string { - return core.TABLE_PREFIX + "footprint" + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` + UserUuid string `json:"userUuid" gorm:"type:char(36)"` + Ip string `json:"ip" gorm:"type:varchar(128) not null"` + Host string `json:"host" gorm:"type:varchar(45) not null"` + Uri string `json:"uri" gorm:"type:varchar(255) not null"` + Params string `json:"params" gorm:"type:text"` + Cost int64 `json:"cost" gorm:"type:bigint(20) not null;default:0"` + Success bool `json:"success" gorm:"type:tinyint(1) not null;default:0"` } diff --git a/code/rest/image_cache_dao.go b/code/rest/image_cache_dao.go index 03a8995..fddd768 100644 --- a/code/rest/image_cache_dao.go +++ b/code/rest/image_cache_dao.go @@ -67,7 +67,7 @@ func (this *ImageCacheDao) CheckByUuidAndUserUuid(uuid string, userUuid string) // Read var imageCache = &ImageCache{} - db := core.CONTEXT.GetDB().Where(&ImageCache{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(imageCache) + db := core.CONTEXT.GetDB().Where(&ImageCache{Uuid: uuid, UserUuid: userUuid}).First(imageCache) this.PanicError(db.Error) return imageCache diff --git a/code/rest/image_cache_model.go b/code/rest/image_cache_model.go index bc9c26e..2c68e0c 100644 --- a/code/rest/image_cache_model.go +++ b/code/rest/image_cache_model.go @@ -1,29 +1,25 @@ package rest -import ( - "github.com/eyebluecn/tank/code/core" -) +import "time" /** * image cache. */ type ImageCache struct { - Base - Name string `json:"name" gorm:"type:varchar(255) not null"` - UserUuid string `json:"userUuid" gorm:"type:char(36)"` - Username string `json:"username" gorm:"type:varchar(45) not null"` - MatterUuid string `json:"matterUuid" gorm:"type:char(36);index:idx_mu"` - MatterName string `json:"matterName" gorm:"type:varchar(255) not null"` - Mode string `json:"mode" gorm:"type:varchar(512)"` - Md5 string `json:"md5" gorm:"type:varchar(45)"` - Size int64 `json:"size" gorm:"type:bigint(20) not null;default:0"` - Path string `json:"path" gorm:"type:varchar(512)"` - Matter *Matter `json:"matter" gorm:"-"` -} - -// set File's table name to be `profiles` -func (this *ImageCache) TableName() string { - return core.TABLE_PREFIX + "image_cache" + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` + Name string `json:"name" gorm:"type:varchar(255) not null"` + UserUuid string `json:"userUuid" gorm:"type:char(36)"` + Username string `json:"username" gorm:"type:varchar(45) not null"` + MatterUuid string `json:"matterUuid" gorm:"type:char(36);index:idx_mu"` + MatterName string `json:"matterName" gorm:"type:varchar(255) not null"` + Mode string `json:"mode" gorm:"type:varchar(512)"` + Md5 string `json:"md5" gorm:"type:varchar(45)"` + Size int64 `json:"size" gorm:"type:bigint(20) not null;default:0"` + Path string `json:"path" gorm:"type:varchar(512)"` + Matter *Matter `json:"matter" gorm:"-"` } // get the absolute path. path in db means relative path. diff --git a/code/rest/install_controller.go b/code/rest/install_controller.go index ad669a2..67d916a 100644 --- a/code/rest/install_controller.go +++ b/code/rest/install_controller.go @@ -31,7 +31,7 @@ type InstallController struct { matterService *MatterService imageCacheDao *ImageCacheDao imageCacheService *ImageCacheService - tableNames []IBase + tableNames []interface{} } func (this *InstallController) Init() { @@ -67,18 +67,18 @@ func (this *InstallController) Init() { this.imageCacheService = c } - this.tableNames = []IBase{ + this.tableNames = []interface{}{ &Dashboard{}, - //&Bridge{}, - //&DownloadToken{}, - //&Footprint{}, - //&ImageCache{}, - //&Matter{}, - //&Preference{}, - //&Session{}, - //&Share{}, - //&UploadToken{}, - //&User{}, + &Bridge{}, + &DownloadToken{}, + &Footprint{}, + &ImageCache{}, + &Matter{}, + &Preference{}, + &Session{}, + &Share{}, + &UploadToken{}, + &User{}, } } @@ -117,6 +117,7 @@ func (this *InstallController) openDbConnection(writer http.ResponseWriter, requ this.logger.Info("Connect MySQL %s", mysqlUrl) + //log config dbLogger := logger.New( log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer logger.Config{ @@ -126,9 +127,11 @@ func (this *InstallController) openDbConnection(writer http.ResponseWriter, requ Colorful: false, // colorful print }, ) + //table name strategy + namingStrategy := core.CONFIG.NamingStrategy() var err error = nil - db, err := gorm.Open(mysql.Open(mysqlUrl), &gorm.Config{Logger: dbLogger}) + db, err := gorm.Open(mysql.Open(mysqlUrl), &gorm.Config{Logger: dbLogger, NamingStrategy: namingStrategy}) this.PanicError(err) return db @@ -149,47 +152,56 @@ func (this *InstallController) closeDbConnection(db *gorm.DB) { } } -// (exists, allFields, missingFields) -func (this *InstallController) getTableMeta(gormDb *gorm.DB, schemaName string, entity IBase) (bool, []*schema.Field, []*schema.Field) { +// (tableName, exists, allFields, missingFields) +func (this *InstallController) getTableMeta(gormDb *gorm.DB, entity interface{}) (string, bool, []*InstallFieldInfo, []*InstallFieldInfo) { //get all useful fields from model. - entitySchema, err := schema.Parse(entity, &sync.Map{}, schema.NamingStrategy{}) + entitySchema, err := schema.Parse(entity, &sync.Map{}, core.CONFIG.NamingStrategy()) this.PanicError(err) tableName := entitySchema.Table - allFields := entitySchema.Fields - var missingFields = make([]*schema.Field, 0) + var allFields = make([]*InstallFieldInfo, 0) + + for _, field := range entitySchema.Fields { + allFields = append(allFields, &InstallFieldInfo{ + Name: field.DBName, + DataType: string(field.DataType), + }) + } + + var missingFields = make([]*InstallFieldInfo, 0) if !gormDb.Migrator().HasTable(tableName) { missingFields = append(missingFields, allFields...) - return false, allFields, missingFields + return tableName, false, allFields, missingFields } else { for _, field := range allFields { //tag with `gorm:"-"` will be "" - if field.DBName != "" { + if field.Name != "" { database := gormDb.Migrator().CurrentDatabase() - if !third.MysqlMigratorHasColumn(gormDb, database, tableName, field.DBName) { + if !third.MysqlMigratorHasColumn(gormDb, database, tableName, field.Name) { missingFields = append(missingFields, field) } } } - return true, allFields, missingFields + return tableName, true, allFields, missingFields } } -func (this *InstallController) getTableMetaList(db *gorm.DB, mysqlSchema string) []*InstallTableInfo { +func (this *InstallController) getTableMetaList(db *gorm.DB) []*InstallTableInfo { var installTableInfos []*InstallTableInfo for _, iBase := range this.tableNames { - exist, allFields, missingFields := this.getTableMeta(db, mysqlSchema, iBase) + tableName, exist, allFields, missingFields := this.getTableMeta(db, iBase) + installTableInfos = append(installTableInfos, &InstallTableInfo{ - Name: iBase.TableName(), + Name: tableName, TableExist: exist, AllFields: allFields, MissingFields: missingFields, @@ -208,7 +220,7 @@ func (this *InstallController) validateTableMetaList(tableInfoList []*InstallTab var strs []string for _, v := range tableInfo.MissingFields { - strs = append(strs, v.DBName) + strs = append(strs, v.Name) } panic(result.BadRequest(fmt.Sprintf("table %s miss the following fields %v", tableInfo.Name, strs))) @@ -236,32 +248,29 @@ func (this *InstallController) Verify(writer http.ResponseWriter, request *http. } 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, mysqlSchema)) + return this.Success(this.getTableMetaList(db)) } func (this *InstallController) CreateTable(writer http.ResponseWriter, request *http.Request) *result.WebResult { var installTableInfos []*InstallTableInfo - mysqlSchema := request.FormValue("mysqlSchema") + mysqlCharset := request.FormValue("mysqlCharset") db := this.openDbConnection(writer, request) defer this.closeDbConnection(db) for _, iBase := range this.tableNames { //complete the missing fields or create table. use utf8 charset - db1 := db.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(iBase) - if db1.Error() != "" { - panic(result.BadRequest(`migrate table error`)) - } + err := db.Set("gorm:table_options", fmt.Sprintf("CHARSET=%s", mysqlCharset)).AutoMigrate(iBase) + this.PanicError(err) - exist, allFields, missingFields := this.getTableMeta(db, mysqlSchema, iBase) + tableName, exist, allFields, missingFields := this.getTableMeta(db, iBase) installTableInfos = append(installTableInfos, &InstallTableInfo{ - Name: iBase.TableName(), + Name: tableName, TableExist: exist, AllFields: allFields, MissingFields: missingFields, @@ -393,7 +402,7 @@ func (this *InstallController) Finish(writer http.ResponseWriter, request *http. defer this.closeDbConnection(db) //Recheck the integrity of tables. - tableMetaList := this.getTableMetaList(db, mysqlSchema) + tableMetaList := this.getTableMetaList(db) this.validateTableMetaList(tableMetaList) //At least one admin diff --git a/code/rest/install_model.go b/code/rest/install_model.go index d5f4628..fed60ef 100644 --- a/code/rest/install_model.go +++ b/code/rest/install_model.go @@ -1,15 +1,19 @@ package rest -import ( - "gorm.io/gorm/schema" -) - /** * table meta info. */ type InstallTableInfo struct { - Name string `json:"name"` - TableExist bool `json:"tableExist"` - AllFields []*schema.Field `json:"allFields"` - MissingFields []*schema.Field `json:"missingFields"` + Name string `json:"name"` + TableExist bool `json:"tableExist"` + AllFields []*InstallFieldInfo `json:"allFields"` + MissingFields []*InstallFieldInfo `json:"missingFields"` +} + +/** + * table meta info. + */ +type InstallFieldInfo struct { + Name string `json:"name"` + DataType string `json:"dataType"` } diff --git a/code/rest/matter_dao.go b/code/rest/matter_dao.go index cfb25f9..6a0aaea 100644 --- a/code/rest/matter_dao.go +++ b/code/rest/matter_dao.go @@ -138,7 +138,7 @@ func (this *MatterDao) FindByUserUuidAndPuuidAndDirTrue(userUuid string, puuid s func (this *MatterDao) CheckByUuidAndUserUuid(uuid string, userUuid string) *Matter { var matter = &Matter{} - db := core.CONTEXT.GetDB().Where(&Matter{Base: Base{Uuid: uuid}, UserUuid: userUuid}).First(matter) + db := core.CONTEXT.GetDB().Where(&Matter{Uuid: uuid, UserUuid: userUuid}).First(matter) this.PanicError(db.Error) return matter diff --git a/code/rest/matter_model.go b/code/rest/matter_model.go index aaaecf5..842f86a 100644 --- a/code/rest/matter_model.go +++ b/code/rest/matter_model.go @@ -30,7 +30,10 @@ const ( * file is too common. so we use matter as file. */ type Matter struct { - Base + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` Puuid string `json:"puuid" gorm:"type:char(36);index:idx_puuid"` UserUuid string `json:"userUuid" gorm:"type:char(36);index:idx_uu"` Username string `json:"username" gorm:"type:varchar(45) not null"` @@ -49,11 +52,6 @@ type Matter struct { DeleteTime time.Time `json:"deleteTime" gorm:"type:timestamp not null;index:idx_delt;default:'2018-01-01 00:00:00'"` } -// set File's table name to be `profiles` -func (Matter) TableName() string { - return core.TABLE_PREFIX + "matter" -} - // get matter's absolute path. the Path property is relative path in db. func (this *Matter) AbsolutePath() string { return GetUserMatterRootDir(this.Username) + this.Path diff --git a/code/rest/preference_model.go b/code/rest/preference_model.go index 304d342..faab532 100644 --- a/code/rest/preference_model.go +++ b/code/rest/preference_model.go @@ -1,30 +1,28 @@ package rest import ( - "github.com/eyebluecn/tank/code/core" jsoniter "github.com/json-iterator/go" + "time" ) type Preference struct { - Base - Name string `json:"name" gorm:"type:varchar(45)"` - LogoUrl string `json:"logoUrl" gorm:"type:varchar(255)"` - FaviconUrl string `json:"faviconUrl" gorm:"type:varchar(255)"` - Copyright string `json:"copyright" gorm:"type:varchar(1024)"` - Record string `json:"record" gorm:"type:varchar(1024)"` - DownloadDirMaxSize int64 `json:"downloadDirMaxSize" gorm:"type:bigint(20) not null;default:-1"` - DownloadDirMaxNum int64 `json:"downloadDirMaxNum" gorm:"type:bigint(20) not null;default:-1"` - DefaultTotalSizeLimit int64 `json:"defaultTotalSizeLimit" gorm:"type:bigint(20) not null;default:-1"` - AllowRegister bool `json:"allowRegister" gorm:"type:tinyint(1) not null;default:0"` - PreviewConfig string `json:"previewConfig" gorm:"type:text"` - ScanConfig string `json:"scanConfig" gorm:"type:text"` - DeletedKeepDays int64 `json:"deletedKeepDays" gorm:"type:bigint(20) not null;default:7"` - Version string `json:"version" gorm:"-"` -} - -// set File's table name to be `profiles` -func (this *Preference) TableName() string { - return core.TABLE_PREFIX + "preference" + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` + Name string `json:"name" gorm:"type:varchar(45)"` + LogoUrl string `json:"logoUrl" gorm:"type:varchar(255)"` + FaviconUrl string `json:"faviconUrl" gorm:"type:varchar(255)"` + Copyright string `json:"copyright" gorm:"type:varchar(1024)"` + Record string `json:"record" gorm:"type:varchar(1024)"` + DownloadDirMaxSize int64 `json:"downloadDirMaxSize" gorm:"type:bigint(20) not null;default:-1"` + DownloadDirMaxNum int64 `json:"downloadDirMaxNum" gorm:"type:bigint(20) not null;default:-1"` + DefaultTotalSizeLimit int64 `json:"defaultTotalSizeLimit" gorm:"type:bigint(20) not null;default:-1"` + AllowRegister bool `json:"allowRegister" gorm:"type:tinyint(1) not null;default:0"` + PreviewConfig string `json:"previewConfig" gorm:"type:text"` + ScanConfig string `json:"scanConfig" gorm:"type:text"` + DeletedKeepDays int64 `json:"deletedKeepDays" gorm:"type:bigint(20) not null;default:7"` + Version string `json:"version" gorm:"-"` } const ( diff --git a/code/rest/session_model.go b/code/rest/session_model.go index 079924f..dd044ac 100644 --- a/code/rest/session_model.go +++ b/code/rest/session_model.go @@ -1,18 +1,15 @@ package rest import ( - "github.com/eyebluecn/tank/code/core" "time" ) type Session struct { - Base + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` UserUuid string `json:"userUuid" gorm:"type:char(36)"` Ip string `json:"ip" gorm:"type:varchar(128) not null"` ExpireTime time.Time `json:"expireTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` } - -// set User's table name to be `profiles` -func (this *Session) TableName() string { - return core.TABLE_PREFIX + "session" -} diff --git a/code/rest/share_model.go b/code/rest/share_model.go index 1362136..996629c 100644 --- a/code/rest/share_model.go +++ b/code/rest/share_model.go @@ -1,7 +1,6 @@ package rest import ( - "github.com/eyebluecn/tank/code/core" "time" ) @@ -22,7 +21,10 @@ const ( * share record */ type Share struct { - Base + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` Name string `json:"name" gorm:"type:varchar(255)"` ShareType string `json:"shareType" gorm:"type:varchar(45)"` Username string `json:"username" gorm:"type:varchar(45)"` @@ -34,8 +36,3 @@ type Share struct { DirMatter *Matter `json:"dirMatter" gorm:"-"` Matters []*Matter `json:"matters" gorm:"-"` } - -// set File's table name to be `profiles` -func (this *Share) TableName() string { - return core.TABLE_PREFIX + "share" -} diff --git a/code/rest/upload_token_model.go b/code/rest/upload_token_model.go index 2ea3880..efe921b 100644 --- a/code/rest/upload_token_model.go +++ b/code/rest/upload_token_model.go @@ -1,12 +1,14 @@ package rest import ( - "github.com/eyebluecn/tank/code/core" "time" ) type UploadToken struct { - Base + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` UserUuid string `json:"userUuid" gorm:"type:char(36) not null"` FolderUuid string `json:"folderUuid" gorm:"type:char(36) not null"` MatterUuid string `json:"matterUuid" gorm:"type:char(36) not null"` @@ -16,7 +18,3 @@ type UploadToken struct { Size int64 `json:"size" gorm:"type:bigint(20) not null;default:0"` Ip string `json:"ip" gorm:"type:varchar(128) not null"` } - -func (this *UploadToken) TableName() string { - return core.TABLE_PREFIX + "upload_token" -} diff --git a/code/rest/user_model.go b/code/rest/user_model.go index bdd0eef..f2bc0cc 100644 --- a/code/rest/user_model.go +++ b/code/rest/user_model.go @@ -1,7 +1,6 @@ package rest import ( - "github.com/eyebluecn/tank/code/core" "time" ) @@ -28,7 +27,10 @@ const ( ) type User struct { - Base + Uuid string `json:"uuid" gorm:"type:char(36);primary_key;unique"` + Sort int64 `json:"sort" gorm:"type:bigint(20) not null"` + UpdateTime time.Time `json:"updateTime" gorm:"type:timestamp not null;default:CURRENT_TIMESTAMP"` + CreateTime time.Time `json:"createTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"` Role string `json:"role" gorm:"type:varchar(45)"` Username string `json:"username" gorm:"type:varchar(45) not null;unique"` Password string `json:"-" gorm:"type:varchar(255)"` @@ -40,8 +42,3 @@ type User struct { TotalSize int64 `json:"totalSize" gorm:"type:bigint(20) not null;default:0"` Status string `json:"status" gorm:"type:varchar(45)"` } - -// set User's table name to be `profiles` -func (this *User) TableName() string { - return core.TABLE_PREFIX + "user" -} diff --git a/code/support/tank_config.go b/code/support/tank_config.go index 823db75..e28a858 100644 --- a/code/support/tank_config.go +++ b/code/support/tank_config.go @@ -4,6 +4,7 @@ import ( "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/util" "github.com/json-iterator/go" + "gorm.io/gorm/schema" "io/ioutil" "os" "time" @@ -178,6 +179,14 @@ func (this *TankConfig) MatterPath() string { return this.matterPath } +//matter path +func (this *TankConfig) NamingStrategy() schema.NamingStrategy { + return schema.NamingStrategy{ + TablePrefix: core.TABLE_PREFIX, + SingularTable: true, + } +} + //Finish the installation. Write config to tank.json func (this *TankConfig) FinishInstall(mysqlPort int, mysqlHost string, mysqlSchema string, mysqlUsername string, mysqlPassword string, mysqlCharset string) { diff --git a/code/support/tank_context.go b/code/support/tank_context.go index f56d530..318a026 100644 --- a/code/support/tank_context.go +++ b/code/support/tank_context.go @@ -75,6 +75,7 @@ func (this *TankContext) ServeHTTP(writer http.ResponseWriter, request *http.Req func (this *TankContext) OpenDb() { + //log strategy. dbLogger := logger.New( log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer logger.Config{ @@ -84,9 +85,11 @@ func (this *TankContext) OpenDb() { Colorful: false, // colorful print }, ) + //table name strategy. + namingStrategy := core.CONFIG.NamingStrategy() var err error = nil - this.db, err = gorm.Open(mysql.Open(core.CONFIG.MysqlUrl()), &gorm.Config{Logger: dbLogger}) + this.db, err = gorm.Open(mysql.Open(core.CONFIG.MysqlUrl()), &gorm.Config{Logger: dbLogger, NamingStrategy: namingStrategy}) if err != nil { core.LOGGER.Panic("failed to connect mysql database")