Finish ToOneSet
- Replace the custom update that actually set every model's relationship to the same thing with a real update method so we get logging & hooks
This commit is contained in:
parent
204ca1291d
commit
1887f23ca5
2 changed files with 59 additions and 3 deletions
|
@ -11,8 +11,10 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
|
|||
}
|
||||
}
|
||||
|
||||
_, err = exec.Exec(`update "{{.ForeignKey.Table}}" set "{{.ForeignKey.Column}}" = $1`, related.{{.Function.ForeignAssignment}})
|
||||
if err != nil {
|
||||
oldVal := {{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}
|
||||
{{.Function.Receiver}}.{{.Function.LocalAssignment}} = related.{{.Function.ForeignAssignment}}
|
||||
if err = {{.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil {
|
||||
{{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}} = oldVal
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -24,7 +26,9 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
|
|||
{{.Function.Receiver}}.R.{{.Function.Name}} = related
|
||||
}
|
||||
|
||||
{{.Function.Receiver}}.{{.Function.LocalAssignment}} = related.{{.Function.ForeignAssignment}}
|
||||
{{if .ForeignKey.Nullable}}
|
||||
{{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}.Valid = true
|
||||
{{end -}}
|
||||
return nil
|
||||
}
|
||||
{{- if .ForeignKey.Nullable}}
|
||||
|
@ -32,6 +36,15 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
|
|||
// Remove{{.Function.Name}} relationship.
|
||||
// Sets R.{{.Function.Name}} to nil.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(exec boil.Executor) error {
|
||||
var err error
|
||||
|
||||
{{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}.Valid = false
|
||||
if err = {{.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil {
|
||||
{{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}.Valid = true
|
||||
return err
|
||||
}
|
||||
|
||||
{{.Function.Receiver}}.R.{{.Function.Name}} = nil
|
||||
return nil
|
||||
}
|
||||
{{end -}}
|
||||
|
|
|
@ -56,6 +56,49 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
|||
{{- if .ForeignKey.Nullable}}
|
||||
|
||||
func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
||||
var err error
|
||||
|
||||
tx := MustTx(boil.Begin())
|
||||
defer tx.Rollback()
|
||||
|
||||
var a {{.LocalTable.NameGo}}
|
||||
var b {{.ForeignTable.NameGo}}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = randomize.Struct(seed, &b, {{$foreignVarNameSingular}}DBTypes, false, {{$foreignVarNameSingular}}PrimaryKeyColumns...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = a.Insert(tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = a.Set{{.Function.Name}}(tx, true, &b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = a.Remove{{.Function.Name}}(tx); err != nil {
|
||||
t.Error("failed to remove relationship")
|
||||
}
|
||||
|
||||
count, err := a.{{.Function.Name}}(tx).Count()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Error("want no relationships remaining")
|
||||
}
|
||||
|
||||
if a.R.{{.Function.Name}} != nil {
|
||||
t.Error("R struct entry should be nil")
|
||||
}
|
||||
|
||||
if a.{{.ForeignKey.Column | titleCase}}.Valid {
|
||||
t.Error("R struct entry should be nil")
|
||||
}
|
||||
}
|
||||
{{end -}}
|
||||
{{- end -}}
|
||||
|
|
Loading…
Add table
Reference in a new issue