fix(models): use client/orm instead of adapter/orm

- using new orm api, change some related logics
- newer orm api has the concept of TxOrmer, for purpose of transaction
handling. A transaction ormer in v2 is a stateful object, it should be
dropped after using. A Ormer object is stateless and thread(routine)
    safe, and should not be used for transaction handling. More details
    count be found at official doc:
    https://beego.me/docs/mvc/model/orm.md, and pr note: https://github.com/mindoc-org/mindoc/pull/662#issuecomment-807040262
This commit is contained in:
roberChen
2021-03-26 11:34:02 +08:00
parent 8b7d56a547
commit 916c9c6c2a
35 changed files with 135 additions and 89 deletions

View File

@@ -4,7 +4,7 @@ import (
"errors"
"github.com/beego/beego/v2/adapter/logs"
"github.com/beego/beego/v2/adapter/orm"
"github.com/beego/beego/v2/client/orm"
"github.com/mindoc-org/mindoc/conf"
)
@@ -122,11 +122,11 @@ func (m *Relationship) Insert() error {
return err
}
func (m *Relationship) Update() error {
o := orm.NewOrm()
_, err := o.Update(m)
func (m *Relationship) Update(txOrm orm.TxOrmer) error {
_, err := txOrm.Update(m)
if err != nil {
txOrm.Rollback()
}
return err
}
@@ -152,11 +152,11 @@ func (m *Relationship) DeleteByBookIdAndMemberId(book_id, member_id int) error {
}
func (m *Relationship) Transfer(book_id, founder_id, receive_id int) error {
o := orm.NewOrm()
ormer := orm.NewOrm()
founder := NewRelationship()
err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).Filter("member_id", founder_id).One(founder)
err := ormer.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).Filter("member_id", founder_id).One(founder)
if err != nil {
return err
@@ -166,12 +166,12 @@ func (m *Relationship) Transfer(book_id, founder_id, receive_id int) error {
}
receive := NewRelationship()
err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).Filter("member_id", receive_id).One(receive)
err = ormer.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).Filter("member_id", receive_id).One(receive)
if err != orm.ErrNoRows && err != nil {
return err
}
o.Begin()
o, _ := ormer.Begin()
founder.RoleId = conf.BookAdmin
@@ -179,8 +179,7 @@ func (m *Relationship) Transfer(book_id, founder_id, receive_id int) error {
receive.RoleId = conf.BookFounder
receive.BookId = book_id
if err := founder.Update(); err != nil {
o.Rollback()
if err := founder.Update(o); err != nil {
return err
}
if receive.RelationshipId > 0 {