Fix: preheat install update transaction.

Signed-off-by: fanjiankong <fanjiankong@tencent.com>
This commit is contained in:
fanjiankong 2020-07-22 08:15:18 +08:00
parent 112e38a080
commit adbdaaffe6

View File

@ -4,7 +4,6 @@ import (
"context" "context"
beego_orm "github.com/astaxie/beego/orm" beego_orm "github.com/astaxie/beego/orm"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/orm" "github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/lib/q" "github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider" "github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider"
@ -89,13 +88,10 @@ func (d *dao) GetByName(ctx context.Context, name string) (instance *provider.In
// Update updates distribution instance. // Update updates distribution instance.
func (d *dao) Update(ctx context.Context, instance *provider.Instance, props ...string) error { func (d *dao) Update(ctx context.Context, instance *provider.Instance, props ...string) error {
var o, err = orm.FromContext(ctx) var trans = func(ctx context.Context) (err error) {
o, err := orm.FromContext(ctx)
if err != nil { if err != nil {
return err return
}
err = o.Begin()
if err != nil {
return err
} }
// check default instances first // check default instances first
@ -104,10 +100,7 @@ func (d *dao) Update(ctx context.Context, instance *provider.Instance, props ...
_, err = o.Raw("UPDATE ? SET default = false WHERE id != ?", instance.TableName(), instance.ID).Exec() _, err = o.Raw("UPDATE ? SET default = false WHERE id != ?", instance.TableName(), instance.ID).Exec()
if err != nil { if err != nil {
if e := o.Rollback(); e != nil { return
err = errors.Wrap(e, err.Error())
}
return err
} }
break break
@ -115,14 +108,10 @@ func (d *dao) Update(ctx context.Context, instance *provider.Instance, props ...
} }
_, err = o.Update(instance, props...) _, err = o.Update(instance, props...)
if err != nil { return
if e := o.Rollback(); e != nil {
err = errors.Wrap(e, err.Error())
} }
} else { return orm.WithTransaction(trans)(ctx)
err = o.Commit()
}
return err
} }
// Delete deletes one distribution instance by id. // Delete deletes one distribution instance by id.