statping/types/configs/migration.go

129 lines
3.8 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package configs
import (
"fmt"
2020-03-11 04:37:52 +00:00
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/mattn/go-sqlite3"
2020-03-22 07:09:45 +00:00
"github.com/statping/statping/source"
2020-03-14 03:13:20 +00:00
"github.com/statping/statping/types/notifications"
2020-03-22 07:09:45 +00:00
"github.com/statping/statping/utils"
2020-03-14 03:13:20 +00:00
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/services"
"github.com/statping/statping/types/users"
2020-03-04 10:29:00 +00:00
)
2020-04-16 09:57:00 +00:00
func (d *DbConfig) DatabaseChanges() error {
2020-03-11 04:37:52 +00:00
var cr core.Core
2020-04-16 09:57:00 +00:00
d.Db.Model(&core.Core{}).Find(&cr)
2020-03-10 15:52:07 +00:00
2020-03-11 04:37:52 +00:00
if latestMigration > cr.MigrationId {
log.Infof("Statping database is out of date, migrating to: %d", latestMigration)
2020-03-10 15:52:07 +00:00
2020-04-16 09:57:00 +00:00
switch d.Db.DbType() {
2020-03-11 04:37:52 +00:00
case "mysql":
2020-04-16 09:57:00 +00:00
if err := d.genericMigration("MODIFY", false); err != nil {
2020-03-11 04:37:52 +00:00
return err
}
case "postgres":
2020-04-16 09:57:00 +00:00
if err := d.genericMigration("ALTER", true); err != nil {
2020-03-11 04:37:52 +00:00
return err
}
default:
2020-04-16 09:57:00 +00:00
if err := d.sqliteMigration(); err != nil {
2020-03-11 04:37:52 +00:00
return err
}
}
2020-03-10 15:52:07 +00:00
2020-04-16 09:57:00 +00:00
if err := d.Db.Exec(fmt.Sprintf("UPDATE core SET migration_id = %d", latestMigration)).Error(); err != nil {
2020-03-11 04:37:52 +00:00
return err
}
2020-03-10 15:52:07 +00:00
2020-04-16 09:57:00 +00:00
if err := d.BackupAssets(); err != nil {
2020-03-22 07:09:45 +00:00
return err
}
}
return nil
}
// BackupAssets is a temporary function (to version 0.90.*) to backup your customized theme
// to a new folder called 'assets_backup'.
2020-04-16 09:57:00 +00:00
func (d *DbConfig) BackupAssets() error {
2020-03-22 07:09:45 +00:00
if source.UsingAssets(utils.Directory) {
log.Infof("Backing up 'assets' folder to 'assets_backup'")
if err := utils.RenameDirectory(utils.Directory+"/assets", utils.Directory+"/assets_backup"); err != nil {
return err
}
log.Infof("Old assets are now stored in: " + utils.Directory + "/assets_backup")
2020-03-11 04:37:52 +00:00
}
return nil
2020-03-10 15:52:07 +00:00
}
2020-03-04 10:29:00 +00:00
//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-04-16 09:57:00 +00:00
func (d *DbConfig) MigrateDatabase() error {
2020-03-04 10:29:00 +00:00
2020-03-14 03:13:20 +00:00
var DbModels = []interface{}{&services.Service{}, &users.User{}, &hits.Hit{}, &failures.Failure{}, &messages.Message{}, &groups.Group{}, &checkins.Checkin{}, &checkins.CheckinHit{}, &notifications.Notification{}, &incidents.Incident{}, &incidents.IncidentUpdate{}}
2020-03-04 10:29:00 +00:00
log.Infoln("Migrating Database Tables...")
2020-04-16 09:57:00 +00:00
tx := d.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
}
2020-03-22 07:09:45 +00:00
log.Infof("Migrating App to version: %s", core.App.Version)
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-22 07:09:45 +00:00
2020-04-16 09:57:00 +00:00
d.Db.Table("core").Model(&core.Core{}).Update("version", core.App.Version)
2020-03-22 07:09:45 +00:00
2020-03-07 10:59:25 +00:00
log.Infoln("Statping Database Tables Migrated")
2020-04-16 09:57:00 +00:00
if err := d.Db.Model(&hits.Hit{}).AddIndex("idx_service_hit", "service").Error(); err != nil {
2020-03-07 10:59:25 +00:00
log.Errorln(err)
}
2020-04-16 09:57:00 +00:00
if err := d.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-04-16 09:57:00 +00:00
if err := d.Db.Model(&failures.Failure{}).AddIndex("fail_created_at", "created_at").Error(); err != nil {
2020-03-10 15:52:07 +00:00
log.Errorln(err)
}
2020-04-16 09:57:00 +00:00
if err := d.Db.Model(&failures.Failure{}).AddIndex("idx_service_fail", "service").Error(); err != nil {
2020-03-07 10:59:25 +00:00
log.Errorln(err)
}
2020-04-16 09:57:00 +00:00
if err := d.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
}