-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: explicit milliseconds precision of timestamp columns
- Loading branch information
Showing
6 changed files
with
64 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/emvi/logbuch" | ||
"github.com/muety/wakapi/config" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func init() { | ||
const name = "20220318-mysql_timestamp_precision" | ||
f := migrationFunc{ | ||
name: name, | ||
f: func(db *gorm.DB, cfg *config.Config) error { | ||
if hasRun(name, db) { | ||
return nil | ||
} | ||
|
||
if cfg.Db.IsMySQL() { | ||
logbuch.Info("altering heartbeats table, this may take a while (up to hours)") | ||
|
||
db.Exec("SET foreign_key_checks=0;") | ||
db.Exec("SET unique_checks=0;") | ||
if err := db.Exec("ALTER TABLE heartbeats MODIFY COLUMN `time` TIMESTAMP(3) NOT NULL").Error; err != nil { | ||
return err | ||
} | ||
if err := db.Exec("ALTER TABLE heartbeats MODIFY COLUMN `created_at` TIMESTAMP(3) NOT NULL").Error; err != nil { | ||
return err | ||
} | ||
db.Exec("SET foreign_key_checks=1;") | ||
db.Exec("SET unique_checks=1;") | ||
|
||
logbuch.Info("migrated timestamp columns to millisecond precision") | ||
} | ||
|
||
setHasRun(name, db) | ||
return nil | ||
}, | ||
} | ||
|
||
registerPostMigration(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
DELETE t1 | ||
FROM heartbeats t1 | ||
INNER JOIN heartbeats t2 | ||
WHERE t1.id < t2.id | ||
AND t1.time = t2.time | ||
AND t1.entity = t2.entity | ||
AND t1.is_write = t2.is_write | ||
AND t1.branch = t2.branch | ||
AND t1.editor = t2.editor | ||
AND t1.machine = t2.machine | ||
AND t1.operating_system = t2.operating_system | ||
AND t1.user_id = t2.user_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters