@@ -1697,8 +1697,7 @@ func (db *DatabaseCollectionWithUser) PutExistingRevWithBody(ctx context.Context
16971697// SyncFnDryrun Runs the given document body through a sync function and returns expiry, channels doc was placed in,
16981698// access map for users, roles, handler errors and sync fn exceptions.
16991699// If syncFn is provided, it will be used instead of the one configured on the database.
1700- func (db * DatabaseCollectionWithUser ) SyncFnDryrun (ctx context.Context , newDoc , oldDoc * Document , syncFn string ) (* channels.ChannelMapperOutput , error ) {
1701-
1700+ func (db * DatabaseCollectionWithUser ) SyncFnDryrun (ctx context.Context , newDoc , oldDoc * Document , syncFn string , errorLogFunc , infoLogFunc func (string )) (* channels.ChannelMapperOutput , error ) {
17021701 mutableBody , metaMap , _ , err := db .prepareSyncFn (oldDoc , newDoc )
17031702 if err != nil {
17041703 base .InfofCtx (ctx , base .KeyDiagnostic , "Failed to prepare to run sync function: %v" , err )
@@ -1709,28 +1708,30 @@ func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, newDoc,
17091708 if err != nil {
17101709 return nil , err
17111710 }
1712- var output * channels. ChannelMapperOutput
1713- var syncErr error
1711+
1712+ // fetch configured sync function if one is not provided
17141713 if syncFn == "" {
1715- output , err = db .ChannelMapper .MapToChannelsAndAccess (ctx , mutableBody , string (oldDoc ._rawBody ), metaMap , syncOptions )
1716- if err != nil {
1717- return nil , & base.SyncFnDryRunError {Err : err }
1718- }
1719- } else {
1720- jsTimeout := time .Duration (base .DefaultJavascriptTimeoutSecs ) * time .Second
1721- syncRunner , err := channels .NewSyncRunner (ctx , syncFn , jsTimeout )
1722- if err != nil {
1723- return nil , fmt .Errorf ("failed to create sync runner: %v" , err )
1714+ if db .ChannelMapper != nil {
1715+ syncFn = db .ChannelMapper .Function ()
1716+ } else {
1717+ scopeAndCollectionName := db .ScopeAndCollectionName ()
1718+ syncFn = channels .GetDefaultSyncFunction (scopeAndCollectionName .Scope , scopeAndCollectionName .Collection )
17241719 }
1725- jsOutput , err := syncRunner .Call (ctx , mutableBody , string (oldDoc ._rawBody ), metaMap , syncOptions )
1726- if err != nil {
1720+ }
17271721
1728- return nil , & base.SyncFnDryRunError {Err : err }
1729- }
1730- output = jsOutput .(* channels.ChannelMapperOutput )
1722+ // create new sync runner instance for this dry run
1723+ jsTimeout := time .Duration (base .DefaultJavascriptTimeoutSecs ) * time .Second
1724+ syncRunner , err := channels .NewSyncRunnerWithLogging (ctx , syncFn , jsTimeout , errorLogFunc , infoLogFunc )
1725+ if err != nil {
1726+ return nil , fmt .Errorf ("failed to create sync runner: %v" , err )
1727+ }
1728+
1729+ jsOutput , err := syncRunner .Call (ctx , mutableBody , sgbucket .JSONString (oldDoc ._rawBody ), metaMap , syncOptions )
1730+ if err != nil {
1731+ return nil , & base.SyncFnDryRunError {Err : err }
17311732 }
17321733
1733- return output , syncErr
1734+ return jsOutput .( * channels. ChannelMapperOutput ), nil
17341735}
17351736
17361737// revTreeConflictCheck checks for conflicts in the rev tree history and returns the parent revid, currentRevIndex
0 commit comments