Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error logging when exception occures during database seeding #1197

Merged
merged 2 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/AzureIoTHub.Portal.Infrastructure/PortalDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace AzureIoTHub.Portal.Infrastructure
using AzureIoTHub.Portal.Infrastructure.Seeds;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Logging;

public class PortalDbContext : DbContext
{
Expand All @@ -19,22 +20,5 @@ public PortalDbContext(DbContextOptions<PortalDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

try
{
var config = this.Database.GetService<ConfigHandler>();

_ = modelBuilder
.MigrateDeviceModelProperties(config);
}
catch (InvalidOperationException)
{

}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Infrastructure.Seeds
{
using Azure.Data.Tables;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Entities;
using AzureIoTHub.Portal.Models;
using AzureIoTHub.Portal.Infrastructure.Factories;
using Microsoft.EntityFrameworkCore;

internal static class DeviceModelPropertySeeder
{
public static async Task MigrateDeviceModelProperties(this PortalDbContext ctx, ConfigHandler config)
{
var table = new TableClientFactory(config.StorageAccountConnectionString)
.GetDeviceTemplateProperties();

var set = ctx.Set<DeviceModelProperty>();

foreach (var item in table.Query<TableEntity>().ToArray())
{
if (await set.AnyAsync(c => c.Id == item.RowKey))
continue;

#pragma warning disable CS8629 // Nullable value type may be null.
_ = await set.AddAsync(new DeviceModelProperty
{
Id = item.RowKey,
ModelId = item.PartitionKey,
Name = item.GetString(nameof(DeviceModelProperty.Name)),
DisplayName = item.GetString(nameof(DeviceModelProperty.DisplayName)),
PropertyType = Enum.Parse<DevicePropertyType>(item.GetString(nameof(DeviceModelProperty.PropertyType))),
Order = item.GetInt32(nameof(DeviceModelProperty.Order)) ?? 0,
IsWritable = item.GetBoolean(nameof(DeviceModelProperty.IsWritable)).Value
});
#pragma warning restore CS8629 // Nullable value type may be null.

if (config is ProductionConfigHandler)
{
_ = await table.DeleteEntityAsync(item.PartitionKey, item.RowKey);
}
}
Comment on lines +22 to +44

Check notice

Code scanning / CodeQL

Missed opportunity to use Where

This foreach loop implicitly filters its target sequence [here](1) - consider filtering the sequence explicitly using '.Where(...)'.
}
}
}

This file was deleted.

15 changes: 15 additions & 0 deletions src/AzureIoTHub.Portal/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace AzureIoTHub.Portal.Server
using AzureIoTHub.Portal.Infrastructure;
using AzureIoTHub.Portal.Infrastructure.Factories;
using AzureIoTHub.Portal.Infrastructure.Repositories;
using AzureIoTHub.Portal.Infrastructure.Seeds;
using Extensions;
using Hellang.Middleware.ProblemDetails;
using Hellang.Middleware.ProblemDetails.Mvc;
Expand All @@ -36,6 +37,7 @@ namespace AzureIoTHub.Portal.Server
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.OpenApi.Models;
using Models.v10;
Expand Down Expand Up @@ -439,8 +441,21 @@ private static async Task EnsureDatabaseCreatedAndUpToDate(IApplicationBuilder a
{
using var scope = app.ApplicationServices.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<PortalDbContext>();
var config = scope.ServiceProvider.GetRequiredService<ConfigHandler>();

await context.Database.MigrateAsync();

try
{
await context
.MigrateDeviceModelProperties(config);

_ = await context.SaveChangesAsync();
}
catch (InvalidOperationException e)
{
scope.ServiceProvider.GetRequiredService<ILogger>().LogError(e, "Failed to seed the database.");
}
}
}
}