Skip to content

Commit

Permalink
RavenDB-22833 forbid creating subscription with both predicate and query
Browse files Browse the repository at this point in the history
  • Loading branch information
garayx committed Jan 13, 2025
1 parent c737f5c commit 9e1663d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
32 changes: 28 additions & 4 deletions src/Raven.Client/Documents/Subscriptions/DocumentSubscriptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,23 @@ public string Create<T>(SubscriptionCreationOptions<T> options, string database
/// </summary>
/// <typeparam name="T">Type of the collection to be processed by the subscription</typeparam>
/// <returns>Created subscription</returns>
public string Create<T>(Expression<Func<T, bool>> predicate = null,
public string Create<T>(
SubscriptionCreationOptions options = null,
string database = null)
{
return Create(CreateSubscriptionOptionsFromGeneric(_store.Conventions, options, predicate, null, includes: null), database);
return Create(CreateSubscriptionOptionsFromGeneric<T>(_store.Conventions, options, null, null, includes: null), database);
}

/// <summary>
/// Creates a data subscription in a database. The subscription will expose all documents that match the specified subscription options for a given type.
/// </summary>
/// <typeparam name="T">Type of the collection to be processed by the subscription</typeparam>
/// <returns>Created subscription</returns>
public string Create<T>(Expression<Func<T, bool>> predicate,
PredicateSubscriptionCreationOptions options = null,
string database = null)
{
return Create(CreateSubscriptionOptionsFromGeneric(_store.Conventions, options?.ToSubscriptionCreationOptions(), predicate, null, includes: null), database);
}

/// <summary>
Expand All @@ -86,12 +98,24 @@ public Task<string> CreateAsync<T>(
/// </summary>
/// <returns>Created subscription name.</returns>
public Task<string> CreateAsync<T>(
Expression<Func<T, bool>> predicate = null,
SubscriptionCreationOptions options = null,
string database = null,
CancellationToken token = default)
{
return CreateAsync(CreateSubscriptionOptionsFromGeneric(_store.Conventions, options, predicate, null, includes: null), database, token);
return CreateAsync(CreateSubscriptionOptionsFromGeneric<T>(_store.Conventions, options, null, null, includes: null), database, token);
}

/// <summary>
/// It creates a data subscription in a database. The subscription will expose all documents that match the specified subscription options for a given type.
/// </summary>
/// <returns>Created subscription name.</returns>
public Task<string> CreateAsync<T>(
Expression<Func<T, bool>> predicate,
PredicateSubscriptionCreationOptions options = null,
string database = null,
CancellationToken token = default)
{
return CreateAsync(CreateSubscriptionOptionsFromGeneric(_store.Conventions, options?.ToSubscriptionCreationOptions(), predicate, null, includes: null), database, token);
}

internal static SubscriptionCreationOptions CreateSubscriptionOptionsFromGeneric<T>(
Expand Down
40 changes: 36 additions & 4 deletions src/Raven.Client/Documents/Subscriptions/SubscriptionCriteria.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Linq.Expressions;
using Raven.Client.Documents.Conventions;
using Raven.Client.Documents.DataArchival;
using Raven.Client.Documents.Indexes;
using Raven.Client.Documents.Operations.DataArchival;
using Raven.Client.Documents.Session.Loaders;

namespace Raven.Client.Documents.Subscriptions
Expand All @@ -15,7 +13,40 @@ internal sealed class SubscriptionTryout
public ArchivedDataProcessingBehavior? ArchivedDataProcessingBehavior { get; set; }
}

public class SubscriptionCreationOptions
internal interface ISubscriptionCreationOptions
{
public string Name { get; set; }
public string ChangeVector { get; set; }
public string MentorNode { get; set; }
public bool Disabled { get; set; }
public bool PinToMentorNode { get; set; }
public ArchivedDataProcessingBehavior? ArchivedDataProcessingBehavior { get; set; }
}

public sealed class PredicateSubscriptionCreationOptions : ISubscriptionCreationOptions
{
public string Name { get; set; }
public string ChangeVector { get; set; }
public string MentorNode { get; set; }
public bool Disabled { get; set; }
public bool PinToMentorNode { get; set; }
public ArchivedDataProcessingBehavior? ArchivedDataProcessingBehavior { get; set; }

internal SubscriptionCreationOptions ToSubscriptionCreationOptions()
{
return new SubscriptionCreationOptions
{
Name = Name,
ChangeVector = ChangeVector,
MentorNode = MentorNode,
PinToMentorNode = PinToMentorNode,
Disabled = Disabled,
ArchivedDataProcessingBehavior = ArchivedDataProcessingBehavior
};
}
}

public class SubscriptionCreationOptions : ISubscriptionCreationOptions
{
public string Name { get; set; }
public string Query { get; set; }
Expand Down Expand Up @@ -47,7 +78,8 @@ public SubscriptionCreationOptions ToSubscriptionCreationOptions(DocumentConvent
ChangeVector = ChangeVector,
MentorNode = MentorNode,
PinToMentorNode = PinToMentorNode,
Disabled = Disabled
Disabled = Disabled,
ArchivedDataProcessingBehavior = ArchivedDataProcessingBehavior
};
return DocumentSubscriptions.CreateSubscriptionOptionsFromGeneric(conventions,
subscriptionCreationOptions, Filter, Projection, Includes);
Expand Down

0 comments on commit 9e1663d

Please sign in to comment.