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