pull/429/head
hunterlong 2020-03-10 08:52:07 -07:00
parent e7ece19e70
commit b0ea8b3621
2 changed files with 32 additions and 0 deletions

View File

@ -131,8 +131,17 @@ func main() {
exit(err)
}
if err = confgs.VerifyMigration(); err != nil {
exit(err)
}
exists := confgs.Db.HasTable("core")
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 {
exit(errors.Wrap(err, "error dropping database"))

View File

@ -42,6 +42,25 @@ import (
// 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.
//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.
@ -82,6 +101,10 @@ func (c *DbConfig) MigrateDatabase() error {
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 {
log.Errorln(err)
}