Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion db/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (db *DatabaseCollectionWithUser) storeAttachments(ctx context.Context, doc
}
digest := Sha1DigestKey(attachment)
key := MakeAttachmentKey(AttVersion2, doc.ID, digest)
_, updated := doc.SyncData.Attachments[name]
_, updated := doc.Attachments()[name]
newAttachments[key] = updatedAttachment{
body: attachment,
name: name,
Expand Down
26 changes: 13 additions & 13 deletions db/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ func TestStoreAttachments(t *testing.T) {
assert.NoError(t, err, "Couldn't update document")
assert.NotEmpty(t, revId, "Document revision id should be generated")
require.NotNil(t, doc)
assert.NotEmpty(t, doc.Attachments, "Attachment metadata should be populated")
attachment := doc.Attachments["att1.txt"].(map[string]interface{})
assert.NotEmpty(t, doc.Attachments(), "Attachment metadata should be populated")
attachment := doc.Attachments()["att1.txt"].(map[string]interface{})
assert.Equal(t, "text/plain", attachment["content_type"])
assert.Equal(t, "sha1-crv3IVNxp3JXbP6bizTHt3GB3O0=", attachment["digest"])
assert.Equal(t, 8, attachment["encoded_length"])
Expand All @@ -665,8 +665,8 @@ func TestStoreAttachments(t *testing.T) {
assert.NoError(t, err, "Couldn't update document")
assert.NotEmpty(t, revId, "Document revision id should be generated")
require.NotNil(t, doc)
assert.NotEmpty(t, doc.Attachments, "Attachment metadata should be populated")
attachment = doc.Attachments["att1.txt"].(map[string]interface{})
assert.NotEmpty(t, doc.Attachments(), "Attachment metadata should be populated")
attachment = doc.Attachments()["att1.txt"].(map[string]interface{})
assert.Equal(t, "text/plain", attachment["content_type"])
assert.Equal(t, "sha1-crv3IVNxp3JXbP6bizTHt3GB3O0=", attachment["digest"])
assert.Equal(t, 8, attachment["encoded_length"])
Expand All @@ -693,7 +693,7 @@ func TestStoreAttachments(t *testing.T) {
"stub": true,
"ver": 2,
},
}, doc.Attachments)
}, doc.Attachments())

// Simulate error scenario for attachment without data; stub is not provided; If the data is
// empty in attachment, the attachment must be a stub that repeats a parent attachment.
Expand Down Expand Up @@ -778,7 +778,7 @@ func TestMigrateBodyAttachments(t *testing.T) {
require.NoError(t, err)

// Fetch the raw doc sync data from the bucket to make sure we didn't store pre-2.5 attachments in syncData.
assert.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
assert.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
return db, ctx
}
Expand Down Expand Up @@ -809,7 +809,7 @@ func TestMigrateBodyAttachments(t *testing.T) {

// Fetch the raw doc sync data from the bucket to see if this read-only op unintentionally persisted the migrated meta.
syncData := GetRawSyncXattr(t, collection.dataStore, docKey)
assert.Empty(t, syncData.Attachments)
assert.Empty(t, syncData.AttachmentsPre4dot0)
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
})

Expand Down Expand Up @@ -839,7 +839,7 @@ func TestMigrateBodyAttachments(t *testing.T) {

// Fetch the raw doc sync data from the bucket to see if this read-only op unintentionally persisted the migrated meta.
syncData := GetRawSyncXattr(t, collection.dataStore, docKey)
assert.Empty(t, syncData.Attachments)
assert.Empty(t, syncData.AttachmentsPre4dot0)
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
})

Expand Down Expand Up @@ -881,7 +881,7 @@ func TestMigrateBodyAttachments(t *testing.T) {
require.NotContains(t, body1, BodyAttachments)

// Fetch the raw doc sync data from the bucket to make sure we actually moved attachments on write.
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
require.Equal(t, AttachmentMap{
"hello.txt": {
Digest: "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=",
Expand All @@ -906,7 +906,7 @@ func TestMigrateBodyAttachments(t *testing.T) {

// Fetch the raw doc sync data from the bucket to see if this read-only op unintentionally persisted the migrated meta.
syncData := GetRawSyncXattr(t, collection.dataStore, docKey)
require.Empty(t, syncData.Attachments)
require.Empty(t, syncData.AttachmentsPre4dot0)
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)

byeTxtData, err := base64.StdEncoding.DecodeString("Z29vZGJ5ZSBjcnVlbCB3b3JsZA==")
Expand Down Expand Up @@ -946,7 +946,7 @@ func TestMigrateBodyAttachments(t *testing.T) {
require.NotContains(t, body1, BodyAttachments)

// Fetch the raw doc sync data from the bucket to make sure we actually moved attachments on write.
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
require.Equal(t, AttachmentMap{
"hello.txt": {
Digest: "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=",
Expand Down Expand Up @@ -1066,7 +1066,7 @@ func TestMigrateBodyAttachmentsMerge(t *testing.T) {
"revpos": float64(1),
"stub": true,
},
}, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
}, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)

rev, err := collection.GetRev(ctx, docKey, "3-a", true, nil)
Expand Down Expand Up @@ -1102,7 +1102,7 @@ func TestMigrateBodyAttachmentsMerge(t *testing.T) {
"revpos": float64(1),
"stub": true,
},
}, GetRawSyncXattr(t, collection.dataStore, docKey).Attachments)
}, GetRawSyncXattr(t, collection.dataStore, docKey).AttachmentsPre4dot0)
base.RequireXattrNotFound(t, collection.dataStore, docKey, base.GlobalXattrName)
}

