From b947e72a767349dbceec946aabf617745a742955 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Tue, 24 Oct 2023 00:54:15 +0200 Subject: [PATCH] added TableName's to migrate20221119131300 --- server/backend/migration/20221119131300.go | 102 +++++++++++++-------- 1 file changed, 66 insertions(+), 36 deletions(-) diff --git a/server/backend/migration/20221119131300.go b/server/backend/migration/20221119131300.go index 04a459fd..5387d508 100644 --- a/server/backend/migration/20221119131300.go +++ b/server/backend/migration/20221119131300.go @@ -17,7 +17,7 @@ import ( //go:embed static_data/iosInitialSchedulingPriorities.json var iosInitialPrioritiesFile []byte -type IOSDevice struct { +type InitialIOSDevice struct { DeviceID string `gorm:"primary_key" json:"deviceId"` CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"` PublicKey string `gorm:"not null" json:"publicKey"` @@ -27,7 +27,12 @@ type IOSDevice struct { ActivityThisYear int32 `gorm:"default:0" json:"activityThisYear"` } -type IOSSchedulingPriority struct { +// TableName sets the insert table name for this struct type +func (n *InitialIOSDevice) TableName() string { + return "ios_devices" +} + +type InitialIOSSchedulingPriority struct { ID int64 `gorm:"primary_key;auto_increment;not_null" json:"id"` FromDay int `gorm:"not null" json:"from_day"` ToDay int `gorm:"not null" json:"to_day"` @@ -36,37 +41,62 @@ type IOSSchedulingPriority struct { Priority int `gorm:"not null" json:"priority"` } -// IOSScheduledUpdateLog logs the last time a device was updated. -type IOSScheduledUpdateLog struct { - ID int64 `gorm:"primary_key;auto_increment;not_null" json:"id"` - DeviceID string `gorm:"index:idx_scheduled_update_log_device,unique" json:"deviceId"` - Device IOSDevice `gorm:"constraint:OnDelete:CASCADE;" json:"device"` - Type string `gorm:"type:enum ('grades');" json:"type"` - CreatedAt time.Time `gorm:"index:idx_scheduled_update_log_created,unique;autoCreateTime" json:"createdAt"` +// TableName sets the insert table name for this struct type +func (p *InitialIOSSchedulingPriority) TableName() string { + return "ios_scheduling_priorities" +} + +// InitialIOSScheduledUpdateLog logs the last time a device was updated. +type InitialIOSScheduledUpdateLog struct { + ID int64 `gorm:"primary_key;auto_increment;not_null" json:"id"` + DeviceID string `gorm:"index:idx_scheduled_update_log_device,unique" json:"deviceId"` + Device InitialIOSDevice `gorm:"constraint:OnDelete:CASCADE;" json:"device"` + Type string `gorm:"type:enum ('grades');" json:"type"` + CreatedAt time.Time `gorm:"index:idx_scheduled_update_log_created,unique;autoCreateTime" json:"createdAt"` +} + +// TableName sets the insert table name for this struct type +func (p *InitialIOSScheduledUpdateLog) TableName() string { + return "ios_scheduled_update_logs" } -type IOSDeviceRequestLog struct { - RequestID string `gorm:"primary_key;default:UUID()" json:"requestId"` - DeviceID string `gorm:"size:200;not null" json:"deviceId"` - Device IOSDevice `gorm:"constraint:OnDelete:CASCADE;" json:"device"` - RequestType string `gorm:"not null;type:enum ('CAMPUS_TOKEN_REQUEST');" json:"requestType"` - CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"` +type InitialIOSDeviceRequestLog struct { + RequestID string `gorm:"primary_key;default:UUID()" json:"requestId"` + DeviceID string `gorm:"size:200;not null" json:"deviceId"` + Device InitialIOSDevice `gorm:"constraint:OnDelete:CASCADE;" json:"device"` + RequestType string `gorm:"not null;type:enum ('CAMPUS_TOKEN_REQUEST');" json:"requestType"` + CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"` } -type IOSEncryptedGrade struct { - ID int64 `gorm:"primaryKey"` - Device IOSDevice `gorm:"constraint:OnDelete:CASCADE"` - DeviceID string `gorm:"index;not null"` - LectureTitle string `gorm:"not null"` - Grade string `gorm:"not null"` - IsEncrypted bool `gorm:"not null,default:true"` +// TableName sets the insert table name for this struct type +func (p *InitialIOSDeviceRequestLog) TableName() string { + return "ios_device_request_logs" } -type IOSDevicesActivityReset struct { +type InitialIOSEncryptedGrade struct { + ID int64 `gorm:"primaryKey"` + Device InitialIOSDevice `gorm:"constraint:OnDelete:CASCADE"` + DeviceID string `gorm:"index;not null"` + LectureTitle string `gorm:"not null"` + Grade string `gorm:"not null"` + IsEncrypted bool `gorm:"not null,default:true"` +} + +// TableName sets the insert table name for this struct type +func (p *InitialIOSEncryptedGrade) TableName() string { + return "ios_encrypted_grades" +} + +type InitialIOSDevicesActivityReset struct { Type string `gorm:"primary_key;type:enum('day', 'week', 'month', 'year')"` LastReset time.Time `gorm:"autoCreateTime"` } +// TableName sets the insert table name for this struct type +func (p *InitialIOSDevicesActivityReset) TableName() string { + return "ios_devices_activity_resets" +} + // migrate20221119131300 // - adds the ability to connect to ios devices func migrate20221119131300() *gormigrate.Migration { @@ -74,12 +104,12 @@ func migrate20221119131300() *gormigrate.Migration { ID: "20221119131300", Migrate: func(tx *gorm.DB) error { if err := tx.AutoMigrate( - &IOSDevice{}, - &IOSSchedulingPriority{}, - &IOSScheduledUpdateLog{}, - &IOSDeviceRequestLog{}, - &IOSEncryptedGrade{}, - &IOSDevicesActivityReset{}, + &InitialIOSDevice{}, + &InitialIOSSchedulingPriority{}, + &InitialIOSScheduledUpdateLog{}, + &InitialIOSDeviceRequestLog{}, + &InitialIOSEncryptedGrade{}, + &InitialIOSDevicesActivityReset{}, ); err != nil { return err } @@ -88,7 +118,7 @@ func migrate20221119131300() *gormigrate.Migration { return err } - var priorities []IOSSchedulingPriority + var priorities []InitialIOSSchedulingPriority if err := json.Unmarshal(iosInitialPrioritiesFile, &priorities); err != nil { log.WithError(err).Error("could not unmarshal json") @@ -117,22 +147,22 @@ func migrate20221119131300() *gormigrate.Migration { }, Rollback: func(tx *gorm.DB) error { - if err := tx.Migrator().DropTable(&IOSDevice{}); err != nil { + if err := tx.Migrator().DropTable(&InitialIOSDevice{}); err != nil { return err } - if err := tx.Migrator().DropTable(&IOSSchedulingPriority{}); err != nil { + if err := tx.Migrator().DropTable(&InitialIOSSchedulingPriority{}); err != nil { return err } - if err := tx.Migrator().DropTable(&IOSScheduledUpdateLog{}); err != nil { + if err := tx.Migrator().DropTable(&InitialIOSScheduledUpdateLog{}); err != nil { return err } - if err := tx.Migrator().DropTable(&IOSDeviceRequestLog{}); err != nil { + if err := tx.Migrator().DropTable(&InitialIOSDeviceRequestLog{}); err != nil { return err } - if err := tx.Migrator().DropTable(&IOSEncryptedGrade{}); err != nil { + if err := tx.Migrator().DropTable(&InitialIOSEncryptedGrade{}); err != nil { return err } - if err := tx.Migrator().DropTable(&IOSDevicesActivityReset{}); err != nil { + if err := tx.Migrator().DropTable(&InitialIOSDevicesActivityReset{}); err != nil { return err }