Skip to content

Commit

Permalink
Set updated_at with created_at when creating Document (#977)
Browse files Browse the repository at this point in the history
This commit ensures that when a new Document is created and attached,
the updated_at field is properly initialized with the created_at.

Prior to commit b494fa2, the updated_at field was correctly set
during the attachment process through a push-pull operation that
involved presence. However, after that commit, the updated_at field is
no longer updated when a document is attached, resulting in
inconsistencies.
  • Loading branch information
window9u authored Sep 2, 2024
1 parent 3f4f5d3 commit 800f139
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions server/backend/database/memory/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ func (d *DB) FindDocInfoByKeyAndOwner(
Owner: clientRefKey.ClientID,
ServerSeq: 0,
CreatedAt: now,
UpdatedAt: now,
AccessedAt: now,
}
if err := txn.Insert(tblDocuments, info); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion server/backend/database/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,9 @@ func (c *Client) FindDocInfoByKeyAndOwner(
"owner": clientRefKey.ClientID,
"server_seq": 0,
"created_at": now,
"updated_at": now,
},
})
}, options.FindOneAndUpdate().SetReturnDocument(options.After))
} else {
result = c.collection(ColDocuments).FindOne(ctx, filter)
if result.Err() == mongo.ErrNoDocuments {
Expand Down
7 changes: 5 additions & 2 deletions server/backend/database/testcases/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,9 @@ func RunCreateChangeInfosTest(t *testing.T, db database.Database, projectID type
// 01. Create a client and a document then attach the document to the client.
clientInfo, _ := db.ActivateClient(ctx, projectID, t.Name())
docInfo1, _ := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), docKey, true)
assert.Equal(t, docInfo1.Owner, clientInfo.ID)
assert.NotEqual(t, gotime.Date(1, gotime.January, 1, 0, 0, 0, 0, gotime.UTC), docInfo1.UpdatedAt)
assert.Equal(t, docInfo1.CreatedAt, docInfo1.UpdatedAt)
docRefKey := docInfo1.RefKey()
assert.NoError(t, clientInfo.AttachDocument(docRefKey.DocID, false))
assert.NoError(t, db.UpdateClientInfoAfterPushPull(ctx, clientInfo, docInfo1))
Expand All @@ -957,7 +960,7 @@ func RunCreateChangeInfosTest(t *testing.T, db database.Database, projectID type
doc := document.New(key.Key(t.Name()))
doc.SetActor(actorID)

// 02. update document only presence
// 02. Update document only presence
assert.NoError(t, doc.Update(func(root *json.Object, p *presence.Presence) error {
p.Set("key", "val")
return nil
Expand All @@ -968,7 +971,7 @@ func RunCreateChangeInfosTest(t *testing.T, db database.Database, projectID type
docInfo2, _ := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), docKey, true)
assert.Equal(t, updatedAt, docInfo2.UpdatedAt)

// 03. update document presence and operation
// 03. Update document presence and operation
assert.NoError(t, doc.Update(func(root *json.Object, p *presence.Presence) error {
p.Set("key", "val")
root.SetNewArray("array")
Expand Down

0 comments on commit 800f139

Please sign in to comment.