Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion db/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -1711,12 +1711,16 @@ func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, newDoc,
}
var output *channels.ChannelMapperOutput
var syncErr error
if syncFn == "" {
if syncFn == "" && db.ChannelMapper != nil {
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

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

The logic flow has become less clear with the nested conditions. The outer condition checks syncFn == \"\" twice in different branches. Consider restructuring to: first check if syncFn is empty and set it to the default if needed, then proceed with a single conditional based on whether to use ChannelMapper or SyncRunner.

Copilot uses AI. Check for mistakes.
output, err = db.ChannelMapper.MapToChannelsAndAccess(ctx, mutableBody, string(oldDoc._rawBody), metaMap, syncOptions)
if err != nil {
return nil, &base.SyncFnDryRunError{Err: err}
}
} else {
if syncFn == "" {
scopeAndCollectionName := db.ScopeAndCollectionName()
syncFn = channels.GetDefaultSyncFunction(scopeAndCollectionName.Scope, scopeAndCollectionName.Collection)
}
jsTimeout := time.Duration(base.DefaultJavascriptTimeoutSecs) * time.Second
syncRunner, err := channels.NewSyncRunner(ctx, syncFn, jsTimeout)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions rest/diagnostic_doc_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,34 @@
expectedOutput: SyncFnDryRun{},
expectedStatus: http.StatusBadRequest,
},
// Since the tests run in named Scopes and Collections therefore the
// default sync function is:
// function(doc){channel("<collection_name>");}
// therefore the channels returned will be named collections
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

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

The phrase 'therefore the' appears twice in succession. The comment should be restructured for better readability. Consider: 'Since the tests run in named Scopes and Collections, the default sync function is: function(doc){channel("<collection_name>");}. Therefore, the channels returned will be named collections.'

Suggested change
// Since the tests run in named Scopes and Collections therefore the
// default sync function is:
// function(doc){channel("<collection_name>");}
// therefore the channels returned will be named collections
// Since the tests run in named Scopes and Collections, the default sync function is:
// function(doc){channel("<collection_name>");}. Therefore, the channels returned will be named collections.

Copilot uses AI. Check for mistakes.
{
name: "no_custom_sync_func-default_db_sync_func-doc_body-no_existing_doc-no_doc_id",
document: map[string]any{"channels": "chanNew"},
existingDoc: false,
expectedOutput: SyncFnDryRun{
Channels: base.SetFromArray([]string{"sg_test_0"}),
Access: channels.AccessMap{},
Roles: channels.AccessMap{},
},
expectedStatus: http.StatusOK,
},
{
name: "no_custom_sync_func-default_db_sync_func-no_doc_body-existing_doc-doc_id",
docID: "doc22",
existingDoc: true,
existingDocID: "doc22",
existingDocBody: `{"channels": "chanNew"}`,
expectedOutput: SyncFnDryRun{
Channels: base.SetFromArray([]string{"sg_test_0"}),
Access: channels.AccessMap{},
Roles: channels.AccessMap{},
},
expectedStatus: http.StatusOK,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -1248,7 +1276,7 @@
var output SyncFnDryRun
err := json.Unmarshal(resp.Body.Bytes(), &output)
assert.NoError(t, err)
assert.Equal(t, test.expectedOutput, output)

Check failure on line 1279 in rest/diagnostic_doc_api_test.go

View workflow job for this annotation

GitHub Actions / test-default-collection

diagnostic_doc_api_test.go:1279: Error Trace: /home/runner/work/sync_gateway/sync_gateway/rest/diagnostic_doc_api_test.go:1279 Error: Not equal: expected: rest.SyncFnDryRun{Channels:base.Set{"sg_test_0":base.present{}}, Access:channels.AccessMap{}, Roles:channels.AccessMap{}, Exception:"", Expiry:(*uint32)(nil)} actual : rest.SyncFnDryRun{Channels:base.Set{"chanNew":base.present{}}, Access:channels.AccessMap{}, Roles:channels.AccessMap{}, Exception:"", Expiry:(*uint32)(nil)} Diff: --- Expected +++ Actual @@ -2,3 +2,3 @@ Channels: (base.Set) (len=1) { - (string) (len=9) "sg_test_0": (base.present) { + (string) (len=7) "chanNew": (base.present) { } Test: TestSyncFuncDryRun/no_custom_sync_func-default_db_sync_func-no_doc_body-existing_doc-doc_id 2025-12-24T11:44:38.314Z

Check failure on line 1279 in rest/diagnostic_doc_api_test.go

View workflow job for this annotation

GitHub Actions / test-default-collection

diagnostic_doc_api_test.go:1279: Error Trace: /home/runner/work/sync_gateway/sync_gateway/rest/diagnostic_doc_api_test.go:1279 Error: Not equal: expected: rest.SyncFnDryRun{Channels:base.Set{"sg_test_0":base.present{}}, Access:channels.AccessMap{}, Roles:channels.AccessMap{}, Exception:"", Expiry:(*uint32)(nil)} actual : rest.SyncFnDryRun{Channels:base.Set{"chanNew":base.present{}}, Access:channels.AccessMap{}, Roles:channels.AccessMap{}, Exception:"", Expiry:(*uint32)(nil)} Diff: --- Expected +++ Actual @@ -2,3 +2,3 @@ Channels: (base.Set) (len=1) { - (string) (len=9) "sg_test_0": (base.present) { + (string) (len=7) "chanNew": (base.present) { } Test: TestSyncFuncDryRun/no_custom_sync_func-default_db_sync_func-doc_body-no_existing_doc-no_doc_id 2025-12-24T11:44:38.292Z

Check failure on line 1279 in rest/diagnostic_doc_api_test.go

View workflow job for this annotation

GitHub Actions / test-default-collection

diagnostic_doc_api_test.go:1279: Error Trace: /home/runner/work/sync_gateway/sync_gateway/rest/diagnostic_doc_api_test.go:1279 Error: Not equal: expected: rest.SyncFnDryRun{Channels:base.Set{"sg_test_0":base.present{}}, Access:channels.AccessMap{}, Roles:channels.AccessMap{}, Exception:"", Expiry:(*uint32)(nil)} actual : rest.SyncFnDryRun{Channels:base.Set{"chanNew":base.present{}}, Access:channels.AccessMap{}, Roles:channels.AccessMap{}, Exception:"", Expiry:(*uint32)(nil)} Diff: --- Expected +++ Actual @@ -2,3 +2,3 @@ Channels: (base.Set) (len=1) { - (string) (len=9) "sg_test_0": (base.present) { + (string) (len=7) "chanNew": (base.present) { } Test: TestSyncFuncDryRun/no_custom_sync_func-default_db_sync_func-no_doc_body-existing_doc-doc_id 2025-12-24T11:44:45.184Z

Check failure on line 1279 in rest/diagnostic_doc_api_test.go

View workflow job for this annotation

GitHub Actions / test-default-collection

diagnostic_doc_api_test.go:1279: Error Trace: /home/runner/work/sync_gateway/sync_gateway/rest/diagnostic_doc_api_test.go:1279 Error: Not equal: expected: rest.SyncFnDryRun{Channels:base.Set{"sg_test_0":base.present{}}, Access:channels.AccessMap{}, Roles:channels.AccessMap{}, Exception:"", Expiry:(*uint32)(nil)} actual : rest.SyncFnDryRun{Channels:base.Set{"chanNew":base.present{}}, Access:channels.AccessMap{}, Roles:channels.AccessMap{}, Exception:"", Expiry:(*uint32)(nil)} Diff: --- Expected +++ Actual @@ -2,3 +2,3 @@ Channels: (base.Set) (len=1) { - (string) (len=9) "sg_test_0": (base.present) { + (string) (len=7) "chanNew": (base.present) { } Test: TestSyncFuncDryRun/no_custom_sync_func-default_db_sync_func-doc_body-no_existing_doc-no_doc_id 2025-12-24T11:44:45.164Z
})
}
}
Loading