Skip to content

Commit

Permalink
added a migration to remove all tickets from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 3, 2023
1 parent 9232857 commit 6381b86
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 11 deletions.
3 changes: 0 additions & 3 deletions server/backend/cron/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const (
ChatType = "chat"
KinoType = "kino"
RoomfinderType = "roomfinder"
TicketSaleType = "ticketsale"
AlarmType = "alarm" */
)

Expand Down Expand Up @@ -108,8 +107,6 @@ func (c *CronService) Run() error {
g.Go(func() error { return c.kinoCron() })
case RoomfinderType:
g.Go(func() error { return c.roomFinderCron() })
case TicketSaleType:
g.Go(func() error { return c.roomFinderCron() })
case AlarmType:
g.Go(func() error { return c.alarmCron() })
*/
Expand Down
7 changes: 0 additions & 7 deletions server/backend/cron/ticketsale.go

This file was deleted.

100 changes: 100 additions & 0 deletions server/backend/migration/20230904000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package migration

import (
"database/sql"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/guregu/null"
"gorm.io/gorm"
)

// migrate20230904000000
// Removes ticketsales from the db
func (m TumDBMigrator) migrate20230904000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20230904000000",
Migrate: func(tx *gorm.DB) error {
// remove "canteenHeadCount" in the enum
if err := tx.Where("type = ?", "ticketsales").Delete(&model.Crontab{}).Error; err != nil {
return err
}
if err := SafeEnumRollback(tx, model.Crontab{}, "type", "ticketsales"); err != nil {
return err
}
// order intentional to avoid foreign key constraint errors
for _, tbl := range []string{"ticket_history", "ticket_type", "ticket_payment", "ticket_admin2group", "ticket_admin", "event", "ticket_group"} {
if err := tx.Migrator().DropTable(tbl); err != nil {
return err
}
}
return nil
},

Rollback: func(tx *gorm.DB) error {
//ticket_group
if err := tx.Exec(`create table ticket_group (ticket_group int auto_increment primary key,description text not null);`).Error; err != nil {
return err
}
//event
if err := tx.Exec("create table event(event int auto_increment primary key, news int null, kino int null, file int null, title varchar(100) not null, description text not null, locality varchar(200) not null, link varchar(200) null, start datetime null, end datetime null, ticket_group int default 1 null, constraint fkEventFile foreign key (file) references files (file) on update cascade on delete set null, constraint fkEventGroup foreign key (ticket_group) references ticket_group (ticket_group), constraint fkKino foreign key (kino) references kino (kino) on update cascade on delete set null, constraint fkNews foreign key (news) references news (news) on update cascade on delete set null);").Error; err != nil {
return err
}
if err := tx.Exec("create index file on event (file);").Error; err != nil {
return err
}
if err := tx.Exec("create fulltext index searchTitle on event (title);").Error; err != nil {
return err
}
//ticket_admin
if err := tx.Exec("create table ticket_admin(ticket_admin int auto_increment primary key, `key` text not null, created timestamp default current_timestamp() not null, active tinyint(1) default 0 not null, comment text null);").Error; err != nil {
return err
}
//ticket_admin2group
if err := tx.Exec("create table ticket_admin2group( ticket_admin2group int auto_increment primary key, ticket_admin int not null, ticket_group int not null, constraint fkTicketAdmin foreign key (ticket_admin) references ticket_admin (ticket_admin) on update cascade on delete cascade, constraint fkTicketGroup foreign key (ticket_group) references ticket_group (ticket_group) on update cascade on delete cascade);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_admin on ticket_admin2group (ticket_admin);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_group on ticket_admin2group (ticket_group);").Error; err != nil {
return err
}
//ticket_payment
if err := tx.Exec("create table ticket_payment( ticket_payment int auto_increment primary key, name varchar(50) not null, min_amount int null, max_amount int null, config text not null);").Error; err != nil {
return err
}
//ticket_type
if err := tx.Exec("create table ticket_type( ticket_type int auto_increment primary key, event int not null, ticket_payment int not null, price double not null, contingent int not null, description varchar(100) not null, constraint fkEvent foreign key (event) references event (event) on update cascade on delete cascade, constraint fkPayment foreign key (ticket_payment) references ticket_payment (ticket_payment) on update cascade);").Error; err != nil {
return err
}
if err := tx.Exec("create index event on ticket_type (event);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_payment on ticket_type (ticket_payment);").Error; err != nil {
return err
}
//ticket_history
if err := tx.Exec("create table ticket_history( ticket_history int auto_increment primary key, member int not null, ticket_payment int null, ticket_type int not null, purchase datetime null, redemption datetime null, created timestamp default current_timestamp() not null, code char(128) not null, constraint fkMember foreign key (member) references member (member) on update cascade on delete cascade, constraint fkTicketPayment foreign key (ticket_payment) references ticket_payment (ticket_payment) on update cascade, constraint fkTicketType foreign key (ticket_type) references ticket_type (ticket_type) on update cascade);").Error; err != nil {
return err
}
if err := tx.Exec("create index member on ticket_history (member);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_payment on ticket_history (ticket_payment);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_type on ticket_history (ticket_type);").Error; err != nil {
return err
}

if err := SafeEnumMigrate(tx, model.Crontab{}, "type", "ticketsales"); err != nil {
return err
}
return tx.Create(&model.Crontab{
Interval: 60 * 10, // Every 10 minutes
Type: null.String{NullString: sql.NullString{String: "ticketsales", Valid: true}},
}).Error
},
}
}
1 change: 1 addition & 0 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (m TumDBMigrator) Migrate() error {
m.migrate20220713000000(),
m.migrate20221119131300(),
m.migrate20221210000000(),
m.migrate20230904000000(),
})
err := mig.Migrate()
return err
Expand Down
2 changes: 1 addition & 1 deletion server/model/crontab.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Crontab struct {
//[ 2] lastRun int null: false primary: false isArray: false auto: false col: int len: -1 default: [0]
LastRun int32 `gorm:"column:lastRun;type:int;default:0;" json:"last_run"`
//[ 3] type char(10) null: true primary: false isArray: false auto: false col: char len: 10 default: []
Type null.String `gorm:"column:type;type:enum ('news', 'mensa', 'chat', 'kino', 'roomfinder', 'ticketsale', 'alarm', 'fileDownload','dishNameDownload','averageRatingComputation', 'iosNotifications', 'iosActivityReset', 'canteenHeadCount');" json:"type"`
Type null.String `gorm:"column:type;type:enum ('news', 'mensa', 'chat', 'kino', 'roomfinder', 'alarm', 'fileDownload','dishNameDownload','averageRatingComputation', 'iosNotifications', 'iosActivityReset', 'canteenHeadCount');" json:"type"`
//[ 4] id int null: true primary: false isArray: false auto: false col: int len: -1 default: []
ID null.Int `gorm:"column:id;type:int;" json:"id"`
}

0 comments on commit 6381b86

Please sign in to comment.