diff --git a/db.go b/db.go index 4bef4307..11de9f34 100644 --- a/db.go +++ b/db.go @@ -84,7 +84,7 @@ func OpenTestConnection() (db *gorm.DB, err error) { func RunMigrations() { var err error - allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}} + allModels := []interface{}{&Layout{}} rand.Seed(time.Now().UnixNano()) rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] }) diff --git a/main_test.go b/main_test.go index 12ffdfe3..e61d896b 100644 --- a/main_test.go +++ b/main_test.go @@ -3,6 +3,8 @@ package main import ( "testing" + "github.com/gofrs/uuid/v5" + "gorm.io/gorm" "gorm.io/playground/models" ) @@ -11,14 +13,31 @@ import ( // TEST_DRIVERS: sqlite, mysql, postgres, sqlserver func TestGORM(t *testing.T) { - user := models.User{Name: "jinzhu"} - - DB.Create(&user) - - var result models.User - if err := DB.First(&result, user.ID).Error; err != nil { - t.Errorf("Failed, got error: %v", err) - } + userId := uuid.Must(uuid.NewV4()) + projectId := uuid.Must(uuid.NewV4()) + layouts := []models.Layout{} + + sql1 := DB.ToSQL(func(tx *gorm.DB) *gorm.DB { + byUser := func(dbScope *gorm.DB) *gorm.DB { + if userId.Valid { + return dbScope.Where(dbScope.Where("private_for = ?", userId).Or("private_for IS NULL")) + } + return dbScope + } + return tx.Table("layouts").Scopes(byUser).Find(&layouts, map[string]any{"project_id": projectId}) + }) + t.Logf(sql1) + + sql2 := DB.ToSQL(func(tx *gorm.DB) *gorm.DB { + byUser := func(dbScope *gorm.DB) *gorm.DB { + if userId.Valid { + return dbScope.Where("(private_for = ? OR private_for IS NULL)", userId) + } + return dbScope + } + return tx.Table("layouts").Scopes(byUser).Find(&layouts, map[string]any{"project_id": projectId}) + }) + t.Logf(sql2) } // func TestGORMGen(t *testing.T) { diff --git a/models/models.go b/models/models.go index 611afa58..f9b56121 100644 --- a/models/models.go +++ b/models/models.go @@ -1,60 +1,16 @@ package models import ( - "database/sql" "time" - "gorm.io/gorm" + "github.com/gofrs/uuid/v5" ) -// User has one `Account` (has one), many `Pets` (has many) and `Toys` (has many - polymorphic) -// He works in a Company (belongs to), he has a Manager (belongs to - single-table), and also managed a Team (has many - single-table) -// He speaks many languages (many to many) and has many friends (many to many - single-table) -// His pet also has one Toy (has one - polymorphic) -type User struct { - gorm.Model - Name string - Age uint - Birthday *time.Time - Account Account - Pets []*Pet - Toys []Toy `gorm:"polymorphic:Owner"` - CompanyID *int - Company Company - ManagerID *uint - Manager *User - Team []User `gorm:"foreignkey:ManagerID"` - Languages []Language `gorm:"many2many:UserSpeak"` - Friends []*User `gorm:"many2many:user_friends"` - Active bool -} - -type Account struct { - gorm.Model - UserID sql.NullInt64 - Number string -} - -type Pet struct { - gorm.Model - UserID *uint - Name string - Toy Toy `gorm:"polymorphic:Owner;"` -} - -type Toy struct { - gorm.Model - Name string - OwnerID string - OwnerType string -} - -type Company struct { - ID int - Name string -} - -type Language struct { - Code string `gorm:"primarykey"` - Name string +type Layout struct { + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id" gorm:"<-:create"` + PrivateFor uuid.NullUUID `json:"private_for"` + Name string `json:"name"` + CreatedAt time.Time `json:"created_at" gorm:"<-:create"` + UpdatedAt time.Time `json:"updated_at"` }