-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,6 @@ func Migrations() []*gormigrate.Migration { | |
v11(), | ||
v12(), | ||
v13(), | ||
v14(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package migration | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/go-gormigrate/gormigrate/v2" | ||
"github.com/gofrs/uuid" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type v14Room struct { | ||
ID uuid.UUID `gorm:"type:char(36);primaryKey"` | ||
Place string `gorm:"type:varchar(32);"` | ||
Verified bool | ||
TimeStart time.Time `gorm:"type:DATETIME; index"` | ||
TimeEnd time.Time `gorm:"type:DATETIME; index"` | ||
CreatedByRefer uuid.UUID `gorm:"type:char(36);"` | ||
} | ||
|
||
func (*v14Room) TableName() string { | ||
return "rooms" | ||
} | ||
|
||
type v14RoomUser struct { | ||
RoomID uuid.UUID `gorm:"type:char(36); primaryKey"` | ||
UserID uuid.UUID `gorm:"type:char(36); primaryKey"` | ||
} | ||
|
||
func (*v14RoomUser) TableName() string { | ||
return "room_admin_users" | ||
} | ||
|
||
type v14RoomAdmin struct { | ||
UserID uuid.UUID `gorm:"type:char(36); primaryKey"` | ||
RoomID uuid.UUID `gorm:"type:char(36); primaryKey"` | ||
} | ||
|
||
func (*v14RoomAdmin) TableName() string { | ||
return "room_admins" | ||
} | ||
|
||
func v14() *gormigrate.Migration { | ||
return &gormigrate.Migration{ | ||
ID: "14", | ||
Migrate: func(db *gorm.DB) error { | ||
// Step 1: Create the new many-to-many table | ||
err := db.Migrator().CreateTable(&v14RoomUser{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Step 2: Migrate data from RoomAdmin to RoomUser | ||
roomAdmins := []v14RoomAdmin{} | ||
err = db.Find(&roomAdmins).Error | ||
if err != nil { | ||
return err | ||
} | ||
|
||
roomUsers := make([]v14RoomUser, len(roomAdmins)) | ||
for i, admin := range roomAdmins { | ||
roomUsers[i] = v14RoomUser{ | ||
RoomID: admin.RoomID, | ||
UserID: admin.UserID, | ||
} | ||
} | ||
|
||
if len(roomUsers) > 0 { | ||
err = db.Create(&roomUsers).Error | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Step 3: Drop the RoomAdmin table | ||
return db.Migrator().DropTable(&v14RoomAdmin{}) | ||
}, | ||
} | ||
} |
e68dbea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RoomAdmin
のModel
が使われてなかったので それなら gorm の Many To Many のアソシエーションを利用した方がAdmins.User
のPreload
をする必要もなく適切