diff --git a/cmd/main.go b/cmd/main.go index 58077b33..91eabcd3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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")) diff --git a/types/configs/migration.go b/types/configs/migration.go index e441ee55..7d3ac98b 100644 --- a/types/configs/migration.go +++ b/types/configs/migration.go @@ -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) }