SyncFnDryRun Runs the given document body through a sync function and returns expiry, channels doc was placed in, access map for users, roles, handler errors and sync fn exceptions. If syncFn is provided, it will be used instead of the one configured on the database.
(ctx context.Context, newDoc, oldDoc *Document, userMeta, syncOptions map[string]any, syncFn string, errorLogFunc, infoLogFunc func(string))
| 1734 | // access map for users, roles, handler errors and sync fn exceptions. |
| 1735 | // If syncFn is provided, it will be used instead of the one configured on the database. |
| 1736 | func (db *DatabaseCollectionWithUser) SyncFnDryRun(ctx context.Context, newDoc, oldDoc *Document, userMeta, syncOptions map[string]any, syncFn string, errorLogFunc, infoLogFunc func(string)) (*channels.ChannelMapperOutput, error) { |
| 1737 | mutableBody, metaMap, _, err := db.prepareSyncFn(oldDoc, newDoc) |
| 1738 | if err != nil { |
| 1739 | base.InfofCtx(ctx, base.KeyDiagnostic, "Failed to prepare to run sync function: %v", err) |
| 1740 | return nil, err |
| 1741 | } |
| 1742 | |
| 1743 | if userMeta != nil { |
| 1744 | metaMap = userMeta |
| 1745 | } |
| 1746 | |
| 1747 | if syncOptions == nil { |
| 1748 | syncOptions, err = MakeUserCtx(db.user, db.ScopeName, db.Name) |
| 1749 | if err != nil { |
| 1750 | return nil, err |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | // fetch configured sync function if one is not provided |
| 1755 | if syncFn == "" { |
| 1756 | if db.ChannelMapper != nil { |
| 1757 | syncFn = db.ChannelMapper.Function() |
| 1758 | } else { |
| 1759 | scopeAndCollectionName := db.ScopeAndCollectionName() |
| 1760 | syncFn = channels.GetDefaultSyncFunction(scopeAndCollectionName.Scope, scopeAndCollectionName.Collection) |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | // create new sync runner instance for this dry run |
| 1765 | jsTimeout := time.Duration(base.DefaultJavascriptTimeoutSecs) * time.Second |
| 1766 | syncRunner, err := channels.NewSyncRunnerWithLogging(ctx, syncFn, jsTimeout, errorLogFunc, infoLogFunc) |
| 1767 | if err != nil { |
| 1768 | return nil, fmt.Errorf("failed to create sync runner: %v", err) |
| 1769 | } |
| 1770 | |
| 1771 | oldDocBodyBytes, err := oldDoc.BodyBytes(ctx) |
| 1772 | if err != nil { |
| 1773 | return nil, err |
| 1774 | } |
| 1775 | jsOutput, err := syncRunner.Call(ctx, mutableBody, sgbucket.JSONString(oldDocBodyBytes), metaMap, syncOptions) |
| 1776 | if err != nil { |
| 1777 | return nil, &base.SyncFnDryRunError{Err: err} |
| 1778 | } |
| 1779 | |
| 1780 | return jsOutput.(*channels.ChannelMapperOutput), nil |
| 1781 | } |
| 1782 | |
| 1783 | // revTreeConflictCheck checks for conflicts in the rev tree history and returns the parent revid, currentRevIndex |
| 1784 | // (index of parent rev), and an error if the document is in conflict |
no test coverage detected