Skip to content

Commit

Permalink
🚧 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
iChemy committed Dec 18, 2024
1 parent dcc891e commit e68dbea
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 43 deletions.
78 changes: 46 additions & 32 deletions infra/db/converter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions infra/db/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions infra/db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var tables = []interface{}{
GroupAdmin{},
Tag{},
Room{},
RoomAdmin{},
// RoomAdmin{},
Event{},
EventTag{}, // Eventより下にないと、overrideされる
EventAdmin{},
Expand Down Expand Up @@ -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 .
Expand All @@ -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:"->"`
Expand Down
1 change: 1 addition & 0 deletions migration/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ func Migrations() []*gormigrate.Migration {
v11(),
v12(),
v13(),
v14(),
}
}
78 changes: 78 additions & 0 deletions migration/v14.go
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{})
},
}
}

1 comment on commit e68dbea

@iChemy
Copy link
Contributor Author

@iChemy iChemy commented on e68dbea Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RoomAdminModel が使われてなかったので それなら gorm の Many To Many のアソシエーションを利用した方が Admins.UserPreload をする必要もなく適切

Please sign in to comment.