From a136d733a491863c4b87e906d832be887533ada2 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sun, 22 Oct 2023 16:31:27 +0200 Subject: [PATCH] rebase --- server/backend/migration/2023090510000000.go | 6 +- server/backend/migration/20231023000000.go | 72 ++++++++++++++++++++ server/backend/migration/migration.go | 3 +- 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 server/backend/migration/20231023000000.go diff --git a/server/backend/migration/2023090510000000.go b/server/backend/migration/2023090510000000.go index 92453e62..523c3d5b 100644 --- a/server/backend/migration/2023090510000000.go +++ b/server/backend/migration/2023090510000000.go @@ -29,14 +29,14 @@ func (f *Files) TableName() string { return "files" } -// migrate2023090510000000 +// migrate20230905100000 // removes the unused trailer column // makes the Cover FK into a not null field // renames kino -> movie // fixes the id being named kino -func (m TumDBMigrator) migrate2023090510000000() *gormigrate.Migration { +func (m TumDBMigrator) migrate20230905100000() *gormigrate.Migration { return &gormigrate.Migration{ - ID: "migrate2023090510000000", + ID: "migrate20230905100000", Migrate: func(tx *gorm.DB) error { // fix the movie table if err := tx.Migrator().RenameTable(&Kino{}, &Movie{}); err != nil { diff --git a/server/backend/migration/20231023000000.go b/server/backend/migration/20231023000000.go new file mode 100644 index 00000000..e7d1a6fa --- /dev/null +++ b/server/backend/migration/20231023000000.go @@ -0,0 +1,72 @@ +package migration + +import ( + "github.com/go-gormigrate/gormigrate/v2" + "gorm.io/gorm" +) + +type device2stats struct { + ChatRoomsActivity int `gorm:"column:ChatRoomsActivity;default 0;not null"` + ChatActivity int `gorm:"column:ChatActivity;default 0;not null"` + WizNavChatActivity int `gorm:"column:WizNavChatActivity;default 0;not null"` +} + +// migrate20231023000000 +// Removes all traces of the chat from the database. +func (m TumDBMigrator) migrate20231023000000() *gormigrate.Migration { + return &gormigrate.Migration{ + ID: "20231023000000", + Migrate: func(tx *gorm.DB) error { + // Remove tracking from device2stats + if err := tx.Migrator().DropColumn(&device2stats{}, "ChatRoomsActivity"); err != nil { + return err + } + if err := tx.Migrator().DropColumn(&device2stats{}, "ChatActivity"); err != nil { + return err + } + if err := tx.Migrator().DropColumn(&device2stats{}, "WizNavChatActivity"); err != nil { + return err + } + + // Delete all tables + if err := tx.Migrator().DropTable("chat_message"); err != nil { + return err + } + if err := tx.Migrator().DropTable("chat_room2members"); err != nil { + return err + } + return tx.Migrator().DropTable("chat_room") + }, + Rollback: func(tx *gorm.DB) error { + // Restore chat_room + if err := tx.Exec("create table chat_room(room int auto_increment primary key, name varchar(100) not null, semester varchar(3) null, constraint `Index 2` unique (semester, name));").Error; err != nil { + return err + } + + // Add tracking from device2stats + if err := tx.Migrator().AutoMigrate(&device2stats{}); err != nil { + return err + } + + // Restore chat_message + if err := tx.Exec("create table chat_message (message int auto_increment primary key, member int not null, room int not null, text longtext not null, created datetime not null, signature longtext not null, constraint FK_chat_message_chat_room foreign key (room) references chat_room (room) on update cascade on delete cascade, constraint chat_message_ibfk_1 foreign key (member) references member (member) on update cascade on delete cascade);").Error; err != nil { + return err + } + if err := tx.Exec("create index chat_message_b3c09425 on chat_message (member);").Error; err != nil { + return err + } + if err := tx.Exec("create index chat_message_ca20ebca on chat_message (room);").Error; err != nil { + return err + } + + // Restore chat_room2members + if err := tx.Exec("create table chat_room2members(room2members int auto_increment primary key, room int not null, member int not null, constraint chatroom_id unique (room, member), constraint FK_chat_room2members_chat_room foreign key (room) references chat_room (room) on update cascade on delete cascade, constraint chat_room2members_ibfk_2 foreign key (member) references member (member) on update cascade on delete cascade );").Error; err != nil { + return err + } + if err := tx.Exec("create index chat_chatroom_members_29801a33 on chat_room2members (room);").Error; err != nil { + return err + } + return tx.Exec("create index chat_chatroom_members_b3c09425 on chat_room2members (member);").Error + }, + } +} diff --git a/server/backend/migration/migration.go b/server/backend/migration/migration.go index a0363627..ba48c9a1 100644 --- a/server/backend/migration/migration.go +++ b/server/backend/migration/migration.go @@ -49,7 +49,7 @@ func (m TumDBMigrator) Migrate() error { m.migrate20221119131300(), m.migrate20221210000000(), m.migrate20230826000000(), - m.migrate2023090510000000(), + m.migrate20230905100000(), m.migrate20230912000000(), m.migrate20230904000000(), m.migrate20230825000000(), @@ -58,6 +58,7 @@ func (m TumDBMigrator) Migrate() error { m.migrate20230904100000(), m.migrate20230826000000(), m.migrate20231003000000(), + m.migrate20231023000000(), }) err := mig.Migrate() return err