Skip to content

Commit

Permalink
Made sure that all tables have the correct AUTO_INCREMENT flags set
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Mar 27, 2024
1 parent a007864 commit 7a56f60
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
77 changes: 77 additions & 0 deletions server/backend/migration/20240327000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package migration

import (
"slices"

"github.com/go-gormigrate/gormigrate/v2"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)

// migrate20240327000000
// made sure auto_increment is re-added to all fields where this was accidentally removed in migrate20240316000000
func migrate20240327000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20240327000000",
Migrate: func(tx *gorm.DB) error {
for _, t := range tablesWithWrongId() {
tablesWithCorrectIds := []string{"roomfinder_buildings2maps", "roomfinder_rooms2maps", "question"}
if slices.Contains(tablesWithCorrectIds, t.table) {
continue
}
log.WithField("table", t.table).Info("migrated PK-field")
if err := migrateField(tx, t.table, t.field, "BIGINT NOT NULL AUTO_INCREMENT"); err != nil {
return err
}
}
// crontab should be a PK instead of a unique key
if err := migrateField(tx, "crontab", "cron", "BIGINT NOT NULL"); err != nil {
return err
}
if err := tx.Exec("alter table crontab drop key cron").Error; err != nil {
return err
}
if err := tx.Exec("alter table crontab add constraint crontab_pk primary key (cron)").Error; err != nil {
return err
}
if err := migrateField(tx, "crontab", "cron", "BIGINT NOT NULL AUTO_INCREMENT"); err != nil {
return err
}
// roomfinder_schedules does not have a PK set
if err := tx.Exec("alter table roomfinder_schedules add constraint roomfinder_schedules_pk primary key (room_id)").Error; err != nil {
return err
}
return nil
},
Rollback: func(tx *gorm.DB) error {
for _, t := range tablesWithWrongId() {
tablesWithCorrectIds := []string{"roomfinder_buildings2maps", "roomfinder_rooms2maps", "question"}
if slices.Contains(tablesWithCorrectIds, t.table) {
continue
}
if err := migrateField(tx, t.table, t.field, "BIGINT NOT NULL"); err != nil {
return err
}
log.WithField("table", t.table).Info("migrated PK-field")
}
// crontab should be a PK instead of a unique key
if err := migrateField(tx, "crontab", "cron", "BIGINT NOT NULL"); err != nil {
return err
}
if err := tx.Exec("alter table crontab drop key crontab_pk").Error; err != nil {
return err
}
if err := tx.Exec("alter table crontab add constraint cron unique (cron)").Error; err != nil {
return err
}
if err := migrateField(tx, "crontab", "cron", "BIGINT NOT NULL AUTO_INCREMENT"); err != nil {
return err
}
// roomfinder_schedules does not have a PK set
if err := tx.Exec("alter table roomfinder_schedules drop key roomfinder_schedules_pk").Error; err != nil {
return err
}
return nil
},
}
}
1 change: 1 addition & 0 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func manualMigrate(db *gorm.DB) error {
migrate20240317000000(),
migrate20240318000000(),
migrate20240319000000(),
migrate20240327000000(),
}
return gormigrate.New(db, gormigrateOptions, migrations).Migrate()
}
Expand Down
6 changes: 0 additions & 6 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 h1:RFiFrvy37/mpSpdySBDrUdipW/dHwsRwh3J3+A9VgT4=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237/go.mod h1:Z5Iiy3jtmioajWHDGFk7CeugTyHtPvMHA4UTmUkyalE=
google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa h1:Jt1XW5PaLXF1/ePZrznsh/aAUvI7Adfc3LY1dAKlzRs=
google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa/go.mod h1:K4kfzHtI0kqWA79gecJarFtDn/Mls+GxQcg3Zox91Ac=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311132316-a219d84964c2 h1:9IZDv+/GcI6u+a4jRFRLxQs0RUCfavGfoOgEW6jpkI0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311132316-a219d84964c2/go.mod h1:UCOku4NytXMJuLQE5VuqA5lX3PcHCBo8pxNyvkf4xBs=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c h1:lfpJ/2rWPa/kJgxyyXM8PrNnfCzcmxJ265mADgwmvLI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
Expand Down

0 comments on commit 7a56f60

Please sign in to comment.