mirror of https://gitee.com/answerdev/answer.git
feat: separate db connection and table check
This commit is contained in:
parent
7e11d6b82c
commit
c468d2ab23
|
@ -73,7 +73,7 @@ To run answer, use:
|
|||
}
|
||||
|
||||
fmt.Println("config file read successfully, try to connect database...")
|
||||
if cli.CheckDB(c.Data.Database, true) {
|
||||
if cli.CheckDBTableExist(c.Data.Database) {
|
||||
fmt.Println("connect to database successfully and table already exists, do nothing.")
|
||||
return
|
||||
}
|
||||
|
@ -152,7 +152,7 @@ To run answer, use:
|
|||
return
|
||||
}
|
||||
|
||||
if cli.CheckDB(c.Data.Database, false) {
|
||||
if cli.CheckDBConnection(c.Data.Database) {
|
||||
fmt.Println("db connection successfully [✔]")
|
||||
} else {
|
||||
fmt.Println("db connection failed [x]")
|
||||
|
|
|
@ -16,9 +16,8 @@ func CheckUploadDir() bool {
|
|||
return dir.CheckDirExist(UploadFilePath)
|
||||
}
|
||||
|
||||
// CheckDB check database whether the connection is normal
|
||||
// if mustInstalled is true, will check table if already exists
|
||||
func CheckDB(dataConf *data.Database, mustInstalled bool) bool {
|
||||
// CheckDBConnection check database whether the connection is normal
|
||||
func CheckDBConnection(dataConf *data.Database) bool {
|
||||
db, err := data.NewDB(false, dataConf)
|
||||
if err != nil {
|
||||
fmt.Printf("connection database failed: %s\n", err)
|
||||
|
@ -28,8 +27,20 @@ func CheckDB(dataConf *data.Database, mustInstalled bool) bool {
|
|||
fmt.Printf("connection ping database failed: %s\n", err)
|
||||
return false
|
||||
}
|
||||
if !mustInstalled {
|
||||
return true
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// CheckDBTableExist check database whether the table is already exists
|
||||
func CheckDBTableExist(dataConf *data.Database) bool {
|
||||
db, err := data.NewDB(false, dataConf)
|
||||
if err != nil {
|
||||
fmt.Printf("connection database failed: %s\n", err)
|
||||
return false
|
||||
}
|
||||
if err = db.Ping(); err != nil {
|
||||
fmt.Printf("connection ping database failed: %s\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
exist, err := db.IsTableExist(&entity.Version{})
|
||||
|
|
|
@ -52,7 +52,10 @@ func CheckConfigFile(ctx *gin.Context) {
|
|||
handler.HandleResponse(ctx, err, nil)
|
||||
return
|
||||
}
|
||||
resp.DbTableExist = cli.CheckDB(allConfig.Data.Database, true)
|
||||
resp.DBConnectionSuccess = cli.CheckDBConnection(allConfig.Data.Database)
|
||||
if resp.DBConnectionSuccess {
|
||||
resp.DbTableExist = cli.CheckDBTableExist(allConfig.Data.Database)
|
||||
}
|
||||
handler.HandleResponse(ctx, nil, resp)
|
||||
}
|
||||
|
||||
|
@ -76,7 +79,7 @@ func CheckDatabase(ctx *gin.Context) {
|
|||
Driver: req.DbType,
|
||||
Connection: req.GetConnection(),
|
||||
}
|
||||
resp.ConnectionSuccess = cli.CheckDB(dataConf, false)
|
||||
resp.ConnectionSuccess = cli.CheckDBConnection(dataConf)
|
||||
if !resp.ConnectionSuccess {
|
||||
handler.HandleResponse(ctx, errors.BadRequest(reason.DatabaseConnectionFailed), schema.ErrTypeAlert)
|
||||
return
|
||||
|
@ -99,6 +102,13 @@ func InitEnvironment(ctx *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
// check config file if exist
|
||||
if cli.CheckConfigFile(confPath) {
|
||||
log.Debugf("config file already exists")
|
||||
handler.HandleResponse(ctx, nil, nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := cli.InstallConfigFile(confPath); err != nil {
|
||||
handler.HandleResponse(ctx, errors.BadRequest(reason.InstallConfigFailed), &InitEnvironmentResp{
|
||||
Success: false,
|
||||
|
@ -152,6 +162,12 @@ func InitBaseInfo(ctx *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if cli.CheckDBTableExist(c.Data.Database) {
|
||||
log.Warnf("database is already initialized")
|
||||
handler.HandleResponse(ctx, nil, nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := migrations.InitDB(c.Data.Database); err != nil {
|
||||
log.Error("init database error: ", err.Error())
|
||||
handler.HandleResponse(ctx, errors.BadRequest(reason.DatabaseConnectionFailed), schema.ErrTypeAlert)
|
||||
|
|
|
@ -10,8 +10,9 @@ import (
|
|||
|
||||
// CheckConfigFileResp check config file if exist or not response
|
||||
type CheckConfigFileResp struct {
|
||||
ConfigFileExist bool `json:"config_file_exist"`
|
||||
DbTableExist bool `json:"db_table_exist"`
|
||||
ConfigFileExist bool `json:"config_file_exist"`
|
||||
DBConnectionSuccess bool `json:"db_connection_success"`
|
||||
DbTableExist bool `json:"db_table_exist"`
|
||||
}
|
||||
|
||||
// CheckDatabaseReq check database
|
||||
|
|
Loading…
Reference in New Issue