Browse Source

Fix for duplicate entries in app or schema collection.

pull/804/head
Sebastian 4 years ago
parent
commit
6faf87c4cd
  1. 6
      backend/src/Squidex.Domain.Apps.Entities.MongoDb/Apps/MongoAppEntity.cs
  2. 21
      backend/src/Squidex.Domain.Apps.Entities.MongoDb/Apps/MongoAppRepository.cs
  3. 6
      backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemaEntity.cs
  4. 19
      backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemaRepository.cs

6
backend/src/Squidex.Domain.Apps.Entities.MongoDb/Apps/MongoAppEntity.cs

@ -6,6 +6,7 @@
// ==========================================================================
using MongoDB.Bson.Serialization.Attributes;
using NodaTime;
using Squidex.Domain.Apps.Entities.Apps.DomainObject;
using Squidex.Infrastructure;
using Squidex.Infrastructure.States;
@ -26,6 +27,10 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Apps
[BsonElement("_dl")]
public bool IndexedDeleted { get; set; }
[BsonIgnoreIfDefault]
[BsonElement("_ct")]
public Instant IndexedCreated { get; set; }
public override void Prepare()
{
var users = new HashSet<string>
@ -37,6 +42,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Apps
users.AddRange(Document.Clients.Keys);
IndexedUserIds = users.ToArray();
IndexedCreated = Document.Created;
IndexedDeleted = Document.IsDeleted;
IndexedName = Document.Name;
}

21
backend/src/Squidex.Domain.Apps.Entities.MongoDb/Apps/MongoAppRepository.cs

@ -48,7 +48,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Apps
{
using (Telemetry.Activities.StartActivity("MongoAppRepository/QueryIdsAsync"))
{
var find = Collection.Find(x => x.IndexedUserIds.Contains(contributorId) && !x.IndexedDeleted);
var find = Collection.Find(x => x.IndexedUserIds.Contains(contributorId) && !x.IndexedDeleted).SortBy(x => x.IndexedCreated);
return await QueryAsync(find, ct);
}
@ -59,7 +59,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Apps
{
using (Telemetry.Activities.StartActivity("MongoAppRepository/QueryAsync"))
{
var find = Collection.Find(x => names.Contains(x.IndexedName) && !x.IndexedDeleted);
var find = Collection.Find(x => names.Contains(x.IndexedName) && !x.IndexedDeleted).SortBy(x => x.IndexedCreated);
return await QueryAsync(find, ct);
}
@ -70,13 +70,20 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Apps
{
var entities = await find.Only(x => x.DocumentId, x => x.IndexedName).ToListAsync(ct);
return entities.Select(x =>
var result = new Dictionary<string, DomainId>();
foreach (var entity in entities)
{
var indexedId = DomainId.Create(x["_id"].AsString);
var indexedName = x["_an"].AsString;
var indexedId = DomainId.Create(entity["_id"].AsString);
var indexedName = entity["_an"].AsString;
if (!result.ContainsKey(indexedName))
{
result.Add(indexedName, indexedId);
}
}
return new { indexedName, indexedId };
}).ToDictionary(x => x.indexedName, x => x.indexedId);
return result;
}
}
}

6
backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemaEntity.cs

@ -6,6 +6,7 @@
// ==========================================================================
using MongoDB.Bson.Serialization.Attributes;
using NodaTime;
using Squidex.Domain.Apps.Entities.Schemas.DomainObject;
using Squidex.Infrastructure;
using Squidex.Infrastructure.States;
@ -30,12 +31,17 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas
[BsonElement("_dl")]
public bool IndexedDeleted { get; set; }
[BsonIgnoreIfDefault]
[BsonElement("_ct")]
public Instant IndexedCreated { get; set; }
public override void Prepare()
{
IndexedAppId = Document.AppId.Id;
IndexedDeleted = Document.IsDeleted;
IndexedId = Document.Id;
IndexedName = Document.SchemaDef.Name;
IndexedCreated = Document.Created;
}
}
}

19
backend/src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemaRepository.cs

@ -46,7 +46,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas
{
using (Telemetry.Activities.StartActivity("MongoSchemaRepository/QueryAsync"))
{
var find = Collection.Find(x => x.IndexedAppId == appId && !x.IndexedDeleted);
var find = Collection.Find(x => x.IndexedAppId == appId && !x.IndexedDeleted).SortBy(x => x.IndexedCreated);
return await QueryAsync(find, ct);
}
@ -57,13 +57,20 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas
{
var entities = await find.Only(x => x.IndexedId, x => x.IndexedName).ToListAsync(ct);
return entities.Select(x =>
var result = new Dictionary<string, DomainId>();
foreach (var entity in entities)
{
var indexedId = DomainId.Create(x["_si"].AsString);
var indexedName = x["_sn"].AsString;
var indexedId = DomainId.Create(entity["_si"].AsString);
var indexedName = entity["_sn"].AsString;
if (!result.ContainsKey(indexedName))
{
result.Add(indexedName, indexedId);
}
}
return new { indexedName, indexedId };
}).ToDictionary(x => x.indexedName, x => x.indexedId);
return result;
}
}
}

Loading…
Cancel
Save