statping/types/configs/database.go

114 lines
2.8 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package configs
import (
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/database"
"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"
2020-03-14 03:13:20 +00:00
"github.com/statping/statping/types/notifications"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/services"
"github.com/statping/statping/types/users"
"github.com/statping/statping/utils"
2020-03-04 14:20:47 +00:00
"gopkg.in/yaml.v2"
2020-03-04 10:29:00 +00:00
"os"
)
2020-03-09 15:15:15 +00:00
type SamplerFunc func() error
2020-03-04 14:20:47 +00:00
2020-03-04 10:29:00 +00:00
type Sampler interface {
Samples() []database.DbObject
}
func TriggerSamples() error {
return createSamples(
2020-03-04 14:20:47 +00:00
core.Samples,
2020-03-09 15:15:15 +00:00
//users.Samples,
2020-03-04 14:20:47 +00:00
messages.Samples,
services.Samples,
checkins.Samples,
checkins.SamplesChkHits,
failures.Samples,
groups.Samples,
hits.Samples,
incidents.Samples,
2020-03-04 10:29:00 +00:00
)
}
2020-03-04 14:20:47 +00:00
func createSamples(sm ...SamplerFunc) error {
2020-03-04 10:29:00 +00:00
for _, v := range sm {
2020-03-09 15:15:15 +00:00
if err := v(); err != nil {
return err
}
2020-03-04 10:29:00 +00:00
}
return nil
}
// Migrate function
func (d *DbConfig) Update() error {
var err error
config, err := os.Create(utils.Directory + "/config.yml")
if err != nil {
return err
}
defer config.Close()
data, err := yaml.Marshal(d)
if err != nil {
log.Errorln(err)
return err
}
config.WriteString(string(data))
return nil
}
// Save will initially create the config.yml file
func (d *DbConfig) Delete() error {
return os.Remove(d.filename)
}
// DropDatabase will DROP each table Statping created
func (d *DbConfig) DropDatabase() error {
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("Dropping Database Tables...")
for _, t := range DbModels {
2020-03-10 05:24:35 +00:00
if err := d.Db.DropTableIfExists(t); err != nil {
2020-03-04 10:29:00 +00:00
return err.Error()
}
log.Infof("Dropped table: %T\n", t)
}
return nil
}
2020-03-10 05:24:35 +00:00
func (d *DbConfig) Close() {
2020-03-16 06:51:15 +00:00
if d == nil {
return
}
2020-03-13 04:06:06 +00:00
if d.Db != nil {
d.Db.Close()
}
2020-03-10 05:24:35 +00:00
}
2020-03-04 10:29:00 +00:00
// CreateDatabase will CREATE TABLES for each of the Statping elements
2020-03-10 05:24:35 +00:00
func (d *DbConfig) CreateDatabase() error {
2020-03-04 10:29:00 +00:00
var err error
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("Creating Database Tables...")
for _, table := range DbModels {
2020-03-10 05:24:35 +00:00
if err := d.Db.CreateTable(table); err.Error() != nil {
2020-03-04 10:29:00 +00:00
return err.Error()
}
}
2020-03-10 05:24:35 +00:00
if err := d.Db.Table("core").CreateTable(&core.Core{}); err.Error() != nil {
2020-03-04 10:29:00 +00:00
return err.Error()
}
log.Infoln("Statping Database Created")
2020-03-04 14:20:47 +00:00
2020-03-04 10:29:00 +00:00
return err
}