Skip to content

Commit

Permalink
fix(node): commit log need to use scope.DB()
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Mar 26, 2019
1 parent 1722e36 commit 88c10a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
24 changes: 19 additions & 5 deletions core/node/sql.go
Expand Up @@ -47,10 +47,10 @@ func (n *Node) handleCommitLog(operation string, scope *gorm.Scope) {

if indirectScopeValue := scope.IndirectValue(); indirectScopeValue.Kind() == reflect.Slice {
for i := 0; i < indirectScopeValue.Len(); i++ {
n.sendCommitLog(n.createCommitLog(operation, indirectScopeValue.Index(i)))
n.sendCommitLog(n.createCommitLog(scope, operation, indirectScopeValue.Index(i)))
}
} else {
n.sendCommitLog(n.createCommitLog(operation, indirectScopeValue))
n.sendCommitLog(n.createCommitLog(scope, operation, indirectScopeValue))
}
}

Expand All @@ -68,7 +68,7 @@ func (n *Node) sendCommitLog(commitLog *node.CommitLog) {
}
}

func (n *Node) createCommitLog(operation string, reflectValue reflect.Value) *node.CommitLog {
func (n *Node) createCommitLog(scope *gorm.Scope, operation string, reflectValue reflect.Value) *node.CommitLog {
var err error

// Only get address from non-pointer
Expand All @@ -93,27 +93,41 @@ func (n *Node) createCommitLog(operation string, reflectValue reflect.Value) *no
switch data := reflectValue.Interface().(type) {
case *entity.Contact:
if operation != "delete" {
data, err = sql.ContactByID(n.sqlDriver, data.ID)
data, err = sql.ContactByID(scope.DB(), data.ID)
if err != nil {
return nil
}
}
log.Entity = &node.CommitLog_Entity{Contact: data}
case *entity.Device:
if operation != "delete" {
data, err = sql.DeviceByID(scope.DB(), data.ID)
if err != nil {
return nil
}
}
log.Entity = &node.CommitLog_Entity{Device: data}
case *entity.Conversation:
if operation != "delete" {
data, err = sql.ConversationByID(n.sqlDriver, data.ID)
data, err = sql.ConversationByID(scope.DB(), data.ID)
if err != nil {
return nil
}
}
log.Entity = &node.CommitLog_Entity{Conversation: data}
case *entity.ConversationMember:
data, err = sql.ConversationMemberByID(scope.DB(), data.ID)
if err != nil {
return nil
}
log.Entity = &node.CommitLog_Entity{ConversationMember: data}
case *entity.Config:
log.Entity = &node.CommitLog_Entity{Config: data}
case *entity.Event:
data, err = sql.EventByID(scope.DB(), data.ID)
if err != nil {
return nil
}
log.Entity = &node.CommitLog_Entity{Event: data}
case *entity.DevicePushConfig:
log.Entity = &node.CommitLog_Entity{DevicePushConfig: data}
Expand Down
8 changes: 4 additions & 4 deletions core/sql/helpers.go
Expand Up @@ -41,8 +41,8 @@ func FindContact(db *gorm.DB, input *entity.Contact) (*entity.Contact, error) {
}

func ConversationMemberByID(db *gorm.DB, id string) (*entity.ConversationMember, error) {
var contact entity.ConversationMember
return &contact, db.First(&contact, "ID = ?", id).Error
var conversationMember entity.ConversationMember
return &conversationMember, db.First(&conversationMember, "ID = ?", id).Error
}

func DeviceByID(db *gorm.DB, id string) (*entity.Device, error) {
Expand All @@ -64,8 +64,8 @@ func MembersByConversationID(db *gorm.DB, conversationID string) ([]*entity.Conv
}

func EventByID(db *gorm.DB, id string) (*entity.Event, error) {
var contact entity.Event
return &contact, db.First(&contact, "ID = ?", id).Error
var event entity.Event
return &event, db.First(&event, "ID = ?", id).Error
}

func ConversationOneToOne(db *gorm.DB, myselfID, contactID string) (*entity.Conversation, error) {
Expand Down

0 comments on commit 88c10a4

Please sign in to comment.