From 916c9c6c2a1bce27f0ec04d8c44f3ba9648df780 Mon Sep 17 00:00:00 2001 From: roberChen Date: Fri, 26 Mar 2021 11:34:02 +0800 Subject: [PATCH] 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 --- commands/command.go | 2 +- commands/migrate/migrate_v03.go | 2 +- controllers/BlogController.go | 2 +- controllers/BookController.go | 2 +- controllers/BookMemberController.go | 2 +- controllers/DocumentController.go | 2 +- controllers/ItemsetsController.go | 2 +- controllers/LabelController.go | 2 +- controllers/ManagerController.go | 2 +- controllers/TemplateController.go | 2 +- database/mindoc.db | Bin 0 -> 188416 bytes models/AttachmentModel.go | 2 +- models/AttachmentResult.go | 2 +- models/Blog.go | 4 +- models/BookModel.go | 118 +++++++++++++++++++--------- models/BookResult.go | 2 +- models/Dashboard.go | 2 +- models/DocumentHistory.go | 2 +- models/DocumentSearchResult.go | 2 +- models/DocumentTree.go | 2 +- models/Itemsets.go | 7 +- models/LabelModel.go | 2 +- models/Logs.go | 2 +- models/Member.go | 10 +-- models/MemberResult.go | 2 +- models/MemberToken.go | 2 +- models/Migrations.go | 2 +- models/Options.go | 2 +- models/Relationship.go | 23 +++--- models/Team.go | 6 +- models/TeamMember.go | 2 +- models/TeamRelationship.go | 2 +- models/comment.go | 2 +- models/comment_result.go | 2 +- models/comment_vote.go | 2 +- 35 files changed, 135 insertions(+), 89 deletions(-) create mode 100644 database/mindoc.db diff --git a/commands/command.go b/commands/command.go index 5596f3cf..08593ad0 100644 --- a/commands/command.go +++ b/commands/command.go @@ -16,11 +16,11 @@ import ( "net/http" "github.com/beego/beego/v2/adapter" - "github.com/beego/beego/v2/adapter/orm" beegoCache "github.com/beego/beego/v2/client/cache" _ "github.com/beego/beego/v2/client/cache/memcache" "github.com/beego/beego/v2/client/cache/redis" _ "github.com/beego/beego/v2/client/cache/redis" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/server/web" "github.com/howeyc/fsnotify" diff --git a/commands/migrate/migrate_v03.go b/commands/migrate/migrate_v03.go index 95a2c5f1..39f8d472 100644 --- a/commands/migrate/migrate_v03.go +++ b/commands/migrate/migrate_v03.go @@ -6,7 +6,7 @@ import ( "strings" "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/models" ) diff --git a/controllers/BlogController.go b/controllers/BlogController.go index b26c2e20..5ff07b05 100644 --- a/controllers/BlogController.go +++ b/controllers/BlogController.go @@ -13,7 +13,7 @@ import ( "strings" "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/server/web" "github.com/mindoc-org/mindoc/conf" diff --git a/controllers/BookController.go b/controllers/BookController.go index 7408da1a..a30fce4b 100644 --- a/controllers/BookController.go +++ b/controllers/BookController.go @@ -18,7 +18,7 @@ import ( "net/http" "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" "github.com/mindoc-org/mindoc/graphics" "github.com/mindoc-org/mindoc/models" diff --git a/controllers/BookMemberController.go b/controllers/BookMemberController.go index bacf58ce..e30012d8 100644 --- a/controllers/BookMemberController.go +++ b/controllers/BookMemberController.go @@ -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" "github.com/mindoc-org/mindoc/models" ) diff --git a/controllers/DocumentController.go b/controllers/DocumentController.go index b70aaa28..be0cee38 100644 --- a/controllers/DocumentController.go +++ b/controllers/DocumentController.go @@ -15,7 +15,7 @@ import ( "strings" "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/server/web" "github.com/boombuler/barcode" diff --git a/controllers/ItemsetsController.go b/controllers/ItemsetsController.go index 4d60b277..eda28b05 100644 --- a/controllers/ItemsetsController.go +++ b/controllers/ItemsetsController.go @@ -2,7 +2,7 @@ package controllers import ( "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" "github.com/mindoc-org/mindoc/models" "github.com/mindoc-org/mindoc/utils/pagination" diff --git a/controllers/LabelController.go b/controllers/LabelController.go index 6ee3330a..e954dd3a 100644 --- a/controllers/LabelController.go +++ b/controllers/LabelController.go @@ -4,7 +4,7 @@ import ( "math" "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" "github.com/mindoc-org/mindoc/models" "github.com/mindoc-org/mindoc/utils/pagination" diff --git a/controllers/ManagerController.go b/controllers/ManagerController.go index ef70bc3e..360c6b8c 100644 --- a/controllers/ManagerController.go +++ b/controllers/ManagerController.go @@ -15,7 +15,7 @@ import ( "github.com/beego/beego/v2/adapter" "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" "github.com/mindoc-org/mindoc/models" "github.com/mindoc-org/mindoc/utils" diff --git a/controllers/TemplateController.go b/controllers/TemplateController.go index b02d1a9b..4348a1c0 100644 --- a/controllers/TemplateController.go +++ b/controllers/TemplateController.go @@ -4,7 +4,7 @@ import ( "errors" "strings" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/conf" "github.com/mindoc-org/mindoc/models" ) diff --git a/database/mindoc.db b/database/mindoc.db new file mode 100644 index 0000000000000000000000000000000000000000..cd6c770f7ac66411f71a06d7af873dfbcb6caf32 GIT binary patch literal 188416 zcmeI5S#TT2na2kNg-fE~SdL*^radIhPzXg42O*Huv8E+R=2{|Uk=$6Zv%nD3BqkbQ zpqT+>IlEOBb@-BxWPQd?Dc<J~Y5jbxXMk7B+DmxDP72toT`*xx9vHc7@174lOwksNU*8$pcK2s$I?&A zikKAm6f5LZGD~E3YuiSJ#={V;K<=4>>Y_C?8wyQ_~;l-@?c_Ya%)xW9G4|2 zc8-(N8BX333x$HEMD`~3jZ7VwWP5s8Gn&BDq6#x7N-@G(3|lZ)5Jg*fT9S^|FP$YH zveM0SGEKNiVM*qRoKz%PtyA%uq_doSl$Va@QmjhuRn27{mM12PlKlI%1sP~CQ@Esn%fevMKzgt(?EI7ehfkZApCl%OVaK}kw^ z(v0ZT*yx>833f}$YQ(K<%5MCiFBorg1@7o}FhY(e_bSIk+VhfJL6vlOvRq-4;d0l; z`D=adKu3q;)N!i^NO_eOU(s^8U9m^0k(ouAt=Jkg8?H+fax7TsH%c4m&o6~t_ zggixB?)27hEf2NfmV2%a@rpWxrC?2{uPlkAeu=6z-B#aHQK$l2SyBDn<#h$_>|KU- z1qDro%RPJ#r(Etpd%NSQO%``8ZB2*uZ+8u8-P=ZN6{NP#jkH&fS*e8$ZMI6X%To(m zX4vlG_JE+9cY9Q^Fw+5{QpdHfuMskgYEyF5Z0zB|u=u)dX~xX)L`lnn zZdOVIRV374Y|9s>MM23BxfG`nT9KP3V)Np8S(u|^hrR@gxfvy?N=G%jT|Q2m z=0(?tJf|qfDW$p+ipr@4r5SL97gVXD{>#n==oBQGmI~UK*|^EmQdXPiB(2g|Rf*I! zqS`QJPb93UE69ruiX|3QiR)%!XP{Q|n7*0}<`9gc745vV0Ls3=Ko)F?S!rfD7>+ncycpIVp8rP%SY za!+cpQ(F8~@K7sFz1ML>xA*3RMmzbbHxX1ur3n87o5JM@{*BMxf}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~sQJGvVDk^HyY1V8`;KmY_l00ck)1V8`;KwvEqXmxrWE!zA4asR)TniX{c0T2KI5C8!X009sH z0T2KI5CDM+0lg)}s zfdB}A00@8p2!H?xfB*=900@9U1p(~;SKxpH1V8`;KmY_l00ck)1V8`;KmY{RBmwOI zugPXbr9c1#KmY_l00ck)1V8`;KmY_lpn?GQ|0{670RkWZ0w4eaAOHd&00JNY0w4ea zYmxx=|JP)*qEa9L0w4eaAOHd&00JNY0w4eaAW%U7_x}|*-~a&-009sH0T2KI5C8!X z009sHfi+2>t@S#_&-|L%5c2%q^O5H<&lXp3-COJSw6ZO)IDhH5q2+5Ec^YheN>`T8 zmH!?4vMVs! z8l(~~o0N&jse+VKGD5zdU+3E@~&b9Vh*|LD9)%^VCQXC297Ba=#vv*yz~a#9gL+s1i@H<71T}lxL}5 zsh#e?5X~=8o1aafJjX9BlcOD>ex9Y^;HiJ;a0j}(9S`g%DS}z<^31yHD1<3irWA%x zeLZii7-sBB<%oqspPh18Hmo^4niZvKPOM|NYm|--G`eOa(-JlEIW@(qp0y>1CzUHo_^i zxMdwq4^UK2EzkkW&Zj=yd|_G?l#D&rpDYC4?mZI2bYtG?ni z8YqmY)uTlHL87wf=;l9H&?cA5>bEaAbb~8!uxA;Al@(@X<9f8~02pr^-i zI%|O`1yVXC$Bi|JN?|r9+iP{Kswpi$mTG;5Xg!+to3j2?-Ic8Q+NtY}vAXK%F;`YcJ<3tCa+z z0o?yzE5`q8-fB*=900@8p2!H?x zfB*=9z~vIa{{Q6?M0^kc0T2KI5C8!X009sH0T2KI5V-OQVE_NhZzGft1V8`;KmY_l z00ck)1V8`;KmY_Tmw>ka@9;m$(0}-V00@8p2!H?xfB*=900@8p2!H?x{51$1b9k9> zUz>Mg!gJ~UXD?oO>C%huE ", err) return err } - if err := o.Begin(); err != nil { + if _, err := o.Begin(); err != nil { logs.Error("开启事物时出错 -> ", err) return err } @@ -209,49 +210,87 @@ func (book *Book) Copy(identify string) error { book.CommentCount = 0 book.HistoryCount = 0 - if _, err := o.Insert(book); err != nil { - logs.Error("复制项目时出错 -> ", err) - o.Rollback() + /* v2 version of beego remove the o.Rollback api for transaction operation. + * typically, in v1, you can write code like this: + * + * o := orm.NewOrm() + * if err := o.Operateion(); err != nil { + * o.Rollback() + * ... + * } + * + * however, in v2, this is not available. beego will handles the transaction in new way using + * cluster. the new code is like below: + * + * o := orm.NewOrm() + * if err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error{ + * err := o.Operations() + * if err != nil { + * return err + * } + * ... + * }); err != nil { + * ... + * } + * + * when operation failed, it will automatically calls o.Rollback() for TxOrmer. + * more details see https://beego.me/docs/mvc/model/transaction.md + */ + if err := o.DoTx(func(ctx context.Context, txo orm.TxOrmer) error { + _, err := txo.Insert(book) + return err + + }); err != nil { + logs.Error("复制项目时出错: %s", err) return err } + var rels []*Relationship - if _, err := o.QueryTable(NewRelationship().TableNameWithPrefix()).Filter("book_id", bookId).All(&rels); err != nil { + if err := o.DoTx(func(ctx context.Context, txo orm.TxOrmer) error { + _, err := txo.QueryTable(NewRelationship().TableNameWithPrefix()).Filter("book_id", bookId).All(&rels) + return err + }); err != nil { logs.Error("复制项目关系时出错 -> ", err) - o.Rollback() return err } for _, rel := range rels { rel.BookId = book.BookId rel.RelationshipId = 0 - if _, err := o.Insert(rel); err != nil { + if err := o.DoTx(func(ctx context.Context, txo orm.TxOrmer) error { + _, err := txo.Insert(rel) + return err + }); err != nil { logs.Error("复制项目关系时出错 -> ", err) - o.Rollback() return err } } var docs []*Document - if _, err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id", bookId).Filter("parent_id", 0).All(&docs); err != nil && err != orm.ErrNoRows { + if err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error { + _, err := txOrm.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id", bookId).Filter("parent_id", 0).All(&docs) + return err + }); err != nil && err != orm.ErrNoRows { logs.Error("读取项目文档时出错 -> ", err) - o.Rollback() return err } + if len(docs) > 0 { - if err := recursiveInsertDocument(docs, o, book.BookId, 0); err != nil { + if err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error { + return recursiveInsertDocument(docs, txOrm, book.BookId, 0) + }); err != nil { logs.Error("复制项目时出错 -> ", err) - o.Rollback() return err } } - return o.Commit() + return nil } //递归的复制文档 -func recursiveInsertDocument(docs []*Document, o orm.Ormer, bookId int, parentId int) error { +func recursiveInsertDocument(docs []*Document, o orm.TxOrmer, bookId int, parentId int) error { for _, doc := range docs { docId := doc.DocumentId @@ -418,42 +457,49 @@ func (book *Book) ThoroughDeleteBook(id int) error { o.Begin() //删除附件,这里没有删除实际物理文件 - _, err = o.Raw("DELETE FROM "+NewAttachment().TableNameWithPrefix()+" WHERE book_id=?", book.BookId).Exec() - if err != nil { - o.Rollback() + if err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error { + _, err = txOrm.Raw("DELETE FROM "+NewAttachment().TableNameWithPrefix()+" WHERE book_id=?", book.BookId).Exec() + return err + }); err != nil { return err } //删除文档 - _, err = o.Raw("DELETE FROM "+NewDocument().TableNameWithPrefix()+" WHERE book_id = ?", book.BookId).Exec() - - if err != nil { - o.Rollback() + if err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error { + _, err = txOrm.Raw("DELETE FROM "+NewDocument().TableNameWithPrefix()+" WHERE book_id = ?", book.BookId).Exec() + return err + }); err != nil { return err } //删除项目 - _, err = o.Raw("DELETE FROM "+book.TableNameWithPrefix()+" WHERE book_id = ?", book.BookId).Exec() - - if err != nil { - o.Rollback() + if err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error { + _, err = txOrm.Raw("DELETE FROM "+book.TableNameWithPrefix()+" WHERE book_id = ?", book.BookId).Exec() + return err + }); err != nil { return err } //删除关系 - _, err = o.Raw("DELETE FROM "+NewRelationship().TableNameWithPrefix()+" WHERE book_id = ?", book.BookId).Exec() - if err != nil { - o.Rollback() + + if err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error { + _, err = txOrm.Raw("DELETE FROM "+NewRelationship().TableNameWithPrefix()+" WHERE book_id = ?", book.BookId).Exec() + return err + }); err != nil { return err } - _, err = o.Raw(fmt.Sprintf("DELETE FROM %s WHERE book_id=?", NewTeamRelationship().TableNameWithPrefix()), book.BookId).Exec() - if err != nil { - o.Rollback() + + if err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error { + _, err = txOrm.Raw(fmt.Sprintf("DELETE FROM %s WHERE book_id=?", NewTeamRelationship().TableNameWithPrefix()), book.BookId).Exec() + return err + }); err != nil { return err } //删除模板 - _, err = o.Raw("DELETE FROM "+NewTemplate().TableNameWithPrefix()+" WHERE book_id = ?", book.BookId).Exec() - if err != nil { - o.Rollback() + + if err := o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error { + _, err = txOrm.Raw("DELETE FROM "+NewTemplate().TableNameWithPrefix()+" WHERE book_id = ?", book.BookId).Exec() + return err + }); err != nil { return err } @@ -470,7 +516,7 @@ func (book *Book) ThoroughDeleteBook(id int) error { logs.Error("删除项目附件和图片失败 ->", err) } - return o.Commit() + return nil } diff --git a/models/BookResult.go b/models/BookResult.go index 2b1a3c02..f26a4d4e 100644 --- a/models/BookResult.go +++ b/models/BookResult.go @@ -16,7 +16,7 @@ import ( "github.com/PuerkitoBio/goquery" "github.com/beego/beego/v2/adapter" "github.com/beego/beego/v2/adapter/logs" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/server/web" "github.com/mindoc-org/mindoc/conf" "github.com/mindoc-org/mindoc/converter" diff --git a/models/Dashboard.go b/models/Dashboard.go index 0ed42356..e8221bd4 100644 --- a/models/Dashboard.go +++ b/models/Dashboard.go @@ -1,6 +1,6 @@ package models -import "github.com/beego/beego/v2/adapter/orm" +import "github.com/beego/beego/v2/client/orm" type Dashboard struct { BookNumber int64 `json:"book_number"` diff --git a/models/DocumentHistory.go b/models/DocumentHistory.go index 655c8292..5f3c7b2b 100644 --- a/models/DocumentHistory.go +++ b/models/DocumentHistory.go @@ -3,7 +3,7 @@ package models import ( "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/DocumentSearchResult.go b/models/DocumentSearchResult.go index 05c92f77..78048bf7 100644 --- a/models/DocumentSearchResult.go +++ b/models/DocumentSearchResult.go @@ -5,7 +5,7 @@ import ( "strings" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" ) diff --git a/models/DocumentTree.go b/models/DocumentTree.go index 029d8d35..7a0c8dcc 100644 --- a/models/DocumentTree.go +++ b/models/DocumentTree.go @@ -6,7 +6,7 @@ import ( "html/template" "math" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/Itemsets.go b/models/Itemsets.go index abe290a6..de7ba523 100644 --- a/models/Itemsets.go +++ b/models/Itemsets.go @@ -5,7 +5,7 @@ import ( "strings" "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/mindoc-org/mindoc/conf" "github.com/mindoc-org/mindoc/utils" @@ -112,8 +112,9 @@ func (item *Itemsets) Delete(itemId int) (err error) { if !item.Exist(itemId) { return errors.New("项目空间不存在") } - o := orm.NewOrm() - if err := o.Begin(); err != nil { + ormer := orm.NewOrm() + o, err := ormer.Begin() + if err != nil { logs.Error("开启事物失败 ->", err) return err } diff --git a/models/LabelModel.go b/models/LabelModel.go index 363ba4d1..49a8ebf8 100644 --- a/models/LabelModel.go +++ b/models/LabelModel.go @@ -3,7 +3,7 @@ package models import ( "strings" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/Logs.go b/models/Logs.go index 0e8e22a0..da662f9d 100644 --- a/models/Logs.go +++ b/models/Logs.go @@ -5,7 +5,7 @@ import ( "sync/atomic" "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/Member.go b/models/Member.go index 5cfe798e..df44ab4c 100644 --- a/models/Member.go +++ b/models/Member.go @@ -19,7 +19,7 @@ import ( "math" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/server/web" "github.com/mindoc-org/mindoc/conf" @@ -81,7 +81,7 @@ func (m *Member) Login(account string, password string) (*Member, error) { logs.Info("转入 HTTP 接口登陆 ->", account) return member.httpLogin(account, password) } else { - logs.Error("user login for `%s`: %s",account, err) + logs.Error("user login for `%s`: %s", account, err) return member, ErrMemberNoExist } } @@ -180,7 +180,7 @@ func (m *Member) ldapLogin(account string, password string) (*Member, error) { } func (m *Member) httpLogin(account, password string) (*Member, error) { - urlStr,_ := web.AppConfig.String("http_login_url") + urlStr, _ := web.AppConfig.String("http_login_url") if urlStr == "" { return nil, ErrMemberAuthMethodInvalid } @@ -462,9 +462,9 @@ func (m *Member) Valid(is_hash_password bool) error { //删除一个用户. func (m *Member) Delete(oldId int, newId int) error { - o := orm.NewOrm() + ormer := orm.NewOrm() - err := o.Begin() + o, err := ormer.Begin() if err != nil { return err diff --git a/models/MemberResult.go b/models/MemberResult.go index 0e1925bd..d3b69f3e 100644 --- a/models/MemberResult.go +++ b/models/MemberResult.go @@ -3,7 +3,7 @@ package models import ( "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/MemberToken.go b/models/MemberToken.go index 6a57f693..5a4bd96e 100644 --- a/models/MemberToken.go +++ b/models/MemberToken.go @@ -3,7 +3,7 @@ package models import ( "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/Migrations.go b/models/Migrations.go index 05db4de4..a91a4870 100644 --- a/models/Migrations.go +++ b/models/Migrations.go @@ -3,7 +3,7 @@ package models import ( "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/Options.go b/models/Options.go index 1d7f2d82..8673b9ff 100644 --- a/models/Options.go +++ b/models/Options.go @@ -1,7 +1,7 @@ package models import ( - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/Relationship.go b/models/Relationship.go index 6074f21e..22ab6382 100644 --- a/models/Relationship.go +++ b/models/Relationship.go @@ -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 { diff --git a/models/Team.go b/models/Team.go index 17a5fd9d..c7226d76 100644 --- a/models/Team.go +++ b/models/Team.go @@ -4,7 +4,7 @@ import ( "errors" "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/mindoc-org/mindoc/conf" ) @@ -59,9 +59,9 @@ func (t *Team) Delete(id int) (err error) { if id <= 0 { return ErrInvalidParameter } - o := orm.NewOrm() + ormer := orm.NewOrm() - err = o.Begin() + o, err := ormer.Begin() if err != nil { logs.Error("开启事物时出错 ->", err) diff --git a/models/TeamMember.go b/models/TeamMember.go index 3eb1f88d..a4302085 100644 --- a/models/TeamMember.go +++ b/models/TeamMember.go @@ -3,7 +3,7 @@ package models import ( "errors" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/TeamRelationship.go b/models/TeamRelationship.go index 0efb61cc..e6e82a7f 100644 --- a/models/TeamRelationship.go +++ b/models/TeamRelationship.go @@ -4,7 +4,7 @@ import ( "errors" "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/comment.go b/models/comment.go index b018ab9b..0786a464 100644 --- a/models/comment.go +++ b/models/comment.go @@ -4,7 +4,7 @@ import ( "errors" "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/conf" ) diff --git a/models/comment_result.go b/models/comment_result.go index 01ec524c..acbd1eec 100644 --- a/models/comment_result.go +++ b/models/comment_result.go @@ -1,6 +1,6 @@ package models -import "github.com/beego/beego/v2/adapter/orm" +import "github.com/beego/beego/v2/client/orm" type CommentResult struct { Comment diff --git a/models/comment_vote.go b/models/comment_vote.go index 63eb4698..544df97f 100644 --- a/models/comment_vote.go +++ b/models/comment_vote.go @@ -3,7 +3,7 @@ package models import ( "time" - "github.com/beego/beego/v2/adapter/orm" + "github.com/beego/beego/v2/client/orm" "github.com/mindoc-org/mindoc/conf" )