Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update presence_change migration for JSON to Protobuf conversion #1075

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion migrations/v0.5.6/migrate-presence-change.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ package v056

import (
"context"
"encoding/json"
"fmt"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"

"github.com/yorkie-team/yorkie/pkg/document/innerpresence"
"github.com/yorkie-team/yorkie/server/backend/database"
)

// validatePresenceChangeMigration validates if all string presence changes are properly migrated
Expand Down Expand Up @@ -75,11 +79,21 @@ func processMigrationBatchPresence(
},
})
} else {
p := &innerpresence.PresenceChange{}
err := json.Unmarshal([]byte(presenceChangeStr), p)
if err != nil {
return fmt.Errorf("unmarshal presence change: %w", err)
}

bytes, err := database.EncodePresenceChange(p)
if err != nil {
return fmt.Errorf("encode error: %w", err)
}
Comment on lines +82 to +91
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Handle individual document errors to prevent migration interruption

Currently, if an error occurs while unmarshaling or encoding a single document's presence change, the entire migration process stops. To enhance robustness, consider logging the error for that document and continuing with the rest of the batch. This ensures that one malformed document doesn't halt the entire migration.

Apply the following changes to handle errors individually:

 if err != nil {
-    return fmt.Errorf("unmarshal presence change: %w", err)
+    fmt.Printf("Warning: failed to unmarshal presence change for document %v: %v\n", doc["_id"], err)
+    continue
 }
 if err != nil {
-    return fmt.Errorf("encode error: %w", err)
+    fmt.Printf("Warning: failed to encode presence change for document %v: %v\n", doc["_id"], err)
+    continue
 }

Committable suggestion skipped: line range outside the PR's diff.

operation = mongo.NewUpdateOneModel().SetFilter(bson.M{
"_id": doc["_id"],
}).SetUpdate(bson.M{
"$set": bson.M{
"presence_change": []byte(presenceChangeStr),
"presence_change": bytes,
},
})
}
Expand Down
Loading