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 tags from st table to database #1207 #1208

Merged
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,35 @@
// 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 AddDeviceTag : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
_ = migrationBuilder.CreateTable(
name: "DeviceTags",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
Label = table.Column<string>(type: "text", nullable: false),
Required = table.Column<bool>(type: "boolean", nullable: false),
Searchable = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
_ = table.PrimaryKey("PK_DeviceTags", x => x.Id);
});
}

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

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

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

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

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

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

b.HasKey("Id");

b.ToTable("DeviceTags");
});
#pragma warning restore 612, 618
}
}
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<DeviceTag> DeviceTags { 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.
Expand Down
43 changes: 43 additions & 0 deletions src/AzureIoTHub.Portal.Infrastructure/Seeds/DeviceTagSeeder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 Domain;
using Domain.Entities;
using Factories;
using Microsoft.EntityFrameworkCore;

internal static class DeviceTagSeeder
{
public static async Task MigrateDeviceTags(this PortalDbContext ctx, ConfigHandler config)
{
var table = new TableClientFactory(config.StorageAccountConnectionString)
.GetDeviceTagSettings();

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

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 DeviceTag
{
Id = item.RowKey,
Label = item[nameof(DeviceTag.Label)].ToString(),
Required = bool.Parse(item[nameof(DeviceTag.Required)].ToString() ?? "false"),
Searchable = bool.Parse(item[nameof(DeviceTag.Searchable)].ToString() ?? "false")
});
#pragma warning restore CS8629 // Nullable value type may be null.

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

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(...)'.
}
}
}
1 change: 1 addition & 0 deletions src/AzureIoTHub.Portal/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ await context
.MigrateDeviceModelProperties(config);

await context
.MigrateDeviceTags(config);
.MigrateDeviceModelCommands(config);

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

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

public string Label { get; set; }

public bool Required { get; set; }

public bool Searchable { get; set; }
}
}