ConnectToDatabase validates and connects to the configured database, then tests the connection.
(app *App)
| 581 | // ConnectToDatabase validates and connects to the configured database, then |
| 582 | // tests the connection. |
| 583 | func ConnectToDatabase(app *App) error { |
| 584 | // Check database configuration |
| 585 | if app.cfg.Database.Type == driverMySQL && app.cfg.Database.User == "" { |
| 586 | return fmt.Errorf("Database user not set.") |
| 587 | } |
| 588 | if app.cfg.Database.Host == "" { |
| 589 | app.cfg.Database.Host = "localhost" |
| 590 | } |
| 591 | if app.cfg.Database.Database == "" { |
| 592 | app.cfg.Database.Database = "writefreely" |
| 593 | } |
| 594 | |
| 595 | // TODO: check err |
| 596 | connectToDatabase(app) |
| 597 | |
| 598 | // Test database connection |
| 599 | err := app.db.Ping() |
| 600 | if err != nil { |
| 601 | return fmt.Errorf("Database ping failed: %s", err) |
| 602 | } |
| 603 | log.Info("Connected to database.") |
| 604 | |
| 605 | ver, err := app.db.version() |
| 606 | if err != nil { |
| 607 | log.Error("Unable to get DB version: %v", err) |
| 608 | } else { |
| 609 | log.Info("Database version: %v", ver) |
| 610 | if app.cfg.Database.Type == driverMySQL && strings.HasPrefix(ver, "5.") { |
| 611 | log.Info("Enabling compatibility for MySQL v5.x") |
| 612 | app.db.useSpencerRegex = true |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | return nil |
| 617 | } |
| 618 | |
| 619 | // FormatVersion constructs the version string for the application |
| 620 | func FormatVersion() string { |
no test coverage detected