From 221f79ba2d05c98a4081d8268b081f3b8b5f6390 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sun, 17 Mar 2024 19:52:47 +0100 Subject: [PATCH] made sure that all timestamp columns are defaulted and update as expected --- server/backend/migration/20240319000000.go | 29 ++++++++++++++++++++++ server/backend/migration/migration.go | 1 + 2 files changed, 30 insertions(+) create mode 100644 server/backend/migration/20240319000000.go diff --git a/server/backend/migration/20240319000000.go b/server/backend/migration/20240319000000.go new file mode 100644 index 00000000..7b8d5b51 --- /dev/null +++ b/server/backend/migration/20240319000000.go @@ -0,0 +1,29 @@ +package migration + +import ( + "fmt" + "github.com/go-gormigrate/gormigrate/v2" + "gorm.io/gorm" +) + +// migrate20240319000000 +// made sure that all timestamp columns are defaulted and update as expected +func migrate20240319000000() *gormigrate.Migration { + return &gormigrate.Migration{ + ID: "20240319000000", + Migrate: func(tx *gorm.DB) error { + tables := []string{"cafeteria_rating", "canteen_head_count", "dish_rating"} + for _, t := range tables { + tx.Exec(fmt.Sprintf("alter table `%s` modify timestamp timestamp default current_timestamp() not null on update current_timestamp()", t)) + } + return nil + }, + Rollback: func(tx *gorm.DB) error { + tables := []string{"cafeteria_rating", "canteen_head_count", "dish_rating"} + for _, t := range tables { + tx.Exec(fmt.Sprintf("alter table `%s` modify timestamp timestamp not null", t)) + } + return nil + }, + } +} diff --git a/server/backend/migration/migration.go b/server/backend/migration/migration.go index 189e7ddc..d8662d35 100644 --- a/server/backend/migration/migration.go +++ b/server/backend/migration/migration.go @@ -75,6 +75,7 @@ func manualMigrate(db *gorm.DB) error { migrate20240312000000(), migrate20240316000000(), migrate20240317000000(), + migrate20240319000000(), } return gormigrate.New(db, gormigrateOptions, migrations).Migrate() }