| | | 1 | | using Elsa.Persistence.EFCore.Modules.Management; |
| | | 2 | | using Elsa.Persistence.EFCore.PostgreSql.Helpers; |
| | | 3 | | using Elsa.Workflows.Management; |
| | | 4 | | using Elsa.Workflows.Management.Entities; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Persistence.EFCore.PostgreSql.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides an implementation of <see cref="IWorkflowReferenceQuery"/> for querying PostgreSQL databases |
| | | 11 | | /// to find all latest workflow definitions that reference a specific workflow definition. |
| | | 12 | | /// </summary> |
| | 0 | 13 | | public class PostgreSqlWorkflowReferenceQuery(IDbContextFactory<ManagementElsaDbContext> dbContextFactory) : IWorkflowRe |
| | | 14 | | { |
| | | 15 | | public async Task<IEnumerable<string>> ExecuteAsync(string workflowDefinitionId, CancellationToken cancellationToken |
| | | 16 | | { |
| | 0 | 17 | | await using var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
| | 0 | 18 | | var tableName = dbContext.Model.FindEntityType(typeof(WorkflowDefinition))!.QuoteSchemaQualifiedTableName(); |
| | | 19 | | |
| | 0 | 20 | | var sql = $""" |
| | 0 | 21 | | SELECT "DefinitionId", "TenantId" FROM {tableName} WHERE "IsLatest" = true AND EXISTS ( |
| | 0 | 22 | | SELECT 1 FROM jsonb_array_elements("StringData"::jsonb->'activities') AS value |
| | 0 | 23 | | WHERE value->>'workflowDefinitionId' = @p0 |
| | 0 | 24 | | AND value->'latestAvailablePublishedVersion' IS NOT NULL) |
| | 0 | 25 | | """; |
| | | 26 | | |
| | 0 | 27 | | return await dbContext.Set<WorkflowDefinition>() |
| | 0 | 28 | | .FromSqlRaw(sql, workflowDefinitionId) |
| | 0 | 29 | | .Select(x => x.DefinitionId) |
| | 0 | 30 | | .ToListAsync(cancellationToken); |
| | 0 | 31 | | } |
| | | 32 | | } |