2021-12-15 12:50:16 +03:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/emvi/logbuch"
|
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
const name = "20211215-migrate_id_to_bigint-add_has_data_field"
|
|
|
|
f := migrationFunc{
|
|
|
|
name: name,
|
|
|
|
f: func(db *gorm.DB, cfg *config.Config) error {
|
|
|
|
if hasRun(name, db) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-22 17:44:37 +03:00
|
|
|
logbuch.Info("this may take a while!")
|
|
|
|
|
2021-12-15 12:50:16 +03:00
|
|
|
if cfg.Db.IsMySQL() {
|
|
|
|
tx := db.Begin()
|
2021-12-15 15:17:07 +03:00
|
|
|
if err := tx.Exec("ALTER TABLE heartbeats MODIFY COLUMN id BIGINT UNSIGNED AUTO_INCREMENT").Error; err != nil {
|
2021-12-15 12:50:16 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-12-15 15:17:07 +03:00
|
|
|
if err := tx.Exec("ALTER TABLE summary_items MODIFY COLUMN id BIGINT UNSIGNED AUTO_INCREMENT").Error; err != nil {
|
2021-12-15 12:50:16 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
} else if cfg.Db.IsPostgres() {
|
|
|
|
// postgres does not have unsigned data types
|
|
|
|
// https://www.postgresql.org/docs/10/datatype-numeric.html
|
|
|
|
tx := db.Begin()
|
|
|
|
if err := tx.Exec("ALTER TABLE heartbeats ALTER COLUMN id TYPE BIGINT").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Exec("ALTER TABLE summary_items ALTER COLUMN id TYPE BIGINT").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
} else {
|
|
|
|
// sqlite doesn't allow for changing column type easily
|
|
|
|
// https://stackoverflow.com/a/2083562/3112139
|
|
|
|
logbuch.Warn("unable to migrate id columns to bigint on %s", cfg.Db.Dialect)
|
|
|
|
}
|
|
|
|
|
|
|
|
setHasRun(name, db)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
registerPostMigration(f)
|
|
|
|
}
|