MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestMigrateMetadata

Function TestMigrateMetadata

db/import_test.go:308–360  ·  view source on GitHub ↗

There are additional tests that exercise the import functionality in rest/import_test.go 1. Write a doc to the bucket 2. Build params to migrateMeta (existing doc, raw doc, cas.. sgbucket docs) 3. Update doc in the bucket with new expiry 4. Call migrateMeta with stale args that have old expiry 5. As

(t *testing.T)

Source from the content-addressed store, hash-verified

306// See SG PR #3109 for more details on motivation for this test
307// Tests when preserve expiry is not used (CBS < 7.0.0)
308func TestMigrateMetadata(t *testing.T) {
309
310 if !base.TestUseXattrs() {
311 t.Skip("This test only works with XATTRS enabled")
312 }
313
314 base.SetUpTestLogging(t, base.LevelInfo, base.KeyMigrate, base.KeyImport)
315
316 db, ctx := setupTestDB(t)
317 defer db.Close(ctx)
318
319 collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db)
320
321 key := "TestMigrateMetadata"
322 bodyBytes := rawDocWithSyncMeta()
323 body := Body{}
324 err := body.Unmarshal(bodyBytes)
325 assert.NoError(t, err, "Error unmarshalling body")
326
327 // Create via the SDK with sync metadata intact
328 expirySeconds := time.Second * 30
329 syncMetaExpiry := time.Now().Add(expirySeconds)
330 _, err = collection.dataStore.Add(key, uint32(syncMetaExpiry.Unix()), bodyBytes)
331 assert.NoError(t, err, "Error writing doc w/ expiry")
332
333 // Get the existing bucket doc
334 _, existingBucketDoc, err := collection.GetDocWithXattrs(ctx, key, DocUnmarshalAll)
335 require.NoError(t, err)
336 // Set the expiry value to a stale value (it's about to be stale, since below it will get updated to a later value)
337 existingBucketDoc.Expiry = uint32(syncMetaExpiry.Unix())
338
339 // Update doc in the bucket with new expiry
340 laterExpirySeconds := time.Second * 60
341 laterSyncMetaExpiry := time.Now().Add(laterExpirySeconds)
342 updateCallbackFn := func(current []byte) (updated []byte, expiry *uint32, isDelete bool, err error) {
343 // This update function will not be "competing" with other updates, so it doesn't need
344 // to handle being called back multiple times or performing any merging with existing values.
345 exp := uint32(laterSyncMetaExpiry.Unix())
346 return bodyBytes, &exp, false, nil
347 }
348 _, err = collection.dataStore.Update(
349 key,
350 uint32(laterSyncMetaExpiry.Unix()), // it's a bit confusing why the expiry needs to be passed here AND via the callback fn
351 updateCallbackFn,
352 )
353 require.NoError(t, err)
354
355 // Call migrateMeta with stale args that have old stale expiry
356 _, err = collection.migrateMetadata(ctx, key, existingBucketDoc, &sgbucket.MutateInOptions{PreserveExpiry: false})
357 assert.True(t, err != nil)
358 assert.True(t, err == base.ErrCasFailureShouldRetry)
359
360}
361
362// Tests metadata migration where a document with inline sync data has been replicated by XDCR, so also has an
363// existing HLV. Migration should preserve the existing HLV while moving doc._sync to sync xattr

Callers

nothing calls this directly

Calls 11

UnmarshalMethod · 0.95
TestUseXattrsFunction · 0.92
SetUpTestLoggingFunction · 0.92
setupTestDBFunction · 0.85
rawDocWithSyncMetaFunction · 0.85
GetDocWithXattrsMethod · 0.80
migrateMetadataMethod · 0.80
CloseMethod · 0.65
AddMethod · 0.45
UpdateMethod · 0.45

Tested by

no test coverage detected