Skip to content

Commit

Permalink
fix: recursive queries on non-tracking ORMs
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Nov 5, 2023
1 parent 708f41d commit c7920ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
9 changes: 5 additions & 4 deletions App/Modules/Cyan/Data/Repositories/PluginRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,13 @@ public async Task<Result<IEnumerable<PluginVersionPrincipal>>> GetAllVersion(IEn

predicate = pluginRefs.Aggregate(predicate, (c, r) =>
r.Version != null
? c.Or(x => x.Version == r.Version && x.Plugin.Name == r.Name && x.Plugin.User.Username == r.Username)
: c.Or(x => x.Plugin.Name == r.Name && x.Plugin.User.Username == r.Username &&
x.Version == x.Plugin.Versions.Max(p => p.Version))
? c.Or(x => x.Plugin.Name == r.Name && x.Plugin.User.Username == r.Username && x.Version == r.Version)
: c.Or(x => x.Plugin.Name == r.Name && x.Plugin.User.Username == r.Username)
);

query = query.Where(predicate);
query = query.Where(predicate)
.GroupBy(x => new { x.Plugin.Name, x.Plugin.User.Username })
.Select(g => g.OrderByDescending(o => o.Version).First());

var plugins = await query.Select(x => x.ToPrincipal()).ToArrayAsync();

Expand Down
13 changes: 6 additions & 7 deletions App/Modules/Cyan/Data/Repositories/ProcessorRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,25 +400,24 @@ public async Task<Result<IEnumerable<ProcessorVersionPrincipal>>> GetAllVersion(
var processorRefs = references as ProcessorVersionRef[] ?? references.ToArray();
try
{
this._logger.LogInformation("Getting all plugin versions {@Processors}", processorRefs.ToJson());
this._logger.LogInformation("Getting all processors versions {@Processors}", processorRefs.ToJson());
if (processorRefs.IsNullOrEmpty()) return Array.Empty<ProcessorVersionPrincipal>();
var query = this._db.ProcessorVersions
.Include(x => x.Processor)
.ThenInclude(x => x.User)
.Include(x => x.Processor)
.ThenInclude(x => x.Versions)
.AsQueryable();

var predicate = PredicateBuilder.New<ProcessorVersionData>(true);

predicate = processorRefs.Aggregate(predicate, (c, r) =>
r.Version != null
? c.Or(x => x.Version == r.Version && x.Processor.Name == r.Name && x.Processor.User.Username == r.Username)
: c.Or(x => x.Processor.Name == r.Name && x.Processor.User.Username == r.Username &&
x.Version == x.Processor.Versions.Max(p => p.Version))
? c.Or(x => x.Processor.Name == r.Name && x.Processor.User.Username == r.Username && x.Version == r.Version)
: c.Or(x => x.Processor.Name == r.Name && x.Processor.User.Username == r.Username)
);

query = query.Where(predicate);
query = query.Where(predicate)
.GroupBy(x => new { x.Processor.Name, x.Processor.User.Username })
.Select(g => g.OrderByDescending(o => o.Version).First());

var processors = await query.Select(x => x.ToPrincipal()).ToArrayAsync();
this._logger.LogInformation("Processor References: {@ProcessorReferences}", processors.Select(x => x.Id));
Expand Down

0 comments on commit c7920ae

Please sign in to comment.