Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
add namespace toString method
Browse files Browse the repository at this point in the history
  • Loading branch information
whhe committed Apr 25, 2020
1 parent 01650ec commit 514eb8b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
6 changes: 6 additions & 0 deletions model/event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import (
"fmt"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
Expand Down Expand Up @@ -28,3 +30,7 @@ type Namespace struct {
Database string `bson:"db,omitempty" json:"db,omitempty"`
Collection string `bson:"coll,omitempty" json:"coll,omitempty"`
}

func (ns Namespace) String() string {
return fmt.Sprintf("%s.%s", ns.Database, ns.Collection)
}
35 changes: 16 additions & 19 deletions operator/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,24 @@ func (m *mongoOperator) Replace(e model.ChangeEvent) error {
}

func (m *mongoOperator) Update(e model.ChangeEvent) error {
if e.FullDocument == nil {
update := bson.M{}
update := bson.M{}

if len(e.UpdateDescription.UpdatedFields) != 0 {
update["$set"] = e.UpdateDescription.UpdatedFields
}
if len(e.UpdateDescription.UpdatedFields) != 0 {
update["$set"] = e.UpdateDescription.UpdatedFields
}

if len(e.UpdateDescription.RemovedFields) != 0 {
unset := bson.M{}
for _, field := range e.UpdateDescription.RemovedFields {
unset[field] = ""
}
update["$unset"] = unset
if len(e.UpdateDescription.RemovedFields) != 0 {
unset := bson.M{}
for _, field := range e.UpdateDescription.RemovedFields {
unset[field] = ""
}

_, err := m.Database(e.Namespace.Database).
Collection(e.Namespace.Collection).
UpdateOne(context.Background(), e.DocumentKey, update)
return err
update["$unset"] = unset
}
return m.Replace(e)

_, err := m.Database(e.Namespace.Database).
Collection(e.Namespace.Collection).
UpdateOne(context.Background(), e.DocumentKey, update)
return err
}

func (m *mongoOperator) Drop(e model.ChangeEvent) error {
Expand All @@ -76,8 +73,8 @@ func (m *mongoOperator) Drop(e model.ChangeEvent) error {
}

func (m *mongoOperator) Rename(e model.ChangeEvent) error {
from := e.Namespace.Database + "." + e.Namespace.Collection
to := e.To.Database + "." + e.To.Collection
from := e.Namespace.String()
to := e.To.String()

result := m.Database("admin").
RunCommand(
Expand Down

0 comments on commit 514eb8b

Please sign in to comment.