Skip to content

Commit

Permalink
[CONTROLLER/DB] optimizes handling deleting db err
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengya authored and SongZhen0704 committed May 10, 2024
1 parent 9c539d8 commit 66afa0d
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion server/controller/db/mysql/migrator/common/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ func LogDBName(databaseName string, format string, a ...any) string {

func DropDatabase(dc *DBConfig) error {
log.Infof(LogDBName(dc.Config.Database, "drop database"))
return dc.DB.Exec(fmt.Sprintf("DROP DATABASE %s", dc.Config.Database)).Error
var databaseName string
dc.DB.Raw(fmt.Sprintf("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='%s'", dc.Config.Database)).Scan(&databaseName)
if databaseName == dc.Config.Database {
return dc.DB.Exec(fmt.Sprintf("DROP DATABASE %s", dc.Config.Database)).Error
} else {
log.Infof(LogDBName(dc.Config.Database, "database doesn't exist"))
return nil
}
}

func CreateDatabase(dc *DBConfig) error {
Expand Down

0 comments on commit 66afa0d

Please sign in to comment.