From cb5b2fa53c4df6bb7595526c2ff0953ff60ceeb7 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 25 Jul 2019 21:06:46 +0200 Subject: [PATCH] Implemented versioning (#56) --- samples/Sample10/Program.cs | 3 +- samples/Sample12/Program.cs | 14 ++-- .../Handlers/SignalRequestHandler.cs | 2 +- .../Models/VersionOptions.cs | 30 ++++++++ .../Models/WorkflowDefinition.cs | 76 ++++++++++--------- .../Models/WorkflowInstance.cs | 1 + .../Persistence/IWorkflowDefinitionStore.cs | 22 +++--- .../Services/IWorkflowRegistry.cs | 2 +- .../Services/Models/Workflow.cs | 5 ++ .../Memory/MemoryWorkflowDefinitionStore.cs | 16 +++- .../WorkflowBuilders/WorkflowBuilder.cs | 9 ++- .../Elsa.Core/Services/WorkflowFactory.cs | 2 +- .../Elsa.Core/Services/WorkflowInvoker.cs | 4 +- .../Elsa.Core/Services/WorkflowRegistry.cs | 11 +-- .../Documents/WorkflowDefinitionDocument.cs | 2 + .../Documents/WorkflowInstanceDocument.cs | 1 + .../Indexes/WorkflowDefinitionIndex.cs | 13 ++-- .../Indexes/WorkflowInstanceIndex.cs | 9 +-- .../Services/YesSqlWorkflowDefinitionStore.cs | 15 +++- .../StartupTasks/StoreInitializationTask.cs | 8 +- 20 files changed, 162 insertions(+), 83 deletions(-) create mode 100644 src/core/Elsa.Abstractions/Models/VersionOptions.cs diff --git a/samples/Sample10/Program.cs b/samples/Sample10/Program.cs index e91cc06e6..584ec2a17 100644 --- a/samples/Sample10/Program.cs +++ b/samples/Sample10/Program.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using Elsa.Activities.Console.Extensions; using Elsa.Extensions; +using Elsa.Models; using Elsa.Persistence; using Elsa.Persistence.YesSql.Extensions; using Elsa.Persistence.YesSql.Options; @@ -31,7 +32,7 @@ namespace Sample10 await definitionStore.AddAsync(workflowDefinition); // Load the workflow definition. - workflowDefinition = await definitionStore.GetByIdAsync(workflowDefinition.Id); + workflowDefinition = await definitionStore.GetByIdAsync(workflowDefinition.Id, VersionOptions.Latest); // Execute the workflow. var invoker = services.GetRequiredService(); diff --git a/samples/Sample12/Program.cs b/samples/Sample12/Program.cs index b71bdee73..b7fc0328a 100644 --- a/samples/Sample12/Program.cs +++ b/samples/Sample12/Program.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Elsa.Activities.Console.Extensions; using Elsa.Activities.Primitives; +using Elsa.Activities.UserTask.Activities; using Elsa.Activities.UserTask.Extensions; using Elsa.Extensions; using Elsa.Models; @@ -10,6 +11,7 @@ using Elsa.Persistence.Memory; using Elsa.Runtime; using Elsa.Services; using Elsa.Services.Extensions; +using Elsa.Services.Models; using Microsoft.Extensions.DependencyInjection; namespace Sample12 @@ -27,8 +29,10 @@ namespace Sample12 var invoker = services.GetRequiredService(); // Invoke the workflow. - var executionContext = await invoker.InvokeAsync(workflowDefinition); - + var correlationId = Guid.NewGuid().ToString("N"); + await invoker.InvokeAsync(workflowDefinition, correlationId: correlationId); + WorkflowExecutionContext executionContext; + do { // Workflow is now halted on the user task activity. Ask user for input: @@ -36,9 +40,9 @@ namespace Sample12 var userAction = Console.ReadLine(); // Resume the workflow with the received stimulus. - var blockingActivities = executionContext.Workflow.BlockingActivities.Select(x => x.Id); - executionContext = await invoker.ResumeAsync(executionContext.Workflow.ToInstance(), new Variables { ["UserAction"] = userAction }, blockingActivities); - + var triggeredExecutionContexts = await invoker.TriggerAsync(nameof(UserTask), new Variables { ["UserAction"] = userAction}, correlationId); + executionContext = triggeredExecutionContexts.First(); + } while (executionContext.Workflow.IsHalted()); } diff --git a/src/activities/Elsa.Activities.Http/RequestHandlers/Handlers/SignalRequestHandler.cs b/src/activities/Elsa.Activities.Http/RequestHandlers/Handlers/SignalRequestHandler.cs index 8f70946bd..5160b9d1d 100644 --- a/src/activities/Elsa.Activities.Http/RequestHandlers/Handlers/SignalRequestHandler.cs +++ b/src/activities/Elsa.Activities.Http/RequestHandlers/Handlers/SignalRequestHandler.cs @@ -98,7 +98,7 @@ namespace Elsa.Activities.Http.RequestHandlers.Handlers ["Signal"] = signal.Name }; - var workflowDefinition = workflowRegistry.GetById(workflowInstance.DefinitionId); + var workflowDefinition = workflowRegistry.GetById(workflowInstance.DefinitionId, workflowInstance.Version); var workflow = workflowFactory.CreateWorkflow(workflowDefinition, input, workflowInstance); var blockingSignalActivities = workflow.BlockingActivities.ToList(); await workflowInvoker.ResumeAsync(workflow, blockingSignalActivities, cancellationToken); diff --git a/src/core/Elsa.Abstractions/Models/VersionOptions.cs b/src/core/Elsa.Abstractions/Models/VersionOptions.cs new file mode 100644 index 000000000..806723dc4 --- /dev/null +++ b/src/core/Elsa.Abstractions/Models/VersionOptions.cs @@ -0,0 +1,30 @@ +namespace Elsa.Models +{ + public struct VersionOptions + { + /// + /// Gets the latest version. + /// + public static readonly VersionOptions Latest = new VersionOptions { IsLatest = true }; + + /// + /// Gets the latest published version. + /// + public static readonly VersionOptions Published = new VersionOptions { IsPublished = true }; + + /// + /// Gets the latest draft version. + /// + public static readonly VersionOptions Draft = new VersionOptions { IsDraft = true }; + + /// + /// Gets a specific version. + /// + public static VersionOptions SpecificVersion(int version) => new VersionOptions { Version = version }; + + public bool IsLatest { get; private set; } + public bool IsPublished { get; private set; } + public bool IsDraft { get; private set; } + public int Version { get; private set; } + } +} \ No newline at end of file diff --git a/src/core/Elsa.Abstractions/Models/WorkflowDefinition.cs b/src/core/Elsa.Abstractions/Models/WorkflowDefinition.cs index 57230f891..e5a216438 100644 --- a/src/core/Elsa.Abstractions/Models/WorkflowDefinition.cs +++ b/src/core/Elsa.Abstractions/Models/WorkflowDefinition.cs @@ -1,37 +1,41 @@ -using System.Collections.Generic; -using System.Linq; - -namespace Elsa.Models -{ - public class WorkflowDefinition - { - public WorkflowDefinition() - { - } - - public WorkflowDefinition( - string id, - IEnumerable activities, - IEnumerable connections, - bool isSingleton, - Variables variables) : this(id) - { - Activities = activities.ToList(); - Connections = connections.ToList(); - IsSingleton = isSingleton; - Variables = variables; - } - - public WorkflowDefinition(string id) - { - Id = id; - Variables = new Variables(); - } - - public string Id { get; } - public ICollection Activities { get; set; } - public IList Connections { get; set; } - public Variables Variables { get; } - public bool IsSingleton { get; set; } - } +using System.Collections.Generic; +using System.Linq; + +namespace Elsa.Models +{ + public class WorkflowDefinition + { + public WorkflowDefinition() + { + } + + public WorkflowDefinition( + string id, + int version, + IEnumerable activities, + IEnumerable connections, + bool isSingleton, + Variables variables) : this(id) + { + Version = version; + Activities = activities.ToList(); + Connections = connections.ToList(); + IsSingleton = isSingleton; + Variables = variables; + } + + public WorkflowDefinition(string id) + { + Id = id; + Variables = new Variables(); + } + + public string Id { get; } + public int Version { get; } + public ICollection Activities { get; set; } + public IList Connections { get; set; } + public Variables Variables { get; } + public bool IsSingleton { get; set; } + public bool IsPublished { get; set; } + } } \ No newline at end of file diff --git a/src/core/Elsa.Abstractions/Models/WorkflowInstance.cs b/src/core/Elsa.Abstractions/Models/WorkflowInstance.cs index 16993e9bf..c5dfbe70d 100644 --- a/src/core/Elsa.Abstractions/Models/WorkflowInstance.cs +++ b/src/core/Elsa.Abstractions/Models/WorkflowInstance.cs @@ -7,6 +7,7 @@ namespace Elsa.Models { public string Id { get; set; } public string DefinitionId { get; set; } + public int Version { get; set; } public WorkflowStatus Status { get; set; } public string CorrelationId { get; set; } public Instant CreatedAt { get; set; } diff --git a/src/core/Elsa.Abstractions/Persistence/IWorkflowDefinitionStore.cs b/src/core/Elsa.Abstractions/Persistence/IWorkflowDefinitionStore.cs index 21d1a2c21..207450a4e 100644 --- a/src/core/Elsa.Abstractions/Persistence/IWorkflowDefinitionStore.cs +++ b/src/core/Elsa.Abstractions/Persistence/IWorkflowDefinitionStore.cs @@ -1,12 +1,12 @@ -using System.Threading; -using System.Threading.Tasks; -using Elsa.Models; - -namespace Elsa.Persistence -{ - public interface IWorkflowDefinitionStore - { - Task AddAsync(WorkflowDefinition definition, CancellationToken cancellationToken = default); - Task GetByIdAsync(string id, CancellationToken cancellationToken = default); - } +using System.Threading; +using System.Threading.Tasks; +using Elsa.Models; + +namespace Elsa.Persistence +{ + public interface IWorkflowDefinitionStore + { + Task AddAsync(WorkflowDefinition definition, CancellationToken cancellationToken = default); + Task GetByIdAsync(string id, VersionOptions version, CancellationToken cancellationToken = default); + } } \ No newline at end of file diff --git a/src/core/Elsa.Abstractions/Services/IWorkflowRegistry.cs b/src/core/Elsa.Abstractions/Services/IWorkflowRegistry.cs index 4425aaf87..3b561ba80 100644 --- a/src/core/Elsa.Abstractions/Services/IWorkflowRegistry.cs +++ b/src/core/Elsa.Abstractions/Services/IWorkflowRegistry.cs @@ -9,6 +9,6 @@ namespace Elsa.Services void RegisterWorkflow(WorkflowDefinition definition); WorkflowDefinition RegisterWorkflow() where T:IWorkflow, new(); IEnumerable<(WorkflowDefinition, ActivityDefinition)> ListByStartActivity(string activityType); - WorkflowDefinition GetById(string id); + WorkflowDefinition GetById(string id, int version); } } \ No newline at end of file diff --git a/src/core/Elsa.Abstractions/Services/Models/Workflow.cs b/src/core/Elsa.Abstractions/Services/Models/Workflow.cs index 8fa605896..d0533140a 100644 --- a/src/core/Elsa.Abstractions/Services/Models/Workflow.cs +++ b/src/core/Elsa.Abstractions/Services/Models/Workflow.cs @@ -13,6 +13,7 @@ namespace Elsa.Services.Models public Workflow( string id, string definitionId, + int version, IEnumerable activities, IEnumerable connections, Variables input = default, @@ -20,6 +21,8 @@ namespace Elsa.Services.Models { Id = id; DefinitionId = definitionId; + Version = version; + CorrelationId = correlationId; Activities = activities.ToList(); Connections = connections.ToList(); Input = new Variables(input ?? Variables.Empty); @@ -34,6 +37,7 @@ namespace Elsa.Services.Models public string Id { get; set; } public string DefinitionId { get; } + public int Version { get; } public string CorrelationId { get; set; } public WorkflowStatus Status { get; set; } public Instant CreatedAt { get; set; } @@ -56,6 +60,7 @@ namespace Elsa.Services.Models { Id = Id, DefinitionId = DefinitionId, + Version = Version, CorrelationId = CorrelationId, Status = Status, CreatedAt = CreatedAt, diff --git a/src/core/Elsa.Core/Persistence/Memory/MemoryWorkflowDefinitionStore.cs b/src/core/Elsa.Core/Persistence/Memory/MemoryWorkflowDefinitionStore.cs index 99bec41c1..d650d1af3 100644 --- a/src/core/Elsa.Core/Persistence/Memory/MemoryWorkflowDefinitionStore.cs +++ b/src/core/Elsa.Core/Persistence/Memory/MemoryWorkflowDefinitionStore.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Elsa.Models; @@ -20,9 +21,20 @@ namespace Elsa.Persistence.Memory return Task.CompletedTask; } - public Task GetByIdAsync(string id, CancellationToken cancellationToken = default) + public Task GetByIdAsync(string id, VersionOptions version, CancellationToken cancellationToken = default) { - var definition = definitions.ContainsKey(id) ? definitions[id] : default; + var query = definitions.Values.Where(x => x.Id == id).AsQueryable(); + + if (version.IsDraft) + query = query.Where(x => !x.IsPublished).OrderByDescending(x => x.Version); + else if(version.IsLatest) + query = query.OrderByDescending(x => x.Version); + else if(version.IsPublished) + query = query.Where(x => x.IsPublished).OrderByDescending(x => x.Version); + else if(version.Version > 0) + query = query.Where(x => x.Version == version.Version); + + var definition = query.FirstOrDefault(); return Task.FromResult(definition); } } diff --git a/src/core/Elsa.Core/Services/WorkflowBuilders/WorkflowBuilder.cs b/src/core/Elsa.Core/Services/WorkflowBuilders/WorkflowBuilder.cs index 980eba32d..76e6e77c2 100644 --- a/src/core/Elsa.Core/Services/WorkflowBuilders/WorkflowBuilder.cs +++ b/src/core/Elsa.Core/Services/WorkflowBuilders/WorkflowBuilder.cs @@ -18,6 +18,7 @@ namespace Elsa.Services.WorkflowBuilders } public string Id { get; set; } + public int Version { get; set; } = 1; public bool IsSingleton { get; set; } public IReadOnlyList Activities => activityBuilders.ToList().AsReadOnly(); @@ -26,6 +27,12 @@ namespace Elsa.Services.WorkflowBuilders Id = id; return this; } + + public IWorkflowBuilder WithVersion(int version) + { + Version = version; + return this; + } public IWorkflowBuilder AsSingleton(bool value) { @@ -78,7 +85,7 @@ namespace Elsa.Services.WorkflowBuilders var activities = activityBuilders.Select(x => x.BuildActivity()).ToList(); var connections = connectionBuilders.Select(x => x.BuildConnection()).ToList(); - return new WorkflowDefinition(Id, activities, connections, IsSingleton, Variables.Empty); + return new WorkflowDefinition(Id, Version, activities, connections, IsSingleton, Variables.Empty); } } } \ No newline at end of file diff --git a/src/core/Elsa.Core/Services/WorkflowFactory.cs b/src/core/Elsa.Core/Services/WorkflowFactory.cs index 2e53b9fcc..43a94998e 100644 --- a/src/core/Elsa.Core/Services/WorkflowFactory.cs +++ b/src/core/Elsa.Core/Services/WorkflowFactory.cs @@ -32,7 +32,7 @@ namespace Elsa.Services var activities = CreateActivities(definition.Activities).ToList(); var connections = CreateConnections(definition.Connections, activities); var id = idGenerator.Generate(); - var workflow = new Workflow(id, definition.Id, activities, connections, input); + var workflow = new Workflow(id, definition.Id, definition.Version, activities, connections, input); if(workflowInstance != null) workflow.Initialize(workflowInstance); diff --git a/src/core/Elsa.Core/Services/WorkflowInvoker.cs b/src/core/Elsa.Core/Services/WorkflowInvoker.cs index b1a257ada..f189b2d7a 100644 --- a/src/core/Elsa.Core/Services/WorkflowInvoker.cs +++ b/src/core/Elsa.Core/Services/WorkflowInvoker.cs @@ -90,7 +90,7 @@ namespace Elsa.Services IEnumerable startActivityIds = default, CancellationToken cancellationToken = default) { - var definition = workflowRegistry.GetById(workflowInstance.DefinitionId); + var definition = workflowRegistry.GetById(workflowInstance.DefinitionId, workflowInstance.Version); return InvokeAsync(definition, input, workflowInstance, startActivityIds, workflowInstance.CorrelationId, cancellationToken); } @@ -149,7 +149,7 @@ namespace Elsa.Services foreach (var (workflowInstance, startActivityInstance) in workflowInstances) { - var workflowDefinition = workflowRegistry.GetById(workflowInstance.DefinitionId); + var workflowDefinition = workflowRegistry.GetById(workflowInstance.DefinitionId, workflowInstance.Version); workflowInstance.Status = WorkflowStatus.Resuming; diff --git a/src/core/Elsa.Core/Services/WorkflowRegistry.cs b/src/core/Elsa.Core/Services/WorkflowRegistry.cs index 790f50713..400f3f845 100644 --- a/src/core/Elsa.Core/Services/WorkflowRegistry.cs +++ b/src/core/Elsa.Core/Services/WorkflowRegistry.cs @@ -11,17 +11,17 @@ namespace Elsa.Services public class WorkflowRegistry : IWorkflowRegistry { private readonly Func workflowBuilderFactory; - private readonly IDictionary workflowDefinitions; + private readonly IDictionary<(string, int), WorkflowDefinition> workflowDefinitions; public WorkflowRegistry(Func workflowBuilderFactory) { this.workflowBuilderFactory = workflowBuilderFactory; - workflowDefinitions = new Dictionary(); + workflowDefinitions = new Dictionary<(string, int), WorkflowDefinition>(); } public void RegisterWorkflow(WorkflowDefinition definition) { - workflowDefinitions[definition.Id] = definition; + workflowDefinitions[(definition.Id, definition.Version)] = definition; } public WorkflowDefinition RegisterWorkflow() where T : IWorkflow, new() @@ -42,9 +42,10 @@ namespace Elsa.Services return query.Distinct(); } - public WorkflowDefinition GetById(string id) + public WorkflowDefinition GetById(string id, int version) { - return workflowDefinitions.ContainsKey(id) ? workflowDefinitions[id] : default; + var identifier = (id, version); + return workflowDefinitions.ContainsKey(identifier) ? workflowDefinitions[identifier] : default; } } } \ No newline at end of file diff --git a/src/persistence/Elsa.Persistence.YesSql/Documents/WorkflowDefinitionDocument.cs b/src/persistence/Elsa.Persistence.YesSql/Documents/WorkflowDefinitionDocument.cs index 56046b0e3..1c2a8dba8 100644 --- a/src/persistence/Elsa.Persistence.YesSql/Documents/WorkflowDefinitionDocument.cs +++ b/src/persistence/Elsa.Persistence.YesSql/Documents/WorkflowDefinitionDocument.cs @@ -6,9 +6,11 @@ namespace Elsa.Persistence.YesSql.Documents public class WorkflowDefinitionDocument : YesSqlDocument { public string WorkflowDefinitionId { get; set; } + public int Version { get; set; } public ICollection Activities { get; set; } public IList Connections { get; set; } public Variables Variables { get; set; } public bool IsSingleton { get; set; } + public bool IsPublished { get; set; } } } \ No newline at end of file diff --git a/src/persistence/Elsa.Persistence.YesSql/Documents/WorkflowInstanceDocument.cs b/src/persistence/Elsa.Persistence.YesSql/Documents/WorkflowInstanceDocument.cs index 650562a36..08ad88d75 100644 --- a/src/persistence/Elsa.Persistence.YesSql/Documents/WorkflowInstanceDocument.cs +++ b/src/persistence/Elsa.Persistence.YesSql/Documents/WorkflowInstanceDocument.cs @@ -9,6 +9,7 @@ namespace Elsa.Persistence.YesSql.Documents public int Id { get; set; } public string WorkflowInstanceId { get; set; } public string DefinitionId { get; set; } + public int Version { get; set; } public WorkflowStatus Status { get; set; } public string CorrelationId { get; set; } public Instant CreatedAt { get; set; } diff --git a/src/persistence/Elsa.Persistence.YesSql/Indexes/WorkflowDefinitionIndex.cs b/src/persistence/Elsa.Persistence.YesSql/Indexes/WorkflowDefinitionIndex.cs index 429e765ad..fe87d64c2 100644 --- a/src/persistence/Elsa.Persistence.YesSql/Indexes/WorkflowDefinitionIndex.cs +++ b/src/persistence/Elsa.Persistence.YesSql/Indexes/WorkflowDefinitionIndex.cs @@ -9,9 +9,11 @@ namespace Elsa.Persistence.YesSql.Indexes public class WorkflowDefinitionIndex : MapIndex { public string WorkflowDefinitionId { get; set; } + public int Version { get; set; } + public bool IsPublished { get; set; } } - public class WorkflowDefinitionStartActivitiesIndex : WorkflowDefinitionIndex + public class WorkflowDefinitionStartActivitiesIndex : MapIndex { public string StartActivityId { get; set; } public string StartActivityType { get; set; } @@ -23,19 +25,20 @@ namespace Elsa.Persistence.YesSql.Indexes { context.For() .Map( - workflowDefinition => new WorkflowDefinitionIndex + document => new WorkflowDefinitionIndex { - WorkflowDefinitionId = workflowDefinition.WorkflowDefinitionId + WorkflowDefinitionId = document.WorkflowDefinitionId, + Version = document.Version, + IsPublished = document.IsPublished } ); context.For() .Map( - workflowDefinition => GetStartActivities(workflowDefinition) + document => GetStartActivities(document) .Select( activity => new WorkflowDefinitionStartActivitiesIndex { - WorkflowDefinitionId = workflowDefinition.WorkflowDefinitionId, StartActivityId = activity.Id, StartActivityType = activity.Type } diff --git a/src/persistence/Elsa.Persistence.YesSql/Indexes/WorkflowInstanceIndex.cs b/src/persistence/Elsa.Persistence.YesSql/Indexes/WorkflowInstanceIndex.cs index 33ac86134..e03fc5e47 100644 --- a/src/persistence/Elsa.Persistence.YesSql/Indexes/WorkflowInstanceIndex.cs +++ b/src/persistence/Elsa.Persistence.YesSql/Indexes/WorkflowInstanceIndex.cs @@ -12,10 +12,11 @@ namespace Elsa.Persistence.YesSql.Indexes public WorkflowStatus WorkflowStatus { get; set; } } - public class WorkflowInstanceBlockingActivitiesIndex : WorkflowInstanceIndex + public class WorkflowInstanceBlockingActivitiesIndex : MapIndex { public string ActivityId { get; set; } public string ActivityType { get; set; } + public string CorrelationId { get; set; } } public class WorkflowInstanceIndexProvider : IndexProvider @@ -38,11 +39,9 @@ namespace Elsa.Persistence.YesSql.Indexes .Select( activity => new WorkflowInstanceBlockingActivitiesIndex { - WorkflowInstanceId = workflowInstance.Id, - WorkflowDefinitionId = workflowInstance.Id, - CorrelationId = workflowInstance.CorrelationId, ActivityId = activity.ActivityId, - ActivityType = activity.ActivityType + ActivityType = activity.ActivityType, + CorrelationId = workflowInstance.CorrelationId } ) ); diff --git a/src/persistence/Elsa.Persistence.YesSql/Services/YesSqlWorkflowDefinitionStore.cs b/src/persistence/Elsa.Persistence.YesSql/Services/YesSqlWorkflowDefinitionStore.cs index 211dadbea..cf89e86e1 100644 --- a/src/persistence/Elsa.Persistence.YesSql/Services/YesSqlWorkflowDefinitionStore.cs +++ b/src/persistence/Elsa.Persistence.YesSql/Services/YesSqlWorkflowDefinitionStore.cs @@ -31,11 +31,22 @@ namespace Elsa.Persistence.YesSql.Services return Task.CompletedTask; } - public async Task GetByIdAsync(string id, CancellationToken cancellationToken = default) + public async Task GetByIdAsync(string id, VersionOptions version, CancellationToken cancellationToken = default) { using (var session = sessionProvider.GetSession()) { - var document = await session.Query(x => x.WorkflowDefinitionId == id).FirstOrDefaultAsync(); + var query = session.Query(); + + if (version.IsDraft) + query = query.Where(x => !x.IsPublished).OrderByDescending(x => x.Version); + else if(version.IsLatest) + query = query.OrderByDescending(x => x.Version); + else if(version.IsPublished) + query = query.Where(x => x.IsPublished).OrderByDescending(x => x.Version); + else if(version.Version > 0) + query = query.Where(x => x.Version == version.Version); + + var document = await query.FirstOrDefaultAsync(); return mapper.Map(document); } diff --git a/src/persistence/Elsa.Persistence.YesSql/StartupTasks/StoreInitializationTask.cs b/src/persistence/Elsa.Persistence.YesSql/StartupTasks/StoreInitializationTask.cs index 055e43540..1a6a3c28a 100644 --- a/src/persistence/Elsa.Persistence.YesSql/StartupTasks/StoreInitializationTask.cs +++ b/src/persistence/Elsa.Persistence.YesSql/StartupTasks/StoreInitializationTask.cs @@ -33,9 +33,10 @@ namespace Elsa.Persistence.YesSql.StartupTasks new SchemaBuilder(store.Configuration, transaction, false) .CreateMapIndexTable(nameof(WorkflowDefinitionIndex), table => table .Column("WorkflowDefinitionId") + .Column("Version") + .Column("IsPublished") ) .CreateMapIndexTable(nameof(WorkflowDefinitionStartActivitiesIndex), table => table - .Column("WorkflowDefinitionId") .Column("StartActivityId") .Column("StartActivityType") ) @@ -46,12 +47,9 @@ namespace Elsa.Persistence.YesSql.StartupTasks .Column("WorkflowStatus") ) .CreateMapIndexTable(nameof(WorkflowInstanceBlockingActivitiesIndex), table => table - .Column("WorkflowInstanceId") - .Column("WorkflowDefinitionId") - .Column("CorrelationId") - .Column("WorkflowStatus") .Column("ActivityId") .Column("ActivityType") + .Column("CorrelationId") ); transaction.Commit();