Skip to content

Commit

Permalink
Add error logging when exception occures during database seeding (#1197)
Browse files Browse the repository at this point in the history
* Add error logging when exception occures during database seeding

* Fix #1190 - Add seeding during Startup manually
  • Loading branch information
kbeaugrand authored Sep 10, 2022
1 parent 0c0dfaf commit b573f21
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 62 deletions.
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);
}
}
}
}
}

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.");
}
}
}
}

0 comments on commit b573f21

Please sign in to comment.