mirror of https://github.com/statping/statping
pull/429/head
parent
e7ece19e70
commit
b0ea8b3621
|
@ -131,8 +131,17 @@ func main() {
|
||||||
exit(err)
|
exit(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = confgs.VerifyMigration(); err != nil {
|
||||||
|
exit(err)
|
||||||
|
}
|
||||||
|
|
||||||
exists := confgs.Db.HasTable("core")
|
exists := confgs.Db.HasTable("core")
|
||||||
if !exists {
|
if !exists {
|
||||||
|
var srvs int64
|
||||||
|
confgs.Db.Model(&services.Service{}).Count(&srvs)
|
||||||
|
if srvs > 0 {
|
||||||
|
exit(errors.Wrap(err, "there are already services setup."))
|
||||||
|
}
|
||||||
|
|
||||||
if err := confgs.DropDatabase(); err != nil {
|
if err := confgs.DropDatabase(); err != nil {
|
||||||
exit(errors.Wrap(err, "error dropping database"))
|
exit(errors.Wrap(err, "error dropping database"))
|
||||||
|
|
|
@ -42,6 +42,25 @@ import (
|
||||||
// return nil
|
// return nil
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
func (c *DbConfig) VerifyMigration() error {
|
||||||
|
|
||||||
|
query := `
|
||||||
|
BEGIN TRANSACTION;
|
||||||
|
ALTER TABLE hits ALTER COLUMN latency BIGINT;
|
||||||
|
ALTER TABLE hits ALTER COLUMN ping_time BIGINT;
|
||||||
|
ALTER TABLE failures ALTER COLUMN ping_time BIGINT;
|
||||||
|
UPDATE hits SET latency = CAST(latency * 10000 AS BIGINT);
|
||||||
|
UPDATE hits SET ping_time = CAST(ping_time * 100000 AS BIGINT);
|
||||||
|
UPDATE failures SET ping_time = CAST(ping_time * 100000 AS BIGINT);
|
||||||
|
COMMIT;`
|
||||||
|
|
||||||
|
fmt.Println(c.Db.DbType())
|
||||||
|
|
||||||
|
q := c.Db.Raw(query).Debug()
|
||||||
|
|
||||||
|
return q.Error()
|
||||||
|
}
|
||||||
|
|
||||||
//MigrateDatabase will migrate the database structure to current version.
|
//MigrateDatabase will migrate the database structure to current version.
|
||||||
//This function will NOT remove previous records, tables or columns from the database.
|
//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.
|
//If this function has an issue, it will ROLLBACK to the previous state.
|
||||||
|
@ -82,6 +101,10 @@ func (c *DbConfig) MigrateDatabase() error {
|
||||||
log.Errorln(err)
|
log.Errorln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := c.Db.Model(&failures.Failure{}).AddIndex("fail_created_at", "created_at").Error(); err != nil {
|
||||||
|
log.Errorln(err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := c.Db.Model(&failures.Failure{}).AddIndex("idx_service_fail", "service").Error(); err != nil {
|
if err := c.Db.Model(&failures.Failure{}).AddIndex("idx_service_fail", "service").Error(); err != nil {
|
||||||
log.Errorln(err)
|
log.Errorln(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue