2020-03-04 10:29:00 +00:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/checkins"
|
|
|
|
"github.com/statping/statping/types/core"
|
|
|
|
"github.com/statping/statping/types/failures"
|
|
|
|
"github.com/statping/statping/types/groups"
|
|
|
|
"github.com/statping/statping/types/hits"
|
|
|
|
"github.com/statping/statping/types/incidents"
|
|
|
|
"github.com/statping/statping/types/messages"
|
|
|
|
"github.com/statping/statping/types/notifications"
|
|
|
|
"github.com/statping/statping/types/services"
|
|
|
|
"github.com/statping/statping/types/users"
|
2020-03-06 09:33:46 +00:00
|
|
|
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
2020-03-04 10:29:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertNotifierDB inject the Statping database instance to the Notifier package
|
|
|
|
//func (c *DbConfig) InsertNotifierDB() error {
|
|
|
|
// if !database.Available() {
|
|
|
|
// err := c.Connect()
|
|
|
|
// if err != nil {
|
|
|
|
// return errors.New("database connection has not been created")
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// notifiers.SetDB(database.DB())
|
|
|
|
// return nil
|
|
|
|
//}
|
|
|
|
|
|
|
|
// InsertIntegratorDB inject the Statping database instance to the Integrations package
|
|
|
|
//func (c *DbConfig) InsertIntegratorDB() error {
|
|
|
|
// if !database.Available() {
|
|
|
|
// err := c.Connect()
|
|
|
|
// if err != nil {
|
|
|
|
// return errors.Wrap(err,"database connection has not been created")
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// integrations.SetDB(database.DB())
|
|
|
|
// return nil
|
|
|
|
//}
|
|
|
|
|
|
|
|
//MigrateDatabase will migrate the database structure to current version.
|
|
|
|
//This function will NOT remove previous records, tables or columns from the database.
|
|
|
|
//If this function has an issue, it will ROLLBACK to the previous state.
|
2020-03-04 14:20:47 +00:00
|
|
|
func (c *DbConfig) MigrateDatabase() error {
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2020-03-09 15:15:15 +00:00
|
|
|
var DbModels = []interface{}{&services.Service{}, &users.User{}, &hits.Hit{}, &failures.Failure{}, &messages.Message{}, &groups.Group{}, &checkins.Checkin{}, &checkins.CheckinHit{}, ¬ifications.Notification{}, &incidents.Incident{}, &incidents.IncidentUpdate{}}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
log.Infoln("Migrating Database Tables...")
|
2020-03-10 05:24:35 +00:00
|
|
|
tx := c.Db.Begin()
|
2020-03-04 10:29:00 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for _, table := range DbModels {
|
|
|
|
tx = tx.AutoMigrate(table)
|
2020-03-04 14:20:47 +00:00
|
|
|
if tx.Error() != nil {
|
|
|
|
log.Errorln(tx.Error())
|
|
|
|
return tx.Error()
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
if err := tx.Table("core").AutoMigrate(&core.Core{}); err.Error() != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
log.Errorln(fmt.Sprintf("Statping Database could not be migrated: %v", tx.Error()))
|
|
|
|
return tx.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit().Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-07 10:59:25 +00:00
|
|
|
log.Infoln("Statping Database Tables Migrated")
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err := c.Db.Model(&hits.Hit{}).AddIndex("idx_service_hit", "service").Error(); err != nil {
|
2020-03-07 10:59:25 +00:00
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err := c.Db.Model(&hits.Hit{}).AddIndex("hit_created_at", "created_at").Error(); err != nil {
|
2020-03-07 10:59:25 +00:00
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err := c.Db.Model(&failures.Failure{}).AddIndex("idx_service_fail", "service").Error(); err != nil {
|
2020-03-07 10:59:25 +00:00
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err := c.Db.Model(&failures.Failure{}).AddIndex("idx_checkin_fail", "checkin").Error(); err != nil {
|
2020-03-07 10:59:25 +00:00
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-03-09 15:15:15 +00:00
|
|
|
log.Infoln("Database Indexes Created")
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|