2018-12-18 18:38:30 +01:00
|
|
|
package tidb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2019-05-01 14:54:11 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2018-12-18 18:38:30 +01:00
|
|
|
)
|
|
|
|
|
2019-05-01 14:54:11 +02:00
|
|
|
func InitDatabase(port uint, password string) error {
|
2019-06-30 00:21:02 +02:00
|
|
|
log.Info("init database")
|
2019-05-01 14:54:11 +02:00
|
|
|
|
2018-12-18 18:38:30 +01:00
|
|
|
db, err := sql.Open("mysql", fmt.Sprintf("root:@tcp(localhost:%d)/", port))
|
|
|
|
|
2019-05-01 14:54:11 +02:00
|
|
|
defer db.Close()
|
|
|
|
|
2018-12-18 18:38:30 +01:00
|
|
|
if err != nil {
|
2019-06-29 23:27:42 +02:00
|
|
|
log.Debug(err.Error())
|
|
|
|
log.Debug("database login as root with password")
|
2019-05-01 14:54:11 +02:00
|
|
|
db, err = sql.Open("mysql", fmt.Sprintf("root:%s@tcp(localhost:%d)/", password, port))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-06-29 23:27:42 +02:00
|
|
|
log.Error(err.Error())
|
2018-12-18 18:38:30 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-29 23:27:42 +02:00
|
|
|
log.Debug("set database password")
|
2018-12-18 18:38:30 +01:00
|
|
|
|
2019-05-01 14:54:11 +02:00
|
|
|
_, err = db.Exec(fmt.Sprintf("SET PASSWORD FOR 'root'@'%%' = '%s'", password))
|
2018-12-18 18:38:30 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2019-06-29 23:27:42 +02:00
|
|
|
log.Error(err.Error())
|
2019-05-01 14:54:11 +02:00
|
|
|
} else {
|
2019-06-29 23:27:42 +02:00
|
|
|
log.Debug("flush database privileges")
|
2019-05-01 14:54:11 +02:00
|
|
|
|
|
|
|
_, err = db.Exec("FLUSH PRIVILEGES")
|
|
|
|
}
|
|
|
|
|
2019-06-29 23:27:42 +02:00
|
|
|
log.Debug("create database if not exists")
|
2019-05-01 14:54:11 +02:00
|
|
|
|
|
|
|
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS photoprism")
|
|
|
|
|
|
|
|
if err != nil {
|
2019-06-29 23:27:42 +02:00
|
|
|
log.Error(err.Error())
|
2018-12-18 18:38:30 +01:00
|
|
|
}
|
|
|
|
|
2019-06-29 23:27:42 +02:00
|
|
|
log.Info("database created")
|
|
|
|
|
2018-12-18 18:38:30 +01:00
|
|
|
return nil
|
|
|
|
}
|