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 migration of device model commands from st table to database #1209 #1210

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

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
@@ -0,0 +1,37 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#nullable disable

namespace AzureIoTHub.Portal.Infrastructure.Migrations
{
using Microsoft.EntityFrameworkCore.Migrations;

public partial class AddDeviceModelCommand : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
_ = migrationBuilder.CreateTable(
name: "DeviceModelCommands",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
Frame = table.Column<string>(type: "text", nullable: false),
Confirmed = table.Column<bool>(type: "boolean", nullable: false),
Port = table.Column<int>(type: "integer", nullable: false),
IsBuiltin = table.Column<bool>(type: "boolean", nullable: false),
DeviceModelId = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
_ = table.PrimaryKey("PK_DeviceModelCommands", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
_ = migrationBuilder.DropTable(
name: "DeviceModelCommands");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ protected override void BuildModel(ModelBuilder modelBuilder)

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("AzureIoTHub.Portal.Domain.Entities.DeviceModelCommand", b =>
{
b.Property<string>("Id")
.HasColumnType("text");

b.Property<bool>("Confirmed")
.HasColumnType("boolean");

b.Property<string>("DeviceModelId")
.IsRequired()
.HasColumnType("text");

b.Property<string>("Frame")
.IsRequired()
.HasColumnType("text");

b.Property<bool>("IsBuiltin")
.HasColumnType("boolean");

b.Property<int>("Port")
.HasColumnType("integer");

b.HasKey("Id");

b.ToTable("DeviceModelCommands");
});

modelBuilder.Entity("AzureIoTHub.Portal.Domain.Entities.DeviceModelProperty", b =>
{
b.Property<string>("Id")
Expand Down
1 change: 1 addition & 0 deletions src/AzureIoTHub.Portal.Infrastructure/PortalDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace AzureIoTHub.Portal.Infrastructure
public class PortalDbContext : DbContext
{
public DbSet<DeviceModelProperty> DeviceModelProperties { get; set; }
public DbSet<DeviceModelCommand> DeviceModelCommands { get; set; }

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public PortalDbContext(DbContextOptions<PortalDbContext> options)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 System.Globalization;
using Azure.Data.Tables;
using Domain;
using Domain.Entities;
using Factories;
using Microsoft.EntityFrameworkCore;

internal static class DeviceModelCommandSeeder
{
public static async Task MigrateDeviceModelCommands(this PortalDbContext ctx, ConfigHandler config)
{
var table = new TableClientFactory(config.StorageAccountConnectionString)
.GetDeviceCommands();

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

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 DeviceModelCommand
{
Id = item.RowKey,
Frame = item[nameof(DeviceModelCommand.Frame)].ToString(),
Port = int.Parse(item[nameof(DeviceModelCommand.Port)].ToString(), CultureInfo.InvariantCulture),
IsBuiltin = bool.Parse(item[nameof(DeviceModelCommand.IsBuiltin)]?.ToString() ?? "false"),
Confirmed = bool.Parse(item[nameof(DeviceModelCommand.Confirmed)]?.ToString() ?? "false"),
DeviceModelId = item.PartitionKey
});
#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 +43

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(...)'.
}
}
}
3 changes: 3 additions & 0 deletions src/AzureIoTHub.Portal/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ private static async Task EnsureDatabaseCreatedAndUpToDate(IApplicationBuilder a
await context
.MigrateDeviceModelProperties(config);

await context
.MigrateDeviceModelCommands(config);

_ = await context.SaveChangesAsync();
}
catch (InvalidOperationException e)
Expand Down
23 changes: 23 additions & 0 deletions src/AzureIoTHubPortal.Domain/Entities/DeviceModelCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.Domain.Entities
{
using Base;
using System.ComponentModel.DataAnnotations.Schema;

public class DeviceModelCommand : EntityBase
{
[NotMapped] public string Name => Id;

public string Frame { get; set; }

public bool Confirmed { get; set; }

public int Port { get; set; } = 1;

public bool IsBuiltin { get; set; }

public string DeviceModelId { get; set; }
}
}