feat(cabana): ClearOnChange clears dependent columns when a relation changes a foreign key
RelationContract (hasMany) and FieldRelationContract (belongsTo) accept ClearOnChange, a list of nullable columns set to NULL whenever the relation changes the foreign key: a hasMany link and unlink (deferred ones included) clear them on the child, and a form save that moves a belongsTo key to another record or to none clears them on the record unless the same save writes the column through its own field. Boot refuses the option on a belongsToMany and for a missing, non-nullable, foreign-key or primary-key column. The zero value changes nothing.
This commit is contained in:
@@ -55,6 +55,15 @@ type RelationContract struct {
|
||||
Columns map[string]string
|
||||
HookPivotColumns []string
|
||||
ExcludedRelatedIDs func(parent any) ([]uint, error)
|
||||
// ClearOnChange names nullable columns of the related child (hasMany
|
||||
// only) that are set to NULL whenever this relation changes the child's
|
||||
// ForeignKey: a link and an unlink in the relation manager, deferred ones
|
||||
// included. Values that only make sense for one parent (a member's role
|
||||
// in an organisation, for example) then never move to another parent or
|
||||
// outlive the membership. Each column must be a pointer field of the
|
||||
// related model and neither the ForeignKey nor the primary key. The zero
|
||||
// value changes nothing.
|
||||
ClearOnChange []string
|
||||
}
|
||||
|
||||
// kind is the contract's normalized kind.
|
||||
@@ -482,10 +491,16 @@ func validateRelationContract(ctl pact.AdminController, contract RelationContrac
|
||||
if err := validatePivotContract(contract); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(contract.ClearOnChange) > 0 {
|
||||
return fmt.Errorf("relation %s: ClearOnChange is only valid on a hasMany contract", contract.Name)
|
||||
}
|
||||
case RelationHasMany:
|
||||
if err := validateHasManyContract(contract); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateClearOnChange(contract.NewRelated(), contract.ClearOnChange, contract.ForeignKey); err != nil {
|
||||
return fmt.Errorf("relation %s: %w", contract.Name, err)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("relation %s has unknown kind %q (want %s or %s)", contract.Name, contract.Kind, RelationBelongsToMany, RelationHasMany)
|
||||
}
|
||||
@@ -578,6 +593,46 @@ func validateHasManyContract(contract RelationContract) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateClearOnChange checks the ClearOnChange columns of a contract
|
||||
// against model: each must be a column of the model, held in a pointer
|
||||
// field (a nullable column), and neither foreignKey nor the primary key.
|
||||
func validateClearOnChange(model any, columns []string, foreignKey string) error {
|
||||
pk := primaryColumn(model)
|
||||
for _, col := range columns {
|
||||
if !identifier(col) {
|
||||
return fmt.Errorf("ClearOnChange column %q is not an identifier", col)
|
||||
}
|
||||
if col == foreignKey {
|
||||
return fmt.Errorf("ClearOnChange column %s is the foreign key", col)
|
||||
}
|
||||
if col == pk {
|
||||
return fmt.Errorf("ClearOnChange column %s is the primary key", col)
|
||||
}
|
||||
field, ok := structFieldByColumn(model, col)
|
||||
if !ok {
|
||||
return fmt.Errorf("ClearOnChange column %s is not a column of the model", col)
|
||||
}
|
||||
if field.Type.Kind() != reflect.Pointer {
|
||||
return fmt.Errorf("ClearOnChange column %s is not nullable (it needs a pointer field)", col)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// clearColumns sets each column of model to NULL, except the columns in
|
||||
// keep.
|
||||
func clearColumns(model any, columns []string, keep map[string]bool) error {
|
||||
for _, col := range columns {
|
||||
if keep[col] {
|
||||
continue
|
||||
}
|
||||
if err := setModelColumn(model, col, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// relationDeferrable reports whether a relation can be managed before its
|
||||
// parent is saved: a belongsToMany always (the pivot row is written on
|
||||
// save), a hasMany only with a nullable ForeignKey (the child is inserted
|
||||
@@ -945,6 +1000,11 @@ func (s RelationService) linkRelated(ctx context.Context, tx *gorm.DB, cc *Compi
|
||||
if err := setModelColumn(related, cr.Contract.ForeignKey, ownerPK); err != nil {
|
||||
return 0, lifecycleFailure(cc, err)
|
||||
}
|
||||
// A link always changes the key: it adopts only children whose
|
||||
// key is NULL.
|
||||
if err := clearColumns(related, cr.Contract.ClearOnChange, nil); err != nil {
|
||||
return 0, lifecycleFailure(cc, err)
|
||||
}
|
||||
if err := tx.WithContext(ctx).Save(related).Error; err != nil {
|
||||
return 0, lifecycleFailure(cc, err)
|
||||
}
|
||||
@@ -1245,6 +1305,9 @@ func (s RelationService) unlinkRelated(ctx context.Context, tx *gorm.DB, cc *Com
|
||||
if err := setModelColumn(child, cr.Contract.ForeignKey, nil); err != nil {
|
||||
return 0, lifecycleFailure(cc, err)
|
||||
}
|
||||
if err := clearColumns(child, cr.Contract.ClearOnChange, nil); err != nil {
|
||||
return 0, lifecycleFailure(cc, err)
|
||||
}
|
||||
if err := tx.WithContext(ctx).Save(child).Error; err != nil {
|
||||
return 0, lifecycleFailure(cc, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user