From 0fecf0ab3823fc3c613a74868ca5b79e0f18f464 Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Mon, 15 Feb 2021 02:58:57 +0800 Subject: [PATCH] add to IoTSharp.Data.MySQL --- IoTSharp.ClientApp/IoTSharp.ClientApp.csproj | 2 +- .../IoTSharp.Data.MySQL.csproj | 23 + .../IoTSharpDataBuilderExtensions.cs | 30 + ...0210214185821_MySQLFirstSchema.Designer.cs | 769 ++++++++++++++++++ .../20210214185821_MySQLFirstSchema.cs | 592 ++++++++++++++ .../ApplicationDbContextModelSnapshot.cs | 767 +++++++++++++++++ .../IoTSharp.Data.PostgreSQL.csproj | 31 +- .../IoTSharpDataBuilderExtensions.cs | 30 + IoTSharp.Data/IoTSharp.Data.csproj | 2 + IoTSharp.sln | 26 +- IoTSharp/AppSettings.cs | 2 - IoTSharp/IoTSharp.csproj | 12 +- IoTSharp/Startup.cs | 70 +- IoTSharp/appsettings.Development.json | 6 +- 14 files changed, 2306 insertions(+), 56 deletions(-) create mode 100644 IoTSharp.Data.MySQL/IoTSharp.Data.MySQL.csproj create mode 100644 IoTSharp.Data.MySQL/IoTSharpDataBuilderExtensions.cs create mode 100644 IoTSharp.Data.MySQL/Migrations/20210214185821_MySQLFirstSchema.Designer.cs create mode 100644 IoTSharp.Data.MySQL/Migrations/20210214185821_MySQLFirstSchema.cs create mode 100644 IoTSharp.Data.MySQL/Migrations/ApplicationDbContextModelSnapshot.cs create mode 100644 IoTSharp.Data.PostgreSQL/IoTSharpDataBuilderExtensions.cs diff --git a/IoTSharp.ClientApp/IoTSharp.ClientApp.csproj b/IoTSharp.ClientApp/IoTSharp.ClientApp.csproj index 289de2c2..c22d9f7c 100644 --- a/IoTSharp.ClientApp/IoTSharp.ClientApp.csproj +++ b/IoTSharp.ClientApp/IoTSharp.ClientApp.csproj @@ -9,7 +9,7 @@ - + diff --git a/IoTSharp.Data.MySQL/IoTSharp.Data.MySQL.csproj b/IoTSharp.Data.MySQL/IoTSharp.Data.MySQL.csproj new file mode 100644 index 00000000..12183dc7 --- /dev/null +++ b/IoTSharp.Data.MySQL/IoTSharp.Data.MySQL.csproj @@ -0,0 +1,23 @@ + + + + net5.0 + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + diff --git a/IoTSharp.Data.MySQL/IoTSharpDataBuilderExtensions.cs b/IoTSharp.Data.MySQL/IoTSharpDataBuilderExtensions.cs new file mode 100644 index 00000000..786d0fbb --- /dev/null +++ b/IoTSharp.Data.MySQL/IoTSharpDataBuilderExtensions.cs @@ -0,0 +1,30 @@ + +using EFCore.Sharding; +using IoTSharp.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class IoTSharpDataBuilderExtensions + { + public static void ConfigureMySql(this IServiceCollection services, string connectionString,int poolSize , IHealthChecksBuilder checksBuilder, HealthChecksUIBuilder healthChecksUI) + { + services.AddDbContextPool(builder => + { + builder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), s => s.MigrationsAssembly("IoTSharp.Data.MySQL")); + } + , poolSize ); + + checksBuilder.AddMySql(connectionString, "IoTSharp.Data.MySQL"); + healthChecksUI.AddMySqlStorage(connectionString); + + } + public static void UseMySqlToSharding(this IShardingBuilder builder, string connectionString, ExpandByDateMode expandBy) + { + builder.AddDataSource(connectionString, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.MySql); + builder.SetDateSharding(nameof(TelemetryData.DateTime), expandBy, DateTime.Now); + } + } +} diff --git a/IoTSharp.Data.MySQL/Migrations/20210214185821_MySQLFirstSchema.Designer.cs b/IoTSharp.Data.MySQL/Migrations/20210214185821_MySQLFirstSchema.Designer.cs new file mode 100644 index 00000000..f3b552a6 --- /dev/null +++ b/IoTSharp.Data.MySQL/Migrations/20210214185821_MySQLFirstSchema.Designer.cs @@ -0,0 +1,769 @@ +// +using System; +using IoTSharp.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace IoTSharp.Data.MySQL.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20210214185821_MySQLFirstSchema")] + partial class MySQLFirstSchema + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 64) + .HasAnnotation("ProductVersion", "5.0.3"); + + modelBuilder.Entity("IoTSharp.Data.AuditLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("ActionData") + .HasColumnType("jsonb"); + + b.Property("ActionName") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ActionResult") + .HasColumnType("jsonb"); + + b.Property("ActiveDateTime") + .HasColumnType("datetime(6)"); + + b.Property("CustomerId") + .HasColumnType("char(36)"); + + b.Property("ObjectID") + .HasColumnType("char(36)"); + + b.Property("ObjectName") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ObjectType") + .HasColumnType("int"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.Property("UserId") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("UserName") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("TenantId"); + + b.ToTable("AuditLog"); + }); + + modelBuilder.Entity("IoTSharp.Data.AuthorizedKey", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("AuthToken") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("CustomerId") + .HasColumnType("char(36)"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("TenantId"); + + b.ToTable("AuthorizedKeys"); + }); + + modelBuilder.Entity("IoTSharp.Data.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Address") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("City") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Country") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Email") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Phone") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Province") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Street") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.Property("ZipCode") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("TenantId"); + + b.ToTable("Customer"); + }); + + modelBuilder.Entity("IoTSharp.Data.DataStorage", b => + { + b.Property("Catalog") + .HasColumnType("int"); + + b.Property("DeviceId") + .HasColumnType("char(36)"); + + b.Property("KeyName") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("DataSide") + .HasColumnType("int"); + + b.Property("DateTime") + .HasColumnType("timestamp with time zone(6)"); + + b.Property("Type") + .HasColumnType("int"); + + b.Property("Value_Binary") + .HasColumnType("longblob"); + + b.Property("Value_Boolean") + .HasColumnType("tinyint(1)"); + + b.Property("Value_DateTime") + .HasColumnType("timestamp with time zone(6)"); + + b.Property("Value_Double") + .HasColumnType("double"); + + b.Property("Value_Json") + .HasColumnType("jsonb"); + + b.Property("Value_Long") + .HasColumnType("bigint"); + + b.Property("Value_String") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Value_XML") + .HasColumnType("xml"); + + b.HasKey("Catalog", "DeviceId", "KeyName"); + + b.HasIndex("Catalog"); + + b.HasIndex("Catalog", "DeviceId"); + + b.ToTable("DataStorage"); + + b.HasDiscriminator("Catalog").HasValue(0); + }); + + modelBuilder.Entity("IoTSharp.Data.Device", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("AuthorizedKeyId") + .HasColumnType("char(36)"); + + b.Property("CustomerId") + .HasColumnType("char(36)"); + + b.Property("DeviceType") + .HasColumnType("int"); + + b.Property("LastActive") + .HasColumnType("datetime(6)"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Online") + .HasColumnType("tinyint(1)"); + + b.Property("OwnerId") + .HasColumnType("char(36)"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.Property("Timeout") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("AuthorizedKeyId"); + + b.HasIndex("CustomerId"); + + b.HasIndex("OwnerId"); + + b.HasIndex("TenantId"); + + b.ToTable("Device"); + + b.HasDiscriminator("DeviceType").HasValue(0); + }); + + modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("DeviceId") + .HasColumnType("char(36)"); + + b.Property("IdentityId") + .IsRequired() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("IdentityType") + .HasColumnType("int"); + + b.Property("IdentityValue") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("DeviceId"); + + b.ToTable("DeviceIdentities"); + }); + + modelBuilder.Entity("IoTSharp.Data.Relationship", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("CustomerId") + .HasColumnType("char(36)"); + + b.Property("IdentityUserId") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("IdentityUserId"); + + b.HasIndex("TenantId"); + + b.ToTable("Relationship"); + }); + + modelBuilder.Entity("IoTSharp.Data.TelemetryData", b => + { + b.Property("DeviceId") + .HasColumnType("char(36)"); + + b.Property("KeyName") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("DateTime") + .HasColumnType("timestamp with time zone(6)"); + + b.Property("DataSide") + .HasColumnType("int"); + + b.Property("Type") + .HasColumnType("int"); + + b.Property("Value_Binary") + .HasColumnType("longblob"); + + b.Property("Value_Boolean") + .HasColumnType("tinyint(1)"); + + b.Property("Value_DateTime") + .HasColumnType("timestamp with time zone(6)"); + + b.Property("Value_Double") + .HasColumnType("double"); + + b.Property("Value_Json") + .HasColumnType("jsonb"); + + b.Property("Value_Long") + .HasColumnType("bigint"); + + b.Property("Value_String") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Value_XML") + .HasColumnType("xml"); + + b.HasKey("DeviceId", "KeyName", "DateTime"); + + b.HasIndex("DeviceId"); + + b.HasIndex("KeyName"); + + b.HasIndex("DeviceId", "KeyName"); + + b.HasIndex("DeviceId", "KeyName", "DateTime"); + + b.ToTable("TelemetryData"); + }); + + modelBuilder.Entity("IoTSharp.Data.Tenant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Address") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("City") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Country") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("EMail") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Phone") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Province") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Street") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ZipCode") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Tenant"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => + { + b.Property("Id") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("ClaimType") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ClaimValue") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("RoleId") + .IsRequired() + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b => + { + b.Property("Id") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("AccessFailedCount") + .HasColumnType("int"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.Property("EmailConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnd") + .HasColumnType("datetime(6)"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.Property("PasswordHash") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("PhoneNumber") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("SecurityStamp") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("TwoFactorEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("ClaimType") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ClaimValue") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("ProviderKey") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("ProviderDisplayName") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("RoleId") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("LoginProvider") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("Name") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("Value") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("IoTSharp.Data.AttributeLatest", b => + { + b.HasBaseType("IoTSharp.Data.DataStorage"); + + b.HasDiscriminator().HasValue(2); + }); + + modelBuilder.Entity("IoTSharp.Data.TelemetryLatest", b => + { + b.HasBaseType("IoTSharp.Data.DataStorage"); + + b.HasDiscriminator().HasValue(4); + }); + + modelBuilder.Entity("IoTSharp.Data.Gateway", b => + { + b.HasBaseType("IoTSharp.Data.Device"); + + b.HasDiscriminator().HasValue(1); + }); + + modelBuilder.Entity("IoTSharp.Data.AuditLog", b => + { + b.HasOne("IoTSharp.Data.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId"); + + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany() + .HasForeignKey("TenantId"); + + b.Navigation("Customer"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("IoTSharp.Data.AuthorizedKey", b => + { + b.HasOne("IoTSharp.Data.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId"); + + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany() + .HasForeignKey("TenantId"); + + b.Navigation("Customer"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("IoTSharp.Data.Customer", b => + { + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany("Customers") + .HasForeignKey("TenantId"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("IoTSharp.Data.Device", b => + { + b.HasOne("IoTSharp.Data.AuthorizedKey", null) + .WithMany("Devices") + .HasForeignKey("AuthorizedKeyId"); + + b.HasOne("IoTSharp.Data.Customer", "Customer") + .WithMany("Devices") + .HasForeignKey("CustomerId"); + + b.HasOne("IoTSharp.Data.Gateway", "Owner") + .WithMany("Children") + .HasForeignKey("OwnerId"); + + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany("Devices") + .HasForeignKey("TenantId"); + + b.Navigation("Customer"); + + b.Navigation("Owner"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b => + { + b.HasOne("IoTSharp.Data.Device", "Device") + .WithMany() + .HasForeignKey("DeviceId"); + + b.Navigation("Device"); + }); + + modelBuilder.Entity("IoTSharp.Data.Relationship", b => + { + b.HasOne("IoTSharp.Data.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId"); + + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "IdentityUser") + .WithMany() + .HasForeignKey("IdentityUserId"); + + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany() + .HasForeignKey("TenantId"); + + b.Navigation("Customer"); + + b.Navigation("IdentityUser"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("IoTSharp.Data.AuthorizedKey", b => + { + b.Navigation("Devices"); + }); + + modelBuilder.Entity("IoTSharp.Data.Customer", b => + { + b.Navigation("Devices"); + }); + + modelBuilder.Entity("IoTSharp.Data.Tenant", b => + { + b.Navigation("Customers"); + + b.Navigation("Devices"); + }); + + modelBuilder.Entity("IoTSharp.Data.Gateway", b => + { + b.Navigation("Children"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IoTSharp.Data.MySQL/Migrations/20210214185821_MySQLFirstSchema.cs b/IoTSharp.Data.MySQL/Migrations/20210214185821_MySQLFirstSchema.cs new file mode 100644 index 00000000..e5bbec88 --- /dev/null +++ b/IoTSharp.Data.MySQL/Migrations/20210214185821_MySQLFirstSchema.cs @@ -0,0 +1,592 @@ +using System; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace IoTSharp.Data.MySQL.Migrations +{ + public partial class MySQLFirstSchema : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AspNetRoles", + columns: table => new + { + Id = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + Name = table.Column(type: "varchar(256) CHARACTER SET utf8mb4", maxLength: 256, nullable: true), + NormalizedName = table.Column(type: "varchar(256) CHARACTER SET utf8mb4", maxLength: 256, nullable: true), + ConcurrencyStamp = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetUsers", + columns: table => new + { + Id = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + UserName = table.Column(type: "varchar(256) CHARACTER SET utf8mb4", maxLength: 256, nullable: true), + NormalizedUserName = table.Column(type: "varchar(256) CHARACTER SET utf8mb4", maxLength: 256, nullable: true), + Email = table.Column(type: "varchar(256) CHARACTER SET utf8mb4", maxLength: 256, nullable: true), + NormalizedEmail = table.Column(type: "varchar(256) CHARACTER SET utf8mb4", maxLength: 256, nullable: true), + EmailConfirmed = table.Column(type: "tinyint(1)", nullable: false), + PasswordHash = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + SecurityStamp = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + ConcurrencyStamp = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + PhoneNumber = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + PhoneNumberConfirmed = table.Column(type: "tinyint(1)", nullable: false), + TwoFactorEnabled = table.Column(type: "tinyint(1)", nullable: false), + LockoutEnd = table.Column(type: "datetime(6)", nullable: true), + LockoutEnabled = table.Column(type: "tinyint(1)", nullable: false), + AccessFailedCount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUsers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DataStorage", + columns: table => new + { + Catalog = table.Column(type: "int", nullable: false), + DeviceId = table.Column(type: "char(36)", nullable: false), + KeyName = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + DateTime = table.Column(type: "timestamp with time zone(6)", nullable: false), + DataSide = table.Column(type: "int", nullable: false), + Type = table.Column(type: "int", nullable: false), + Value_Boolean = table.Column(type: "tinyint(1)", nullable: false), + Value_String = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Value_Long = table.Column(type: "bigint", nullable: false), + Value_DateTime = table.Column(type: "timestamp with time zone(6)", nullable: false), + Value_Double = table.Column(type: "double", nullable: false), + Value_Json = table.Column(type: "jsonb", nullable: true), + Value_XML = table.Column(type: "xml", nullable: true), + Value_Binary = table.Column(type: "longblob", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_DataStorage", x => new { x.Catalog, x.DeviceId, x.KeyName }); + }); + + migrationBuilder.CreateTable( + name: "TelemetryData", + columns: table => new + { + DateTime = table.Column(type: "timestamp with time zone(6)", nullable: false), + DeviceId = table.Column(type: "char(36)", nullable: false), + KeyName = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + DataSide = table.Column(type: "int", nullable: false), + Type = table.Column(type: "int", nullable: false), + Value_Boolean = table.Column(type: "tinyint(1)", nullable: false), + Value_String = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Value_Long = table.Column(type: "bigint", nullable: false), + Value_DateTime = table.Column(type: "timestamp with time zone(6)", nullable: false), + Value_Double = table.Column(type: "double", nullable: false), + Value_Json = table.Column(type: "jsonb", nullable: true), + Value_XML = table.Column(type: "xml", nullable: true), + Value_Binary = table.Column(type: "longblob", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_TelemetryData", x => new { x.DeviceId, x.KeyName, x.DateTime }); + }); + + migrationBuilder.CreateTable( + name: "Tenant", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + Name = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + EMail = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Phone = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Country = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Province = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + City = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Street = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Address = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + ZipCode = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Tenant", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetRoleClaims", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), + RoleId = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + ClaimType = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + ClaimValue = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserClaims", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), + UserId = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + ClaimType = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + ClaimValue = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetUserClaims_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserLogins", + columns: table => new + { + LoginProvider = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + ProviderKey = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + ProviderDisplayName = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + UserId = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey }); + table.ForeignKey( + name: "FK_AspNetUserLogins_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserRoles", + columns: table => new + { + UserId = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + RoleId = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId }); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserTokens", + columns: table => new + { + UserId = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + LoginProvider = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + Name = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false), + Value = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); + table.ForeignKey( + name: "FK_AspNetUserTokens_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Customer", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + Name = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Email = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Phone = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Country = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Province = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + City = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Street = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + Address = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + ZipCode = table.Column(type: "int", nullable: false), + TenantId = table.Column(type: "char(36)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Customer", x => x.Id); + table.ForeignKey( + name: "FK_Customer_Tenant_TenantId", + column: x => x.TenantId, + principalTable: "Tenant", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "AuditLog", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + TenantId = table.Column(type: "char(36)", nullable: true), + CustomerId = table.Column(type: "char(36)", nullable: true), + UserId = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + UserName = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + ObjectID = table.Column(type: "char(36)", nullable: false), + ObjectName = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + ObjectType = table.Column(type: "int", nullable: false), + ActionName = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + ActionData = table.Column(type: "jsonb", nullable: true), + ActionResult = table.Column(type: "jsonb", nullable: true), + ActiveDateTime = table.Column(type: "datetime(6)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AuditLog", x => x.Id); + table.ForeignKey( + name: "FK_AuditLog_Customer_CustomerId", + column: x => x.CustomerId, + principalTable: "Customer", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AuditLog_Tenant_TenantId", + column: x => x.TenantId, + principalTable: "Tenant", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "AuthorizedKeys", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + Name = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + AuthToken = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + TenantId = table.Column(type: "char(36)", nullable: true), + CustomerId = table.Column(type: "char(36)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AuthorizedKeys", x => x.Id); + table.ForeignKey( + name: "FK_AuthorizedKeys_Customer_CustomerId", + column: x => x.CustomerId, + principalTable: "Customer", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_AuthorizedKeys_Tenant_TenantId", + column: x => x.TenantId, + principalTable: "Tenant", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "Relationship", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + IdentityUserId = table.Column(type: "varchar(255) CHARACTER SET utf8mb4", nullable: true), + TenantId = table.Column(type: "char(36)", nullable: true), + CustomerId = table.Column(type: "char(36)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Relationship", x => x.Id); + table.ForeignKey( + name: "FK_Relationship_AspNetUsers_IdentityUserId", + column: x => x.IdentityUserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_Relationship_Customer_CustomerId", + column: x => x.CustomerId, + principalTable: "Customer", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_Relationship_Tenant_TenantId", + column: x => x.TenantId, + principalTable: "Tenant", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "Device", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + Name = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + DeviceType = table.Column(type: "int", nullable: false), + Online = table.Column(type: "tinyint(1)", nullable: false), + LastActive = table.Column(type: "datetime(6)", nullable: false), + Timeout = table.Column(type: "int", nullable: false), + OwnerId = table.Column(type: "char(36)", nullable: true), + TenantId = table.Column(type: "char(36)", nullable: true), + CustomerId = table.Column(type: "char(36)", nullable: true), + AuthorizedKeyId = table.Column(type: "char(36)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Device", x => x.Id); + table.ForeignKey( + name: "FK_Device_AuthorizedKeys_AuthorizedKeyId", + column: x => x.AuthorizedKeyId, + principalTable: "AuthorizedKeys", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_Device_Customer_CustomerId", + column: x => x.CustomerId, + principalTable: "Customer", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_Device_Device_OwnerId", + column: x => x.OwnerId, + principalTable: "Device", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_Device_Tenant_TenantId", + column: x => x.TenantId, + principalTable: "Tenant", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "DeviceIdentities", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + IdentityType = table.Column(type: "int", nullable: false), + IdentityId = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: false), + IdentityValue = table.Column(type: "longtext CHARACTER SET utf8mb4", nullable: true), + DeviceId = table.Column(type: "char(36)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_DeviceIdentities", x => x.Id); + table.ForeignKey( + name: "FK_DeviceIdentities_Device_DeviceId", + column: x => x.DeviceId, + principalTable: "Device", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_AspNetRoleClaims_RoleId", + table: "AspNetRoleClaims", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "RoleNameIndex", + table: "AspNetRoles", + column: "NormalizedName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserClaims_UserId", + table: "AspNetUserClaims", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserLogins_UserId", + table: "AspNetUserLogins", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserRoles_RoleId", + table: "AspNetUserRoles", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "EmailIndex", + table: "AspNetUsers", + column: "NormalizedEmail"); + + migrationBuilder.CreateIndex( + name: "UserNameIndex", + table: "AspNetUsers", + column: "NormalizedUserName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AuditLog_CustomerId", + table: "AuditLog", + column: "CustomerId"); + + migrationBuilder.CreateIndex( + name: "IX_AuditLog_TenantId", + table: "AuditLog", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_AuthorizedKeys_CustomerId", + table: "AuthorizedKeys", + column: "CustomerId"); + + migrationBuilder.CreateIndex( + name: "IX_AuthorizedKeys_TenantId", + table: "AuthorizedKeys", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_Customer_TenantId", + table: "Customer", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_DataStorage_Catalog", + table: "DataStorage", + column: "Catalog"); + + migrationBuilder.CreateIndex( + name: "IX_DataStorage_Catalog_DeviceId", + table: "DataStorage", + columns: new[] { "Catalog", "DeviceId" }); + + migrationBuilder.CreateIndex( + name: "IX_Device_AuthorizedKeyId", + table: "Device", + column: "AuthorizedKeyId"); + + migrationBuilder.CreateIndex( + name: "IX_Device_CustomerId", + table: "Device", + column: "CustomerId"); + + migrationBuilder.CreateIndex( + name: "IX_Device_OwnerId", + table: "Device", + column: "OwnerId"); + + migrationBuilder.CreateIndex( + name: "IX_Device_TenantId", + table: "Device", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_DeviceIdentities_DeviceId", + table: "DeviceIdentities", + column: "DeviceId"); + + migrationBuilder.CreateIndex( + name: "IX_Relationship_CustomerId", + table: "Relationship", + column: "CustomerId"); + + migrationBuilder.CreateIndex( + name: "IX_Relationship_IdentityUserId", + table: "Relationship", + column: "IdentityUserId"); + + migrationBuilder.CreateIndex( + name: "IX_Relationship_TenantId", + table: "Relationship", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_TelemetryData_DeviceId", + table: "TelemetryData", + column: "DeviceId"); + + migrationBuilder.CreateIndex( + name: "IX_TelemetryData_DeviceId_KeyName", + table: "TelemetryData", + columns: new[] { "DeviceId", "KeyName" }); + + migrationBuilder.CreateIndex( + name: "IX_TelemetryData_DeviceId_KeyName_DateTime", + table: "TelemetryData", + columns: new[] { "DeviceId", "KeyName", "DateTime" }); + + migrationBuilder.CreateIndex( + name: "IX_TelemetryData_KeyName", + table: "TelemetryData", + column: "KeyName"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AspNetRoleClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserLogins"); + + migrationBuilder.DropTable( + name: "AspNetUserRoles"); + + migrationBuilder.DropTable( + name: "AspNetUserTokens"); + + migrationBuilder.DropTable( + name: "AuditLog"); + + migrationBuilder.DropTable( + name: "DataStorage"); + + migrationBuilder.DropTable( + name: "DeviceIdentities"); + + migrationBuilder.DropTable( + name: "Relationship"); + + migrationBuilder.DropTable( + name: "TelemetryData"); + + migrationBuilder.DropTable( + name: "AspNetRoles"); + + migrationBuilder.DropTable( + name: "Device"); + + migrationBuilder.DropTable( + name: "AspNetUsers"); + + migrationBuilder.DropTable( + name: "AuthorizedKeys"); + + migrationBuilder.DropTable( + name: "Customer"); + + migrationBuilder.DropTable( + name: "Tenant"); + } + } +} diff --git a/IoTSharp.Data.MySQL/Migrations/ApplicationDbContextModelSnapshot.cs b/IoTSharp.Data.MySQL/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 00000000..b92f94b8 --- /dev/null +++ b/IoTSharp.Data.MySQL/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,767 @@ +// +using System; +using IoTSharp.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace IoTSharp.Data.MySQL.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 64) + .HasAnnotation("ProductVersion", "5.0.3"); + + modelBuilder.Entity("IoTSharp.Data.AuditLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("ActionData") + .HasColumnType("jsonb"); + + b.Property("ActionName") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ActionResult") + .HasColumnType("jsonb"); + + b.Property("ActiveDateTime") + .HasColumnType("datetime(6)"); + + b.Property("CustomerId") + .HasColumnType("char(36)"); + + b.Property("ObjectID") + .HasColumnType("char(36)"); + + b.Property("ObjectName") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ObjectType") + .HasColumnType("int"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.Property("UserId") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("UserName") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("TenantId"); + + b.ToTable("AuditLog"); + }); + + modelBuilder.Entity("IoTSharp.Data.AuthorizedKey", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("AuthToken") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("CustomerId") + .HasColumnType("char(36)"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("TenantId"); + + b.ToTable("AuthorizedKeys"); + }); + + modelBuilder.Entity("IoTSharp.Data.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Address") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("City") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Country") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Email") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Phone") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Province") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Street") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.Property("ZipCode") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("TenantId"); + + b.ToTable("Customer"); + }); + + modelBuilder.Entity("IoTSharp.Data.DataStorage", b => + { + b.Property("Catalog") + .HasColumnType("int"); + + b.Property("DeviceId") + .HasColumnType("char(36)"); + + b.Property("KeyName") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("DataSide") + .HasColumnType("int"); + + b.Property("DateTime") + .HasColumnType("timestamp with time zone(6)"); + + b.Property("Type") + .HasColumnType("int"); + + b.Property("Value_Binary") + .HasColumnType("longblob"); + + b.Property("Value_Boolean") + .HasColumnType("tinyint(1)"); + + b.Property("Value_DateTime") + .HasColumnType("timestamp with time zone(6)"); + + b.Property("Value_Double") + .HasColumnType("double"); + + b.Property("Value_Json") + .HasColumnType("jsonb"); + + b.Property("Value_Long") + .HasColumnType("bigint"); + + b.Property("Value_String") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Value_XML") + .HasColumnType("xml"); + + b.HasKey("Catalog", "DeviceId", "KeyName"); + + b.HasIndex("Catalog"); + + b.HasIndex("Catalog", "DeviceId"); + + b.ToTable("DataStorage"); + + b.HasDiscriminator("Catalog").HasValue(0); + }); + + modelBuilder.Entity("IoTSharp.Data.Device", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("AuthorizedKeyId") + .HasColumnType("char(36)"); + + b.Property("CustomerId") + .HasColumnType("char(36)"); + + b.Property("DeviceType") + .HasColumnType("int"); + + b.Property("LastActive") + .HasColumnType("datetime(6)"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Online") + .HasColumnType("tinyint(1)"); + + b.Property("OwnerId") + .HasColumnType("char(36)"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.Property("Timeout") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("AuthorizedKeyId"); + + b.HasIndex("CustomerId"); + + b.HasIndex("OwnerId"); + + b.HasIndex("TenantId"); + + b.ToTable("Device"); + + b.HasDiscriminator("DeviceType").HasValue(0); + }); + + modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("DeviceId") + .HasColumnType("char(36)"); + + b.Property("IdentityId") + .IsRequired() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("IdentityType") + .HasColumnType("int"); + + b.Property("IdentityValue") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("DeviceId"); + + b.ToTable("DeviceIdentities"); + }); + + modelBuilder.Entity("IoTSharp.Data.Relationship", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("CustomerId") + .HasColumnType("char(36)"); + + b.Property("IdentityUserId") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("TenantId") + .HasColumnType("char(36)"); + + b.HasKey("Id"); + + b.HasIndex("CustomerId"); + + b.HasIndex("IdentityUserId"); + + b.HasIndex("TenantId"); + + b.ToTable("Relationship"); + }); + + modelBuilder.Entity("IoTSharp.Data.TelemetryData", b => + { + b.Property("DeviceId") + .HasColumnType("char(36)"); + + b.Property("KeyName") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("DateTime") + .HasColumnType("timestamp with time zone(6)"); + + b.Property("DataSide") + .HasColumnType("int"); + + b.Property("Type") + .HasColumnType("int"); + + b.Property("Value_Binary") + .HasColumnType("longblob"); + + b.Property("Value_Boolean") + .HasColumnType("tinyint(1)"); + + b.Property("Value_DateTime") + .HasColumnType("timestamp with time zone(6)"); + + b.Property("Value_Double") + .HasColumnType("double"); + + b.Property("Value_Json") + .HasColumnType("jsonb"); + + b.Property("Value_Long") + .HasColumnType("bigint"); + + b.Property("Value_String") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Value_XML") + .HasColumnType("xml"); + + b.HasKey("DeviceId", "KeyName", "DateTime"); + + b.HasIndex("DeviceId"); + + b.HasIndex("KeyName"); + + b.HasIndex("DeviceId", "KeyName"); + + b.HasIndex("DeviceId", "KeyName", "DateTime"); + + b.ToTable("TelemetryData"); + }); + + modelBuilder.Entity("IoTSharp.Data.Tenant", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Address") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("City") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Country") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("EMail") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Phone") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Province") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Street") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ZipCode") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Tenant"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => + { + b.Property("Id") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("ClaimType") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ClaimValue") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("RoleId") + .IsRequired() + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b => + { + b.Property("Id") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("AccessFailedCount") + .HasColumnType("int"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.Property("EmailConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("LockoutEnd") + .HasColumnType("datetime(6)"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.Property("PasswordHash") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("PhoneNumber") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("tinyint(1)"); + + b.Property("SecurityStamp") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("TwoFactorEnabled") + .HasColumnType("tinyint(1)"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("varchar(256) CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("ClaimType") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("ClaimValue") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("ProviderKey") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("ProviderDisplayName") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("RoleId") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("LoginProvider") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("Name") + .HasColumnType("varchar(255) CHARACTER SET utf8mb4"); + + b.Property("Value") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("IoTSharp.Data.AttributeLatest", b => + { + b.HasBaseType("IoTSharp.Data.DataStorage"); + + b.HasDiscriminator().HasValue(2); + }); + + modelBuilder.Entity("IoTSharp.Data.TelemetryLatest", b => + { + b.HasBaseType("IoTSharp.Data.DataStorage"); + + b.HasDiscriminator().HasValue(4); + }); + + modelBuilder.Entity("IoTSharp.Data.Gateway", b => + { + b.HasBaseType("IoTSharp.Data.Device"); + + b.HasDiscriminator().HasValue(1); + }); + + modelBuilder.Entity("IoTSharp.Data.AuditLog", b => + { + b.HasOne("IoTSharp.Data.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId"); + + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany() + .HasForeignKey("TenantId"); + + b.Navigation("Customer"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("IoTSharp.Data.AuthorizedKey", b => + { + b.HasOne("IoTSharp.Data.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId"); + + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany() + .HasForeignKey("TenantId"); + + b.Navigation("Customer"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("IoTSharp.Data.Customer", b => + { + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany("Customers") + .HasForeignKey("TenantId"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("IoTSharp.Data.Device", b => + { + b.HasOne("IoTSharp.Data.AuthorizedKey", null) + .WithMany("Devices") + .HasForeignKey("AuthorizedKeyId"); + + b.HasOne("IoTSharp.Data.Customer", "Customer") + .WithMany("Devices") + .HasForeignKey("CustomerId"); + + b.HasOne("IoTSharp.Data.Gateway", "Owner") + .WithMany("Children") + .HasForeignKey("OwnerId"); + + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany("Devices") + .HasForeignKey("TenantId"); + + b.Navigation("Customer"); + + b.Navigation("Owner"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b => + { + b.HasOne("IoTSharp.Data.Device", "Device") + .WithMany() + .HasForeignKey("DeviceId"); + + b.Navigation("Device"); + }); + + modelBuilder.Entity("IoTSharp.Data.Relationship", b => + { + b.HasOne("IoTSharp.Data.Customer", "Customer") + .WithMany() + .HasForeignKey("CustomerId"); + + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "IdentityUser") + .WithMany() + .HasForeignKey("IdentityUserId"); + + b.HasOne("IoTSharp.Data.Tenant", "Tenant") + .WithMany() + .HasForeignKey("TenantId"); + + b.Navigation("Customer"); + + b.Navigation("IdentityUser"); + + b.Navigation("Tenant"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("IoTSharp.Data.AuthorizedKey", b => + { + b.Navigation("Devices"); + }); + + modelBuilder.Entity("IoTSharp.Data.Customer", b => + { + b.Navigation("Devices"); + }); + + modelBuilder.Entity("IoTSharp.Data.Tenant", b => + { + b.Navigation("Customers"); + + b.Navigation("Devices"); + }); + + modelBuilder.Entity("IoTSharp.Data.Gateway", b => + { + b.Navigation("Children"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IoTSharp.Data.PostgreSQL/IoTSharp.Data.PostgreSQL.csproj b/IoTSharp.Data.PostgreSQL/IoTSharp.Data.PostgreSQL.csproj index 0eb2e1ac..f883dd34 100644 --- a/IoTSharp.Data.PostgreSQL/IoTSharp.Data.PostgreSQL.csproj +++ b/IoTSharp.Data.PostgreSQL/IoTSharp.Data.PostgreSQL.csproj @@ -1,19 +1,24 @@  - - net5.0 - + + net5.0 + - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + - - - + + + diff --git a/IoTSharp.Data.PostgreSQL/IoTSharpDataBuilderExtensions.cs b/IoTSharp.Data.PostgreSQL/IoTSharpDataBuilderExtensions.cs new file mode 100644 index 00000000..133b0828 --- /dev/null +++ b/IoTSharp.Data.PostgreSQL/IoTSharpDataBuilderExtensions.cs @@ -0,0 +1,30 @@ + +using EFCore.Sharding; +using IoTSharp.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class IoTSharpDataBuilderExtensions + { + public static void ConfigureNpgsql(this IServiceCollection services, string connectionString, int poolSize, IHealthChecksBuilder checksBuilder, HealthChecksUIBuilder healthChecksUI) + { + services.AddDbContextPool(builder => + { + builder.UseNpgsql(connectionString, s => s.MigrationsAssembly("IoTSharp.Data.PostgreSQL")); + } + , poolSize); + checksBuilder.AddNpgSql(connectionString); + healthChecksUI.AddPostgreSqlStorage(connectionString); + + } + + public static void UseNpgsqlToSharding(this IShardingBuilder builder, string connectionString, ExpandByDateMode expandBy) + { + builder.AddDataSource(connectionString, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.PostgreSql); + builder.SetDateSharding(nameof(TelemetryData.DateTime), expandBy, DateTime.Now); + } + } +} diff --git a/IoTSharp.Data/IoTSharp.Data.csproj b/IoTSharp.Data/IoTSharp.Data.csproj index edb5d7ba..f8d54beb 100644 --- a/IoTSharp.Data/IoTSharp.Data.csproj +++ b/IoTSharp.Data/IoTSharp.Data.csproj @@ -9,6 +9,8 @@ + + diff --git a/IoTSharp.sln b/IoTSharp.sln index a71bf441..c46924a2 100644 --- a/IoTSharp.sln +++ b/IoTSharp.sln @@ -43,9 +43,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IoTSharp.Sdk.Http", "IoTSha EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IoTSharp.Sdk.MQTT", "IoTSharp.SDKs\IoTSharp.Sdk.MQTT\IoTSharp.Sdk.MQTT.csproj", "{A2A1191C-32ED-482B-AACE-4091EE518CEC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IoTSharp.Data.PostgreSQL", "IoTSharp.Data.PostgreSQL\IoTSharp.Data.PostgreSQL.csproj", "{0A6EAEEC-79A3-406B-9897-C3DA17F06BB9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IoTSharp.Data.PostgreSQL", "IoTSharp.Data.PostgreSQL\IoTSharp.Data.PostgreSQL.csproj", "{0A6EAEEC-79A3-406B-9897-C3DA17F06BB9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IoTSharp.Data", "IoTSharp.Data\IoTSharp.Data.csproj", "{522B2676-EB0E-4191-ACBC-EDC86DD2351D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IoTSharp.Data", "IoTSharp.Data\IoTSharp.Data.csproj", "{522B2676-EB0E-4191-ACBC-EDC86DD2351D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IoTSharp.Data.MySQL", "IoTSharp.Data.MySQL\IoTSharp.Data.MySQL.csproj", "{233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -276,6 +278,26 @@ Global {522B2676-EB0E-4191-ACBC-EDC86DD2351D}.Release|x64.Build.0 = Release|Any CPU {522B2676-EB0E-4191-ACBC-EDC86DD2351D}.Release|x86.ActiveCfg = Release|Any CPU {522B2676-EB0E-4191-ACBC-EDC86DD2351D}.Release|x86.Build.0 = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|ARM.ActiveCfg = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|ARM.Build.0 = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|ARM64.Build.0 = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|x64.ActiveCfg = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|x64.Build.0 = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|x86.ActiveCfg = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Debug|x86.Build.0 = Debug|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|Any CPU.Build.0 = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|ARM.ActiveCfg = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|ARM.Build.0 = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|ARM64.ActiveCfg = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|ARM64.Build.0 = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|x64.ActiveCfg = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|x64.Build.0 = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|x86.ActiveCfg = Release|Any CPU + {233F2C8D-DD4C-49B7-99A2-F3DB5620DEFB}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IoTSharp/AppSettings.cs b/IoTSharp/AppSettings.cs index 071a2cf0..17d2ad42 100644 --- a/IoTSharp/AppSettings.cs +++ b/IoTSharp/AppSettings.cs @@ -1,9 +1,7 @@ using CoAP; -using CoAP.Server; using DotNetCore.CAP.Dashboard.NodeDiscovery; using EFCore.Sharding; using IoTSharp.Data; -using MaiKeBing.CAP; using MaiKeBing.HostedService.ZeroMQ; using Newtonsoft.Json; using Newtonsoft.Json.Converters; diff --git a/IoTSharp/IoTSharp.csproj b/IoTSharp/IoTSharp.csproj index da56ca8a..024d2828 100644 --- a/IoTSharp/IoTSharp.csproj +++ b/IoTSharp/IoTSharp.csproj @@ -40,18 +40,18 @@ - + - + - + @@ -64,8 +64,7 @@ - - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -91,6 +90,7 @@ + @@ -114,7 +114,6 @@ - @@ -139,6 +138,7 @@ + diff --git a/IoTSharp/Startup.cs b/IoTSharp/Startup.cs index ada4a7a1..ea500706 100644 --- a/IoTSharp/Startup.cs +++ b/IoTSharp/Startup.cs @@ -72,18 +72,35 @@ namespace IoTSharp { Configuration.Bind(setting); })); - services.AddDbContextPool(options => + var healthChecksUI = services.AddHealthChecksUI(setup => { - switch (settings.DataBase) - { - case DataBaseType.PostgreSql: - default: - options.UseNpgsql(Configuration.GetConnectionString("IoTSharp"), s => s.MigrationsAssembly("IoTSharp.Data.PostgreSQL")); - break; - } - + setup.SetHeaderText("IoTSharp HealthChecks"); + //Maximum history entries by endpoint + setup.MaximumHistoryEntriesPerEndpoint(50); + setup.AddIoTSharpHealthCheckEndpoint(); + }); + + var healthChecks = services.AddHealthChecks() + .AddDiskStorageHealthCheck(dso => + { + System.IO.DriveInfo.GetDrives() + .Where(d => d.DriveType != System.IO.DriveType.CDRom && d.DriveType != System.IO.DriveType.Ram) + .Select(f => f.Name).Distinct().ToList() + .ForEach(f => dso.AddDrive(f, 1024)); + + }, name: "Disk Storage"); + + switch (settings.DataBase) + { + case DataBaseType.MySql: + services.ConfigureMySql(Configuration.GetConnectionString("IoTSharp"), settings.DbContextPoolSize, healthChecks, healthChecksUI); + break; + case DataBaseType.PostgreSql: + default: + services.ConfigureNpgsql(Configuration.GetConnectionString("IoTSharp"), settings.DbContextPoolSize, healthChecks, healthChecksUI); + break; } - , poolSize: settings.DbContextPoolSize); + services.AddIdentity() .AddRoles() .AddRoleManager>() @@ -128,24 +145,8 @@ namespace IoTSharp services.AddMqttClient(settings.MqttClient); services.AddSingleton(); - var healthChecks = services.AddHealthChecks() - .AddNpgSql(Configuration.GetConnectionString("IoTSharp"), name: "PostgreSQL") - .AddDiskStorageHealthCheck(dso => - { - System.IO.DriveInfo.GetDrives() - .Where(d=>d.DriveType != System.IO.DriveType.CDRom && d.DriveType!= System.IO.DriveType.Ram) - .Select(f => f.Name).Distinct().ToList() - .ForEach(f => dso.AddDrive(f, 1024)); - - }, name: "Disk Storage"); - - services.AddHealthChecksUI(setup => - { - setup.SetHeaderText("IoTSharp HealthChecks"); - //Maximum history entries by endpoint - setup.MaximumHistoryEntriesPerEndpoint(50); - setup.AddIoTSharpHealthCheckEndpoint(); - }).AddPostgreSqlStorage(Configuration.GetConnectionString("IoTSharp")); + + services.AddSilkierQuartz(opt => { @@ -194,8 +195,17 @@ namespace IoTSharp case TelemetryStorage.Sharding: services.AddEFCoreSharding(config => { - config.AddDataSource(Configuration.GetConnectionString("TelemetryStorage"), ReadWriteType.Read | ReadWriteType.Write, settings.Sharding.DatabaseType); - config.SetDateSharding(nameof(TelemetryData.DateTime), settings.Sharding.ExpandByDateMode, DateTime.Now); + switch (settings.DataBase) + { + case DataBaseType.MySql: + config.UseMySqlToSharding(Configuration.GetConnectionString("TelemetryStorage"), settings.Sharding.ExpandByDateMode); + break; + case DataBaseType.PostgreSql: + default: + config.UseNpgsqlToSharding(Configuration.GetConnectionString("TelemetryStorage"), settings.Sharding.ExpandByDateMode); + break; + } + }); services.AddSingleton(); break; diff --git a/IoTSharp/appsettings.Development.json b/IoTSharp/appsettings.Development.json index 23cb8fc6..d9ce7af6 100644 --- a/IoTSharp/appsettings.Development.json +++ b/IoTSharp/appsettings.Development.json @@ -6,8 +6,10 @@ "Microsoft": "Information" } }, + "DataBase": "MySql", "ConnectionStrings": { - "IoTSharp": "Server=localhost;Database=IoTSharp;Username=postgres;Password=future;", + //pgsql "IoTSharp": "Server=localhost;Database=IoTSharp;Username=postgres;Password=future;", + "IoTSharp": "server=localhost;user=root;password=kissme;database=IoTSharp", "EventBusStore": "mongodb://root:kissme@mongodb:27017", "TelemetryStorage": "DataSource=taos;DataBase=IoTSharp;Username=root;Password=taosdata;Port=6030", "EventBusMQ": "amqp://root:kissme@rabbitmq:5672" @@ -19,7 +21,7 @@ "JwtAudience": "IoTSharp.Net", "EventBusStore": "InMemory", "EventBusMQ": "InMemory", - "TelemetryStorage": "Taos" + "TelemetryStorage": "Sharding" //"Discovery": { // "DiscoveryServerHostName": "iotsharp", // "DiscoveryServerPort": 8500, -- GitLab