2021-02-07 00:32:03 +03:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/emvi/logbuch"
|
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
f := migrationFunc{
|
|
|
|
name: "20210206_drop_badges_column_add_sharing_flags",
|
|
|
|
f: func(db *gorm.DB, cfg *config.Config) error {
|
|
|
|
migrator := db.Migrator()
|
|
|
|
|
|
|
|
if !migrator.HasColumn(&models.User{}, "badges_enabled") {
|
2021-02-14 18:01:31 +03:00
|
|
|
// empty database or already migrated, nothing to migrate
|
2021-02-07 00:32:03 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Exec("UPDATE users SET share_data_max_days = 30 WHERE badges_enabled = TRUE").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.Exec("UPDATE users SET share_editors = TRUE WHERE badges_enabled = TRUE").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.Exec("UPDATE users SET share_languages = TRUE WHERE badges_enabled = TRUE").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.Exec("UPDATE users SET share_projects = TRUE WHERE badges_enabled = TRUE").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.Exec("UPDATE users SET share_oss = TRUE WHERE badges_enabled = TRUE").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.Exec("UPDATE users SET share_machines = TRUE WHERE badges_enabled = TRUE").Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:23:18 +03:00
|
|
|
if cfg.Db.Dialect == config.SQLDialectSqlite {
|
2021-02-16 19:11:39 +03:00
|
|
|
logbuch.Info("not attempting to drop column 'badges_enabled' on sqlite")
|
2021-02-16 11:23:18 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-07 00:32:03 +03:00
|
|
|
if err := migrator.DropColumn(&models.User{}, "badges_enabled"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-16 11:23:18 +03:00
|
|
|
logbuch.Info("dropped column 'badges_enabled' after substituting it by sharing indicators")
|
2021-02-07 00:32:03 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
registerPostMigration(f)
|
|
|
|
}
|