Skip to content

Commit

Permalink
feat(seeding): add initial wallet seeding
Browse files Browse the repository at this point in the history
Refs: #108
  • Loading branch information
Phil91 committed Oct 8, 2024
1 parent 758b6ee commit 605c906
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 40 deletions.
57 changes: 57 additions & 0 deletions charts/dim/templates/configmap-seeding-initialdata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{{- /*
* Copyright (c) 2024 BMW Group AG
* Copyright 2024 SAP SE or an SAP affiliate company and ssi-dim-middle-layer contributors.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/}}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "dim.fullname" . }}-seeding-initialdata
namespace: {{ .Release.Namespace }}
data:
tenants.initial.json: |-
[
{
"id": "5c9a4f56-0609-49a5-ab86-dd8f93dfd3fa",
"company_name": "Catena-X",
"bpn": "{{ .Values.dim.bpn }}",
"did_document_location": "{{ .Values.dim.didDocumentLocationAddress }}",
"process_id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd1",
"is_issuer": true,
"operator_id": "{{ .Values.dim.operatorId }}"
}
]
processes.initial.json: |-
[
{
"id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd1",
"process_type_id" : 1,
"lock_expiry_date" : "2023-03-01 00:00:00.000000 +00:00",
"version" : "deadbeef-dead-beef-dead-beefdeadbeef"
}
]
process_steps.initial.json: |-
[
{
"id": "5c9a4f56-0609-49a5-ab86-dd8f93dfd3fa",
"process_step_type_id": 1,
"process_step_status_id": 1,
"process_id": "dd371565-9489-4907-a2e4-b8cbfe7a8cd1",
"date_created": "2023-02-21 08:15:20.479000 +00:00",
"date_last_changed": null
}
]
53 changes: 47 additions & 6 deletions charts/dim/templates/job-migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ spec:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
- ALL
readOnlyRootFilesystem: true
runAsNonRoot: true
image: "{{ .Values.migrations.image.name }}:{{ .Values.migrations.image.tag | default .Chart.AppVersion }}"
Expand All @@ -64,15 +64,56 @@ spec:
- name: "CONNECTIONSTRINGS__DIMDB"
value: "Server={{ .Values.externalDatabase.host }};Database={{ .Values.externalDatabase.database }};Port={{ .Values.externalDatabase.port }};User Id={{ .Values.externalDatabase.username }};Password=$(DIM_PASSWORD);Ssl Mode={{ .Values.dbConnection.sslMode }};"
{{- end }}
{{- if (.Values.migrations.seeding.useInitial) }}
- name: "SEEDING__DATAPATHS__0"
value: "Seeder/Data/import/initial-data"
- name: "SEEDING__TESTDATAENVIRONMENTS__0"
value: "{{ .Values.migrations.seeding.testDataEnvironments }}"
value: "initial"
{{- end }}
{{- if and (.Values.migrations.seeding.useOwnConfigMap.configMap) (.Values.migrations.seeding.useOwnConfigMap.filename) }}
{{- if (.Values.migrations.seeding.useInitial) }}
- name: "SEEDING__DATAPATHS__1"
value: "Seeder/Data/import/custom-data"
- name: "SEEDING__TESTDATAENVIRONMENTS__1"
value: "{{ .Values.migrations.seeding.useOwnConfigMap.filename }}"
{{ else }}
- name: "SEEDING__DATAPATHS__0"
value: "{{ .Values.migrations.seeding.testDataPaths }}"
value: "Seeder/Data/import/custom-data"
- name: "SEEDING__TESTDATAENVIRONMENTS__0"
value: "{{ .Values.migrations.seeding.useOwnConfigMap.filename }}"
{{- end }}
{{- end }}
- name: "SERILOG__MINIMUMLEVEL__Default"
value: "{{ .Values.migrations.logging.default }}"
ports:
- name: http
containerPort: {{ .Values.portContainer }}
protocol: TCP
- name: http
containerPort: {{ .Values.portContainer }}
protocol: TCP
resources:
{{- toYaml .Values.migrations.resources | nindent 10 }}
{{- if or (.Values.migrations.seeding.useInitial) (and (.Values.migrations.seeding.useOwnConfigMap.configMap) (.Values.migrations.seeding.useOwnConfigMap.filename)) }}
volumeMounts:
{{- if (.Values.migrations.seeding.useInitial) }}
- name: data-initial
mountPath: /migrations/Seeder/Data/import/initial-data
{{- end }}
{{- if and (.Values.migrations.seeding.useOwnConfigMap.configMap) (.Values.migrations.seeding.useOwnConfigMap.filename) }}
- name: data-custom
mountPath: /migrations/Seeder/Data/import/custom-data
{{- end }}
{{- end }}
{{- if or (.Values.migrations.seeding.useInitial) (and (.Values.migrations.seeding.useOwnConfigMap.configMap) (.Values.migrations.seeding.useOwnConfigMap.filename)) }}
volumes:
{{- if (.Values.migrations.seeding.useInitial) }}
- name: data-initial
configMap:
name: "{{ include "dim.fullname" . }}-seeding-initialdata"
optional: true
{{- end }}
{{- if and (.Values.migrations.seeding.useOwnConfigMap.configMap) (.Values.migrations.seeding.useOwnConfigMap.filename) }}
- name: data-custom
configMap:
name: "{{ .Values.migrations.seeding.useOwnConfigMap.configMap }}"
optional: true
{{- end }}
{{- end }}
9 changes: 7 additions & 2 deletions charts/dim/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ migrations:
cpu: 45m
memory: 200M
seeding:
testDataEnvironments: ""
testDataPaths: "Seeder/Data"
# -- If set to true the data configured in the config map 'configmap-seeding-initialdata.yaml' will be taken to insert the initial data
useInitial: true
useOwnConfigMap:
# -- ConfigMap containing json files for the tables to seed, e.g. processes.initial.json, process_steps.initial.json, etc.
configMap: ""
# -- Filename identifying the test data files e.g. for processes.initial.json the value would be "initial"
filename: ""
logging:
default: "Information"

Expand Down
4 changes: 3 additions & 1 deletion environments/helm-values/values-int.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ ingress:

dim:
swaggerEnabled: true
operatorId: "d6cd4e2e-1053-4ba6-888e-1cd56509958a"
operatorId: "d6cd4e2e-1053-4ba6-888e-1cd56509958a"
bpn: "BPNL00000003CRHK"
didDocumentLocationAddress: "https://portal-backend.int.catena-x.net/api/administration/staticdata/did/BPNL00000003CRHK/did.json"

migrations:
logging:
Expand Down
16 changes: 16 additions & 0 deletions src/database/Dim.Migrations/Dim.Migrations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Seeder\Data\processes.json">
<LinkBase>Seeder/Data/</LinkBase>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Seeder\Data\process_steps.json">
<LinkBase>Seeder/Data/</LinkBase>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Seeder\Data\technical_users.json">
<LinkBase>Seeder/Data/</LinkBase>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Seeder\Data\tenants.json">
<LinkBase>Seeder/Data/</LinkBase>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,15 @@
using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Seeding;

namespace Dim.DbAccess.Tests.Seeder;
namespace Dim.Migrations.Seeder;

/// <summary>
/// Seeder to seed the base entities (those with an id as primary key)
/// </summary>
public class BatchInsertSeeder : ICustomSeeder
public class BatchInsertSeeder(DimDbContext context, ILogger<BatchInsertSeeder> logger, IOptions<SeederSettings> options)
: ICustomSeeder
{
private readonly DimDbContext _context;
private readonly ILogger<BatchInsertSeeder> _logger;
private readonly SeederSettings _settings;

/// <summary>
/// Constructor
/// </summary>
/// <param name="context">The database context</param>
/// <param name="logger">The logger</param>
/// <param name="options">The options</param>
public BatchInsertSeeder(DimDbContext context, ILogger<BatchInsertSeeder> logger, IOptions<SeederSettings> options)
{
_context = context;
_logger = logger;
_settings = options.Value;
}
private readonly SeederSettings _settings = options.Value;

/// <inheritdoc />
public int Order => 1;
Expand All @@ -56,37 +42,36 @@ public async Task ExecuteAsync(CancellationToken cancellationToken)
{
if (!_settings.DataPaths.Any())
{
_logger.LogInformation("There a no data paths configured, therefore the {SeederName} will be skipped", nameof(BatchInsertSeeder));
logger.LogInformation("There a no data paths configured, therefore the {SeederName} will be skipped", nameof(BatchInsertSeeder));
return;
}

_logger.LogInformation("Start BaseEntityBatch Seeder");
logger.LogInformation("Start BaseEntityBatch Seeder");
await SeedTable<Tenant>("tenants", x => x.Id, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await SeedTable<ProcessStep>("process_steps", x => x.Id, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await SeedTable<Process>("processes", x => x.Id, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await SeedTable<TechnicalUser>("technical_users", x => x.Id, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);

await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
_logger.LogInformation("Finished BaseEntityBatch Seeder");
await context.SaveChangesAsync(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
logger.LogInformation("Finished BaseEntityBatch Seeder");
}

private async Task SeedTable<T>(string fileName, Func<T, object> keySelector, CancellationToken cancellationToken) where T : class
{
_logger.LogInformation("Start seeding {Filename}", fileName);
logger.LogInformation("Start seeding {Filename}", fileName);
var additionalEnvironments = _settings.TestDataEnvironments ?? Enumerable.Empty<string>();
var data = await SeederHelper.GetSeedData<T>(_logger, fileName, _settings.DataPaths, cancellationToken, additionalEnvironments.ToArray()).ConfigureAwait(ConfigureAwaitOptions.None);
_logger.LogInformation("Found {ElementCount} data", data.Count);
var data = await SeederHelper.GetSeedData<T>(logger, fileName, _settings.DataPaths, cancellationToken, additionalEnvironments.ToArray()).ConfigureAwait(ConfigureAwaitOptions.None);
logger.LogInformation("Found {ElementCount} data", data.Count);
if (data.Any())
{
var typeName = typeof(T).Name;
_logger.LogInformation("Started to Seed {TableName}", typeName);
data = data.GroupJoin(_context.Set<T>(), keySelector, keySelector, (d, dbEntry) => new { d, dbEntry })
logger.LogInformation("Started to Seed {TableName}", typeName);
data = data.GroupJoin(context.Set<T>(), keySelector, keySelector, (d, dbEntry) => new { d, dbEntry })
.SelectMany(t => t.dbEntry.DefaultIfEmpty(), (t, x) => new { t, x })
.Where(t => t.x == null)
.Select(t => t.t.d).ToList();
_logger.LogInformation("Seeding {DataCount} {TableName}", data.Count, typeName);
await _context.Set<T>().AddRangeAsync(data, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
_logger.LogInformation("Seeded {TableName}", typeName);
await context.Set<T>().AddRangeAsync(data, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
logger.LogInformation("Seeded {TableName}", typeName);
}
}
}
2 changes: 1 addition & 1 deletion tests/database/Dim.DbAccess.Tests/Setup/TestDbFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Dim.DbAccess.Tests.Seeder;
using Dim.Entities;
using Dim.Migrations.Migrations;
using Dim.Migrations.Seeder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down

0 comments on commit 605c906

Please sign in to comment.