Expand Down
2 changes: 1 addition & 1 deletion db/background_mgr_attachment_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (a *AttachmentMigrationManager) Run(ctx context.Context, options map[string
return false
}

if syncData == nil || syncData.Attachments == nil {
if syncData == nil || syncData.AttachmentsPre4dot0 == nil {
// no attachments to migrate
return true
}
Expand Down
10 changes: 5 additions & 5 deletions db/background_mgr_attachment_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAttachmentMigrationTaskMixMigratedAndNonMigratedDocs(t *testing.T) {
key := fmt.Sprintf("%s_%d", t.Name(), i)
_, doc, err := collection.Put(ctx, key, docBody)
require.NoError(t, err)
assert.NotNil(t, doc.SyncData.Attachments)
assert.NotNil(t, doc.Attachments())
}

// Move some subset of the documents attachment metadata from global sync to sync data
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestAttachmentMigrationManagerResumeStoppedMigration(t *testing.T) {
key := fmt.Sprintf("%s_%d", t.Name(), i)
_, doc, err := collection.Put(ctx, key, docBody)
require.NoError(t, err)
require.NotNil(t, doc.SyncData.Attachments)
require.NotNil(t, doc.Attachments())
}
attachMigrationMgr := NewAttachmentMigrationManager(db.DatabaseContext)
require.NotNil(t, attachMigrationMgr)
Expand Down Expand Up @@ -206,14 +206,14 @@ func TestMigrationManagerDocWithSyncAndGlobalAttachmentMetadata(t *testing.T) {
var syncData SyncData
require.NoError(t, base.JSONUnmarshal(xattrs[base.SyncXattrName], &syncData))
// define some attachment meta on sync data
syncData.Attachments = AttachmentsMeta{}
syncData.AttachmentsPre4dot0 = AttachmentsMeta{}
att := map[string]interface{}{
"stub": true,
"digest": "sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0=",
"length": 11,
"revpos": 1,
}
syncData.Attachments["someAtt.txt"] = att
syncData.AttachmentsPre4dot0["someAtt.txt"] = att

updateXattrs := map[string][]byte{
base.SyncXattrName: base.MustJSONMarshal(t, syncData),
Expand Down Expand Up @@ -254,5 +254,5 @@ func TestMigrationManagerDocWithSyncAndGlobalAttachmentMetadata(t *testing.T) {
Revpos: 1,
},
}, GetRawGlobalSyncAttachments(t, collection.dataStore, key))
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, key).Attachments)
require.Empty(t, GetRawSyncXattr(t, collection.dataStore, key).AttachmentsPre4dot0)
}
4 changes: 2 additions & 2 deletions db/blip_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ func (bh *blipHandler) processRev(rq *blip.Message, stats *processRevStats) (err
currentDigests = make(map[string]string, len(bodyAtts))
for name, value := range bodyAtts {
// Check if we have this attachment name already, if we do, continue check
currentAttachment, ok := currentBucketDoc.Attachments[name]
currentAttachment, ok := currentBucketDoc.Attachments()[name]
if !ok {
// If we don't have this attachment already, ensure incoming revpos is greater than minRevPos, otherwise
// update to ensure it's fetched and uploaded
Expand Down Expand Up @@ -1301,7 +1301,7 @@ func (bh *blipHandler) processRev(rq *blip.Message, stats *processRevStats) (err
return err
}

newDoc.DocAttachments = GetBodyAttachments(body)
newDoc.SetAttachments(GetBodyAttachments(body))
delete(body, BodyAttachments)
newDoc.UpdateBody(body)
}
Expand Down
Loading
Loading