|
| 1 | +// Package migration migrate current struct |
| 2 | +package migration |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/go-gormigrate/gormigrate/v2" |
| 9 | + "github.com/gofrs/uuid" |
| 10 | + "github.com/traPtitech/traPortfolio/internal/infrastructure/repository/model" |
| 11 | + "gorm.io/gorm" |
| 12 | +) |
| 13 | + |
| 14 | +// v1 unique_index:idx_room_uniqueの削除 |
| 15 | +func v2() *gormigrate.Migration { |
| 16 | + return &gormigrate.Migration{ |
| 17 | + ID: "2", |
| 18 | + Migrate: func(db *gorm.DB) error { |
| 19 | + if err := db.AutoMigrate(&v2Project{}, &v2Contest{}, &v2ContestTeam{}); err != nil { |
| 20 | + return err |
| 21 | + } |
| 22 | + |
| 23 | + // プロジェクト名の重複禁止 |
| 24 | + { |
| 25 | + projects := make([]*model.Project, 0) |
| 26 | + if err := db.Find(&projects).Error; err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + |
| 30 | + projectMap := make(map[string][]uuid.UUID, len(projects)) |
| 31 | + for _, p := range projects { |
| 32 | + projectMap[p.Name] = append(projectMap[p.Name], p.ID) |
| 33 | + } |
| 34 | + |
| 35 | + updates := make(map[uuid.UUID]string, len(projects)) |
| 36 | + for { |
| 37 | + noDuplicate := true |
| 38 | + for name, ids := range projectMap { |
| 39 | + if len(ids) <= 1 { |
| 40 | + continue |
| 41 | + } |
| 42 | + noDuplicate = false |
| 43 | + for i, pid := range ids { |
| 44 | + if i == 0 { |
| 45 | + projectMap[name] = []uuid.UUID{pid} |
| 46 | + continue |
| 47 | + } |
| 48 | + nameNew := fmt.Sprintf("%s (%d)", name, i) |
| 49 | + updates[pid] = nameNew |
| 50 | + projectMap[nameNew] = append(projectMap[nameNew], pid) |
| 51 | + } |
| 52 | + } |
| 53 | + if noDuplicate { |
| 54 | + break |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + for id, nameNew := range updates { |
| 59 | + err := db. |
| 60 | + Model(&model.Project{}). |
| 61 | + Where(&model.Project{ID: id}). |
| 62 | + Update("name", nameNew). |
| 63 | + Error |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // コンテスト名の重複禁止 |
| 71 | + { |
| 72 | + contests := make([]*model.Contest, 0) |
| 73 | + if err := db.Find(&contests).Error; err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + contestMap := make(map[string][]uuid.UUID, len(contests)) |
| 78 | + for _, c := range contests { |
| 79 | + contestMap[c.Name] = append(contestMap[c.Name], c.ID) |
| 80 | + } |
| 81 | + |
| 82 | + updates := make(map[uuid.UUID]string, len(contests)) |
| 83 | + noDuplicate := false |
| 84 | + for !noDuplicate { |
| 85 | + noDuplicate = true |
| 86 | + for name, ids := range contestMap { |
| 87 | + if len(ids) <= 1 { |
| 88 | + continue |
| 89 | + } |
| 90 | + noDuplicate = false |
| 91 | + for i, cid := range ids { |
| 92 | + if i == 0 { |
| 93 | + contestMap[name] = []uuid.UUID{cid} |
| 94 | + continue |
| 95 | + } |
| 96 | + nameNew := fmt.Sprintf("%s (%d)", name, i) |
| 97 | + updates[cid] = nameNew |
| 98 | + contestMap[nameNew] = append(contestMap[nameNew], cid) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + for id, nameNew := range updates { |
| 104 | + err := db. |
| 105 | + Model(&model.Contest{}). |
| 106 | + Where(&model.Contest{ID: id}). |
| 107 | + Update("name", nameNew). |
| 108 | + Error |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return db. |
| 116 | + Table("portfolio"). |
| 117 | + Error |
| 118 | + }, |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +type v2Project struct { |
| 123 | + ID uuid.UUID `gorm:"type:char(36);not null;primaryKey"` |
| 124 | + Name string `gorm:"type:varchar(128)"` // 制限増加 (32->128) |
| 125 | + Description string `gorm:"type:text"` |
| 126 | + Link string `gorm:"type:text"` |
| 127 | + SinceYear int `gorm:"type:smallint(4);not null"` |
| 128 | + SinceSemester int `gorm:"type:tinyint(1);not null"` |
| 129 | + UntilYear int `gorm:"type:smallint(4);not null"` |
| 130 | + UntilSemester int `gorm:"type:tinyint(1);not null"` |
| 131 | + CreatedAt time.Time `gorm:"precision:6"` |
| 132 | + UpdatedAt time.Time `gorm:"precision:6"` |
| 133 | +} |
| 134 | + |
| 135 | +func (*v2Project) TableName() string { |
| 136 | + return "projects" |
| 137 | +} |
| 138 | + |
| 139 | +type v2Contest struct { |
| 140 | + ID uuid.UUID `gorm:"type:char(36);not null;primaryKey"` |
| 141 | + Name string `gorm:"type:varchar(128)"` // 制限増加 (32->128) |
| 142 | + Description string `gorm:"type:text"` |
| 143 | + Link string `gorm:"type:text"` |
| 144 | + Since time.Time `gorm:"precision:6"` |
| 145 | + Until time.Time `gorm:"precision:6"` |
| 146 | + CreatedAt time.Time `gorm:"precision:6"` |
| 147 | + UpdatedAt time.Time `gorm:"precision:6"` |
| 148 | +} |
| 149 | + |
| 150 | +func (*v2Contest) TableName() string { |
| 151 | + return "contests" |
| 152 | +} |
| 153 | + |
| 154 | +type v2ContestTeam struct { |
| 155 | + ID uuid.UUID `gorm:"type:char(36);not null;primaryKey"` |
| 156 | + ContestID uuid.UUID `gorm:"type:char(36);not null"` |
| 157 | + Name string `gorm:"type:varchar(128)"` // 制限増加 (32->128) |
| 158 | + Description string `gorm:"type:text"` |
| 159 | + Result string `gorm:"type:text"` |
| 160 | + Link string `gorm:"type:text"` |
| 161 | + CreatedAt time.Time `gorm:"precision:6"` |
| 162 | + UpdatedAt time.Time `gorm:"precision:6"` |
| 163 | + |
| 164 | + Contest model.Contest `gorm:"foreignKey:ContestID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` |
| 165 | +} |
| 166 | + |
| 167 | +func (*v2ContestTeam) TableName() string { |
| 168 | + return "contest_teams" |
| 169 | +} |
0 commit comments