Skip to content
This repository was archived by the owner on Apr 5, 2023. It is now read-only.

Commit 320b095

Browse files
author
Derek Trider
authored
Merge pull request #63 from DRK3/CouchDBStoreFilterOutDesignDocs
fix: CouchDBStore GetAll call returns design documents
2 parents 9d60bd3 + b42950f commit 320b095

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

pkg/storage/couchdb/couchdbstore.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import (
2626

2727
const (
2828
logModuleName = "edge-core-couchdbstore"
29+
30+
designDocumentFilteredOutLogMsg = "Getting all documents from a CouchDB store. " +
31+
"A document with id %s was filtered out since it's a CouchDB design document."
2932
)
3033

3134
var logger = log.New(logModuleName)
@@ -375,19 +378,23 @@ func (c *CouchDBStore) getAllKeyValuePairs(rows *kivik.Rows) (map[string][]byte,
375378
return nil, fmt.Errorf(failureWhileUnquotingKey, err)
376379
}
377380

378-
rawDoc := make(map[string]interface{})
381+
if strings.HasPrefix(key, "_design") {
382+
logger.Debugf(designDocumentFilteredOutLogMsg, key)
383+
} else {
384+
rawDoc := make(map[string]interface{})
379385

380-
err = rows.ScanDoc(&rawDoc)
381-
if err != nil {
382-
return nil, fmt.Errorf(failureWhileScanningResultRowsDoc, err)
383-
}
386+
err = rows.ScanDoc(&rawDoc)
387+
if err != nil {
388+
return nil, fmt.Errorf(failureWhileScanningResultRowsDoc, err)
389+
}
384390

385-
documentBytes, err := c.getStoredValueFromRawDoc(rawDoc, key)
386-
if err != nil {
387-
return nil, fmt.Errorf(failureWhileGettingStoredValueFromRawDoc, err)
388-
}
391+
documentBytes, err := c.getStoredValueFromRawDoc(rawDoc, key)
392+
if err != nil {
393+
return nil, fmt.Errorf(failureWhileGettingStoredValueFromRawDoc, err)
394+
}
389395

390-
allKeyValuePairs[key] = documentBytes
396+
allKeyValuePairs[key] = documentBytes
397+
}
391398
}
392399

393400
return allKeyValuePairs, nil

pkg/storage/couchdb/couchdbstore_test.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func (t *TestLogger) Panicf(msg string, _ ...interface{}) {
5858
t.logContents = msg
5959
}
6060

61-
func (t *TestLogger) Debugf(msg string, _ ...interface{}) {
62-
t.logContents = msg
61+
func (t *TestLogger) Debugf(msg string, args ...interface{}) {
62+
t.logContents = fmt.Sprintf(msg, args...)
6363
}
6464

6565
func (t *TestLogger) Infof(msg string, _ ...interface{}) {
@@ -277,7 +277,12 @@ func TestCouchDBStore_GetAll(t *testing.T) {
277277

278278
store := createAndOpenTestStore(t, provider)
279279

280-
err := store.Put(testDocKey, []byte(testJSONValue))
280+
// Creating an index will create a design document.
281+
// This test ensures that it gets filtered out of the results, as expected.
282+
err := createIndex(store, `{"fields": ["SomeField"]}`)
283+
require.NoError(t, err)
284+
285+
err = store.Put(testDocKey, []byte(testJSONValue))
281286
require.NoError(t, err)
282287

283288
err = store.Put(testDocKey2, []byte(testJSONValue2))
@@ -288,6 +293,9 @@ func TestCouchDBStore_GetAll(t *testing.T) {
288293
require.Equal(t, allValues[testDocKey], []byte(testJSONValue))
289294
require.Equal(t, allValues[testDocKey2], []byte(testJSONValue2))
290295
require.Len(t, allValues, 2)
296+
297+
require.Equal(t, fmt.Sprintf(designDocumentFilteredOutLogMsg, "_design/TestDesignDoc"),
298+
testLogger.logContents)
291299
})
292300
t.Run("Success, but no key-value pairs exist", func(t *testing.T) {
293301
provider := initializeTest(t)

0 commit comments

Comments
 (0)