refactor: simplify dataset source ownership
This commit is contained in:
@@ -3,22 +3,20 @@ package models
|
||||
import "time"
|
||||
|
||||
type SaDatasetSource struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Identity string `gorm:"type:char(36);uniqueIndex"`
|
||||
OwnerID uint `gorm:"index;not null;default:0;uniqueIndex:idx_sa_dataset_source_owner_seed,priority:1"`
|
||||
OwnerIdentity string `gorm:"type:char(36);index"`
|
||||
CreatedBy uint `gorm:"index;not null"`
|
||||
CreatedByIdentity string `gorm:"type:char(36);index"`
|
||||
SeedKey *string `gorm:"size:64;uniqueIndex:idx_sa_dataset_source_owner_seed,priority:2"`
|
||||
Name string `gorm:"size:160;not null"`
|
||||
Kind string `gorm:"size:32;not null"`
|
||||
URL string `gorm:"size:2048"`
|
||||
IconURL string `gorm:"size:2048"`
|
||||
Description string `gorm:"type:text"`
|
||||
Enabled bool `gorm:"not null;default:true;index"`
|
||||
LastSyncedAt *time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Identity string `gorm:"type:char(36);uniqueIndex"`
|
||||
OwnerID uint `gorm:"index;not null;default:0;uniqueIndex:idx_sa_dataset_source_owner_seed,priority:1"`
|
||||
OwnerIdentity string `gorm:"type:char(36);index"`
|
||||
SeedKey *string `gorm:"size:64;uniqueIndex:idx_sa_dataset_source_owner_seed,priority:2"`
|
||||
Name string `gorm:"size:160;not null"`
|
||||
Kind string `gorm:"size:32;not null"`
|
||||
URL string `gorm:"size:2048"`
|
||||
IconURL string `gorm:"size:2048"`
|
||||
Description string `gorm:"type:text"`
|
||||
Enabled bool `gorm:"not null;default:true;index"`
|
||||
LastSyncedAt *time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (SaDatasetSource) TableName() string {
|
||||
|
||||
@@ -23,19 +23,10 @@ func (m *SaDatasetSource) BeforeCreate(tx *gorm.DB) error {
|
||||
if err := ensureIdentity(&m.Identity); err != nil {
|
||||
return err
|
||||
}
|
||||
if m.OwnerID == 0 {
|
||||
m.OwnerID = m.CreatedBy
|
||||
}
|
||||
if m.CreatedBy == 0 {
|
||||
m.CreatedBy = m.OwnerID
|
||||
}
|
||||
if m.OwnerID == 0 {
|
||||
return errors.New("dataset source owner is required")
|
||||
}
|
||||
if err := resolveIdentity(tx, &SaUser{}, m.OwnerID, &m.OwnerIdentity); err != nil {
|
||||
return err
|
||||
}
|
||||
return resolveIdentity(tx, &SaUser{}, m.CreatedBy, &m.CreatedByIdentity)
|
||||
return resolveIdentity(tx, &SaUser{}, m.OwnerID, &m.OwnerIdentity)
|
||||
}
|
||||
|
||||
func (m *SaDatasetItem) BeforeCreate(tx *gorm.DB) error {
|
||||
|
||||
@@ -50,8 +50,6 @@ func TestDatasetSourceBeforeCreateAssociatesOwner(t *testing.T) {
|
||||
|
||||
require.Equal(t, user.ID, source.OwnerID)
|
||||
require.Equal(t, user.Identity, source.OwnerIdentity)
|
||||
require.Equal(t, user.ID, source.CreatedBy)
|
||||
require.Equal(t, user.Identity, source.CreatedByIdentity)
|
||||
}
|
||||
|
||||
func newIdentityTestDB(t *testing.T) *gorm.DB {
|
||||
|
||||
Reference in New Issue
Block a user