Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: separate db connection and table check

LinkinStarc468d2ab2391
diff --git a/cmd/answer/command.go b/cmd/answer/command.go
index b5d83f9f..5d026f77 100644
--- a/cmd/answer/command.go
+++ b/cmd/answer/command.go
@@ -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]")
diff --git a/internal/cli/install_check.go b/internal/cli/install_check.go
index 4526ccd8..6cb25987 100644
--- a/internal/cli/install_check.go
+++ b/internal/cli/install_check.go
@@ -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{})
diff --git a/internal/install/install_controller.go b/internal/install/install_controller.go
index 25552ccf..b4449203 100644
--- a/internal/install/install_controller.go
+++ b/internal/install/install_controller.go
@@ -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)
diff --git a/internal/install/install_req.go b/internal/install/install_req.go
index a3add9ca..534a0fa6 100644
--- a/internal/install/install_req.go
+++ b/internal/install/install_req.go
@@ -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