Commit 27ab15af authored by Amos Wenger's avatar Amos Wenger

woha

parent ebf651fe
Pipeline #11013 failed with stage
in 18 seconds
......@@ -34,5 +34,11 @@ func withContext(t *testing.T, models []interface{}, f WithContextFunc) {
wtest.Must(t, c.AutoMigrate(conn))
defer func() {
c.ScopeMap.Each(func(scope *hades.Scope) error {
return c.ExecRaw(conn, "DROP TABLE "+scope.TableName(), nil)
})
}()
f(conn, c)
}
......@@ -360,7 +360,7 @@ func Test_ManyToManyThorough(t *testing.T) {
})
}
ordie(c.Save(conn, p))
ordie(c.Save(conn, p, hades.AssocReplace("Authors")))
assertCount(&Piece{}, 1)
assertCount(&Author{}, len(originalAuthors)+1+1200)
......
......@@ -147,6 +147,19 @@ func (c *Context) SaveNoTransaction(conn *sqlite.Conn, rec interface{}, opts ...
walk = func(p reflect.Value, pri *RecordInfo, v reflect.Value, vri *RecordInfo, persist bool) error {
if v.Kind() == reflect.Slice {
cull := false
if vri.Relationship != nil {
switch vri.Relationship.Kind {
case "has_many":
if vri.Field.Mode() == AssocModeReplace {
cull = true
}
case "many_to_many":
// culling is done later, but let's record the ManyToMany now
vri.ManyToMany.Mark(p)
}
}
for i := 0; i < v.Len(); i++ {
err := visit(p, pri, v.Index(i), vri, persist)
if err != nil {
......@@ -154,10 +167,7 @@ func (c *Context) SaveNoTransaction(conn *sqlite.Conn, rec interface{}, opts ...
}
}
if vri.Relationship != nil &&
vri.Relationship.Kind == "has_many" &&
vri.Field.Mode() == AssocModeReplace {
if cull {
var oldValuePKs []string
rel := vri.Relationship
......
......@@ -49,3 +49,13 @@ func (sm *ScopeMap) ByDBName(dbname string) *Scope {
func (sm *ScopeMap) ByType(typ reflect.Type) *Scope {
return sm.byType[typ]
}
func (sm *ScopeMap) Each(f func(*Scope) error) error {
for _, scope := range sm.byDBName {
err := f(scope)
if err != nil {
return err
}
}
return nil
}
......@@ -55,7 +55,7 @@ func Save(conn *sqlite.Conn) (releaseFn func(*error)) {
releaseFn, err := savepoint(conn, name)
if err != nil {
if sqlite.ErrCode(err) == sqlite.SQLITE_INTERRUPT {
if sqlite.ErrCode(errors.Cause(err)) == sqlite.SQLITE_INTERRUPT {
return func(errp *error) {
if *errp == nil {
*errp = err
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment