diff --git a/infra/db/converter.go b/infra/db/converter.go index 59b142e..99a2c5b 100644 --- a/infra/db/converter.go +++ b/infra/db/converter.go @@ -15,16 +15,17 @@ func ConvCreateRoomParamsToRoom(src CreateRoomParams) (dst Room) { dst.Place = src.WriteRoomParams.Place dst.TimeStart = src.WriteRoomParams.TimeStart dst.TimeEnd = src.WriteRoomParams.TimeEnd - dst.Admins = make([]RoomAdmin, len(src.WriteRoomParams.Admins)) + dst.Admins = make([]User, len(src.WriteRoomParams.Admins)) for i := range src.WriteRoomParams.Admins { - dst.Admins[i] = convuuidUUIDToRoomAdmin(src.WriteRoomParams.Admins[i]) + dst.Admins[i] = ConvuuidUUIDToUserMeta(src.WriteRoomParams.Admins[i]) } return } -func ConvEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { - dst.UserID = src.UserID - return -} + +// func ConvEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { +// dst.UserID = src.UserID +// return +// } func ConvEventAdminTodomainUser(src EventAdmin) (dst domain.User) { dst.ID = src.UserID @@ -100,10 +101,10 @@ func ConvGroupTodomainGroup(src Group) (dst domain.Group) { (*dst.Model.DeletedAt) = convgormDeletedAtTotimeTime(src.Model.DeletedAt) return } -func ConvRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { - dst.ID = src.UserID - return -} +// func ConvRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { +// dst.ID = src.UserID +// return +// } func ConvRoomTodomainRoom(src Room) (dst domain.Room) { dst.ID = src.ID @@ -117,7 +118,7 @@ func ConvRoomTodomainRoom(src Room) (dst domain.Room) { } dst.Admins = make([]domain.User, len(src.Admins)) for i := range src.Admins { - dst.Admins[i] = convRoomAdminTodomainUser(src.Admins[i]) + dst.Admins[i] = convUserTodomainUser(src.Admins[i]) } dst.CreatedBy = convUserTodomainUser(src.CreatedBy) dst.Model.CreatedAt = src.Model.CreatedAt @@ -127,10 +128,18 @@ func ConvRoomTodomainRoom(src Room) (dst domain.Room) { return } -func ConvSEventAdminToSRoomAdmin(src []EventAdmin) (dst []RoomAdmin) { - dst = make([]RoomAdmin, len(src)) +// func ConvSEventAdminToSRoomAdmin(src []EventAdmin) (dst []RoomAdmin) { +// dst = make([]RoomAdmin, len(src)) +// for i := range src { +// dst[i] = convEventAdminToRoomAdmin(src[i]) +// } +// return +// } + +func ConvSEventAdminToSUser(src []EventAdmin) (dst []User) { + dst = make([]User, len(src)) for i := range src { - dst[i] = convEventAdminToRoomAdmin(src[i]) + dst[i] = convEventAdminToUser(src[i]) } return } @@ -207,9 +216,9 @@ func ConvUpdateRoomParamsToRoom(src UpdateRoomParams) (dst Room) { dst.Place = src.WriteRoomParams.Place dst.TimeStart = src.WriteRoomParams.TimeStart dst.TimeEnd = src.WriteRoomParams.TimeEnd - dst.Admins = make([]RoomAdmin, len(src.WriteRoomParams.Admins)) + dst.Admins = make([]User, len(src.WriteRoomParams.Admins)) for i := range src.WriteRoomParams.Admins { - dst.Admins[i] = convuuidUUIDToRoomAdmin(src.WriteRoomParams.Admins[i]) + dst.Admins[i] = ConvuuidUUIDToUserMeta(src.WriteRoomParams.Admins[i]) } return } @@ -288,21 +297,26 @@ func ConvuuidUUIDToGroupMember(src uuid.UUID) (dst GroupMember) { dst.UserID = src return } -func ConvuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { - dst.UserID = src - return -} +// func ConvuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { +// dst.UserID = src +// return +// } func ConvuuidUUIDToUserMeta(src uuid.UUID) (dst User) { dst.ID = src return } -func convEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { - dst.UserID = src.UserID +// func convEventAdminToRoomAdmin(src EventAdmin) (dst RoomAdmin) { +// dst.UserID = src.UserID +// return +// } + +func convEventAdminTodomainUser(src EventAdmin) (dst domain.User) { + dst.ID = src.UserID return } -func convEventAdminTodomainUser(src EventAdmin) (dst domain.User) { +func convEventAdminToUser(src EventAdmin) (dst User) { dst.ID = src.UserID return } @@ -375,10 +389,10 @@ func convGroupTodomainGroup(src Group) (dst domain.Group) { (*dst.Model.DeletedAt) = convgormDeletedAtTotimeTime(src.Model.DeletedAt) return } -func convRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { - dst.ID = src.UserID - return -} +// func convRoomAdminTodomainUser(src RoomAdmin) (dst domain.User) { +// dst.ID = src.UserID +// return +// } func convRoomTodomainRoom(src Room) (dst domain.Room) { dst.ID = src.ID dst.Place = src.Place @@ -391,7 +405,7 @@ func convRoomTodomainRoom(src Room) (dst domain.Room) { } dst.Admins = make([]domain.User, len(src.Admins)) for i := range src.Admins { - dst.Admins[i] = convRoomAdminTodomainUser(src.Admins[i]) + dst.Admins[i] = convUserTodomainUser(src.Admins[i]) } dst.CreatedBy = convUserTodomainUser(src.CreatedBy) dst.Model.CreatedAt = src.Model.CreatedAt @@ -439,7 +453,7 @@ func convuuidUUIDToGroupMember(src uuid.UUID) (dst GroupMember) { return } -func convuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { - dst.UserID = src - return -} +// func convuuidUUIDToRoomAdmin(src uuid.UUID) (dst RoomAdmin) { +// dst.UserID = src +// return +// } diff --git a/infra/db/hooks.go b/infra/db/hooks.go index 078c57f..3c7f6ff 100644 --- a/infra/db/hooks.go +++ b/infra/db/hooks.go @@ -22,7 +22,7 @@ func (e *Event) BeforeSave(tx *gorm.DB) (err error) { e.Room.TimeStart = e.TimeStart e.Room.TimeEnd = e.TimeEnd e.Room.CreatedByRefer = e.CreatedByRefer - e.Room.Admins = ConvSEventAdminToSRoomAdmin(e.Admins) + e.Room.Admins = ConvSEventAdminToSUser(e.Admins) } else { return NewValueError(ErrRoomUndefined, "roomID", "place") } @@ -180,8 +180,7 @@ func (r *Room) BeforeSave(tx *gorm.DB) (err error) { } func (r *Room) BeforeUpdate(tx *gorm.DB) (err error) { - err = tx.Where("room_id", r.ID).Delete(&RoomAdmin{}).Error - if err != nil { + if err := tx.Model(r).Association("Admins").Clear(); err != nil { return err } return nil diff --git a/infra/db/model.go b/infra/db/model.go index 90cb213..5bd8e01 100644 --- a/infra/db/model.go +++ b/infra/db/model.go @@ -15,7 +15,7 @@ var tables = []interface{}{ GroupAdmin{}, Tag{}, Room{}, - RoomAdmin{}, + // RoomAdmin{}, Event{}, EventTag{}, // Eventより下にないと、overrideされる EventAdmin{}, @@ -79,12 +79,12 @@ type UserBody struct { User User `gorm:"->; foreignKey:ID; constraint:OnDelete:CASCADE;" cvt:"->"` } -type RoomAdmin struct { - UserID uuid.UUID `gorm:"type:char(36); primaryKey"` - RoomID uuid.UUID `gorm:"type:char(36); primaryKey"` - User User `gorm:"->; foreignKey:UserID; constraint:OnDelete:CASCADE;" cvt:"->"` - Model `cvt:"-"` -} +// type RoomAdmin struct { +// UserID uuid.UUID `gorm:"type:char(36); primaryKey"` +// RoomID uuid.UUID `gorm:"type:char(36); primaryKey"` +// User User `gorm:"->; foreignKey:UserID; constraint:OnDelete:CASCADE;" cvt:"->"` +// Model `cvt:"-"` +// } //go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s Room -d domain.Room -o converter.go . //go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s []*Room -d []*domain.Room -o converter.go . @@ -95,7 +95,7 @@ type Room struct { TimeStart time.Time `gorm:"type:DATETIME; index"` TimeEnd time.Time `gorm:"type:DATETIME; index"` Events []Event `gorm:"->; constraint:-"` // readOnly - Admins []RoomAdmin + Admins []User `gorm:"many2many:room_admin_users;"` CreatedByRefer uuid.UUID `gorm:"type:char(36);" cvt:"CreatedBy, <-"` CreatedBy User `gorm:"->; foreignKey:CreatedByRefer; constraint:OnDelete:CASCADE;" cvt:"->"` Model `cvt:"->"` diff --git a/migration/current.go b/migration/current.go index f066004..e9488bc 100644 --- a/migration/current.go +++ b/migration/current.go @@ -20,5 +20,6 @@ func Migrations() []*gormigrate.Migration { v11(), v12(), v13(), + v14(), } } diff --git a/migration/v14.go b/migration/v14.go new file mode 100644 index 0000000..7d5b54c --- /dev/null +++ b/migration/v14.go @@ -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{}) + }, + } +}