From 05d3d7d554458a14df8c41571ffdab54bdbf625a Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sat, 26 Aug 2023 20:08:32 +0200 Subject: [PATCH] removed the chat from the database --- server/backend/migration/20230825000000.go | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 server/backend/migration/20230825000000.go diff --git a/server/backend/migration/20230825000000.go b/server/backend/migration/20230825000000.go new file mode 100644 index 00000000..22885c5c --- /dev/null +++ b/server/backend/migration/20230825000000.go @@ -0,0 +1,85 @@ +package migration + +import ( + "github.com/TUM-Dev/Campus-Backend/server/model" + "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"` +} + +// migrate20230825000000 +// Removes all traces of the chat from the database. +func (m TumDBMigrator) migrate20230825000000() *gormigrate.Migration { + return &gormigrate.Migration{ + ID: "20230825000000", + Migrate: func(tx *gorm.DB) error { + // deactivete the crontab (Rollback deletes this from the enum) + // given that previously, not cronjobs for this type existed there is no need to remove offending entries first + if err := SafeEnumRollback(tx, &model.Crontab{}, "type", "chat"); err != nil { + return err + } + + // 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 + } + if err := tx.Exec("create index chat_chatroom_members_b3c09425 on chat_room2members (member);").Error; err != nil { + return err + } + + // activete the crontab (Migrate adds this from to the enum) + // given that previously, not cronjobs for this type existed there is no need to add entries first + return SafeEnumMigrate(tx, &model.Crontab{}, "type", "chat") + }, + } +}