Skip to content

Commit

Permalink
feat(seeding): add delete seeder to remove changed entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil91 committed Apr 25, 2024
1 parent e0b1498 commit d1bb5e0
Show file tree
Hide file tree
Showing 20 changed files with 303 additions and 231 deletions.
18 changes: 14 additions & 4 deletions src/database/PolicyHub.DbAccess/Repositories/PolicyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public IAsyncEnumerable<string> GetAttributeKeys() =>
public IAsyncEnumerable<PolicyTypeResponse> GetPolicyTypes(PolicyTypeId? type, UseCaseId? useCase) =>
dbContext.Policies
.Where(p =>
(type == null || p.Types.Any(x => x.Id == type)) &&
(useCase == null || p.UseCases.Any(x => x.Id == useCase)))
p.IsActive &&
(type == null || p.Types.Any(x => x.Id == type && x.IsActive)) &&
(useCase == null || p.UseCases.Any(x => x.Id == useCase && x.IsActive)))
.Select(p => new PolicyTypeResponse(
p.TechnicalKey,
p.Types.Where(t => t.IsActive).Select(t => t.Id),
Expand All @@ -50,20 +51,29 @@ public IAsyncEnumerable<PolicyTypeResponse> GetPolicyTypes(PolicyTypeId? type, U
public Task<(bool Exists, string LeftOperand, (AttributeKeyId? Key, IEnumerable<string> Values) Attributes, string? RightOperandValue)> GetPolicyContentAsync(UseCaseId? useCase, PolicyTypeId type, string credential) =>
dbContext.Policies
.Where(p =>
p.IsActive &&
p.Types.Any(t => t.IsActive && t.Id == type) &&
(useCase == null || p.UseCases.Any(x => x.Id == useCase)) &&
(useCase == null || p.UseCases.Any(x => x.Id == useCase && x.IsActive)) &&
p.TechnicalKey == credential)
.Select(p => new ValueTuple<bool, string, ValueTuple<AttributeKeyId?, IEnumerable<string>>, string?>(
true,
p.LeftOperandValue ?? p.TechnicalKey,
new ValueTuple<AttributeKeyId?, IEnumerable<string>>(p.AttributeKeyId, p.AttributeKey!.PolicyAttributes.Where(pa => pa.IsActive && pa.PolicyId == p.Id && (useCase == null || pa.PolicyAttributeAssignedUseCases.Any(x => x.UseCaseId == useCase))).Select(a => a.AttributeValue)),
new ValueTuple<AttributeKeyId?, IEnumerable<string>>(
p.AttributeKeyId,
p.AttributeKey!.PolicyAttributes.Where(pa =>
pa.IsActive &&
pa.PolicyId == p.Id &&
pa.IsActive &&
(useCase == null || pa.PolicyAttributeAssignedUseCases.Any(x => x.UseCaseId == useCase && x.IsActive))
).Select(a => a.AttributeValue)),
p.PolicyKind!.Configuration!.RightOperandValue
))
.FirstOrDefaultAsync();

public IAsyncEnumerable<(string TechnicalKey, string LeftOperand, (AttributeKeyId? Key, IEnumerable<string> Values) Attributes, string? RightOperandValue)> GetPolicyForOperandContent(PolicyTypeId type, IEnumerable<string> technicalKeys) =>
dbContext.Policies
.Where(p =>
p.IsActive &&
p.Types.Any(t => t.IsActive && t.Id == type) &&
technicalKeys.Contains(p.TechnicalKey))
.Select(p => new ValueTuple<string, string, ValueTuple<AttributeKeyId?, IEnumerable<string>>, string?>(
Expand Down
6 changes: 6 additions & 0 deletions src/database/PolicyHub.Entities/Entities/IActiveEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public interface IActiveEntity
{
bool IsActive { get; set; }
}
2 changes: 1 addition & 1 deletion src/database/PolicyHub.Entities/Entities/Policy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public class Policy
public class Policy : IActiveEntity
{
private Policy()
{
Expand Down
9 changes: 5 additions & 4 deletions src/database/PolicyHub.Entities/Entities/PolicyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@

namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public class PolicyAttribute
public class PolicyAttribute : IActiveEntity
{
private PolicyAttribute()
{
AttributeValue = null!;
PolicyAttributeAssignedUseCases = new HashSet<PolicyAttributeAssignedUseCases>();
}

public PolicyAttribute(Guid id, Guid policyId, AttributeKeyId key, string attributeValue)
Expand All @@ -38,11 +39,11 @@ public PolicyAttribute(Guid id, Guid policyId, AttributeKeyId key, string attrib
}

public Guid Id { get; set; }
public Guid PolicyId { get; private set; }
public Guid PolicyId { get; set; }

public AttributeKeyId Key { get; private set; }
public AttributeKeyId Key { get; set; }

public string AttributeValue { get; private set; }
public string AttributeValue { get; set; }

public bool IsActive { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ private PolicyAttributeAssignedUseCases()
UseCase = null!;
}

public PolicyAttributeAssignedUseCases(Guid id, UseCaseId useCaseId)
public PolicyAttributeAssignedUseCases(Guid attributeId, UseCaseId useCaseId)
: this()
{
Id = id;
AttributeId = attributeId;
UseCaseId = useCaseId;
}

public Guid Id { get; set; }
public Guid AttributeId { get; set; }

public UseCaseId UseCaseId { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/database/PolicyHub.Entities/Entities/PolicyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public class PolicyType
public class PolicyType : IActiveEntity
{
private PolicyType()
{
Expand Down
6 changes: 4 additions & 2 deletions src/database/PolicyHub.Entities/Entities/UseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@

namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public class UseCase
public class UseCase : IActiveEntity
{
private UseCase()
{
Label = null!;
Policies = new HashSet<Policy>();
PolicyAttributeAssignedUseCases = new HashSet<PolicyAttributeAssignedUseCases>();
}

public UseCase(UseCaseId useCaseId, bool isActive) : this()
public UseCase(UseCaseId useCaseId, bool isActive)
: this()
{
Id = useCaseId;
Label = useCaseId.ToString();
Expand Down
10 changes: 3 additions & 7 deletions src/database/PolicyHub.Entities/PolicyHubContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<PolicyAttribute>(entity =>
{
entity.Property(x => x.IsActive).HasDefaultValue(true);
entity
.HasKey(x => new { x.Id });
entity
.HasKey(x => new { x.PolicyId, x.Key, x.AttributeValue });
entity.Property(x => x.IsActive).HasDefaultValue(true);
entity
.HasOne(pa => pa.AttributeKey)
Expand All @@ -135,13 +132,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.Property(x => x.IsActive).HasDefaultValue(true);
entity
.HasKey(x => new { x.Id, x.UseCaseId });
.HasKey(x => new { x.AttributeId, x.UseCaseId });
entity
.HasOne(pa => pa.PolicyAttribute)
.WithMany(p => p.PolicyAttributeAssignedUseCases)
.HasForeignKey(x => x.Id)
.HasPrincipalKey(x => x.Id);
.HasForeignKey(x => x.AttributeId);
entity
.HasOne(pa => pa.UseCase)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/********************************************************************************
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand All @@ -17,16 +17,15 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace Org.Eclipse.TractusX.PolicyHub.Migrations.Migrations
{
/// <inheritdoc />
public partial class feat25policyseeding : Migration
public partial class _25policyseeding : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
Expand All @@ -41,22 +40,21 @@ protected override void Up(MigrationBuilder migrationBuilder)
schema: "policy-hub",
table: "policy_attributes");

migrationBuilder.DeleteData(
migrationBuilder.DropPrimaryKey(
name: "pk_policy_attributes",
schema: "policy-hub",
table: "policy_types",
keyColumn: "id",
keyValue: 3);
table: "policy_attributes");

migrationBuilder.AddColumn<Guid>(
name: "id",
schema: "policy-hub",
table: "policy_attributes",
type: "uuid",
nullable: false,
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
defaultValueSql: "gen_random_uuid()");

migrationBuilder.AddUniqueConstraint(
name: "ak_policy_attributes_id",
migrationBuilder.AddPrimaryKey(
name: "pk_policy_attributes",
schema: "policy-hub",
table: "policy_attributes",
column: "id");
Expand All @@ -66,16 +64,16 @@ protected override void Up(MigrationBuilder migrationBuilder)
schema: "policy-hub",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
attribute_id = table.Column<Guid>(type: "uuid", nullable: false),
use_case_id = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true)
},
constraints: table =>
{
table.PrimaryKey("pk_policy_attribute_assigned_use_cases", x => new { x.id, x.use_case_id });
table.PrimaryKey("pk_policy_attribute_assigned_use_cases", x => new { x.attribute_id, x.use_case_id });
table.ForeignKey(
name: "fk_policy_attribute_assigned_use_cases_policy_attributes_id",
column: x => x.id,
name: "fk_policy_attribute_assigned_use_cases_policy_attributes_attri",
column: x => x.attribute_id,
principalSchema: "policy-hub",
principalTable: "policy_attributes",
principalColumn: "id",
Expand All @@ -89,6 +87,12 @@ protected override void Up(MigrationBuilder migrationBuilder)
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "ix_policy_attributes_policy_id",
schema: "policy-hub",
table: "policy_attributes",
column: "policy_id");

migrationBuilder.CreateIndex(
name: "ix_policy_attribute_assigned_use_cases_use_case_id",
schema: "policy-hub",
Expand Down Expand Up @@ -132,8 +136,13 @@ protected override void Down(MigrationBuilder migrationBuilder)
name: "policy_attribute_assigned_use_cases",
schema: "policy-hub");

migrationBuilder.DropUniqueConstraint(
name: "ak_policy_attributes_id",
migrationBuilder.DropPrimaryKey(
name: "pk_policy_attributes",
schema: "policy-hub",
table: "policy_attributes");

migrationBuilder.DropIndex(
name: "ix_policy_attributes_policy_id",
schema: "policy-hub",
table: "policy_attributes");

Expand All @@ -142,11 +151,11 @@ protected override void Down(MigrationBuilder migrationBuilder)
schema: "policy-hub",
table: "policy_attributes");

migrationBuilder.InsertData(
migrationBuilder.AddPrimaryKey(
name: "pk_policy_attributes",
schema: "policy-hub",
table: "policy_types",
columns: new[] { "id", "is_active", "label" },
values: new object[] { 3, true, "Purpose" });
table: "policy_attributes",
columns: new[] { "policy_id", "key", "attribute_value" });

migrationBuilder.AddForeignKey(
name: "fk_policies_policy_kinds_policy_kind_id",
Expand Down
Loading

0 comments on commit d1bb5e0

Please sign in to comment.