Finish upgrade the gorm framework.
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user