using System; using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable namespace Tau.Acuvim.Portal.Migrations.Admin { /// public partial class InitialFleet : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.EnsureSchema( name: "identity"); migrationBuilder.EnsureSchema( name: "fleet"); migrationBuilder.EnsureSchema( name: "app"); migrationBuilder.CreateTable( name: "AspNetRoles", schema: "identity", columns: table => new { Id = table.Column(type: "text", nullable: false), Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), ConcurrencyStamp = table.Column(type: "text", nullable: true) }, constraints: table => { table.PrimaryKey("PK_AspNetRoles", x => x.Id); }); migrationBuilder.CreateTable( name: "AspNetUsers", schema: "identity", columns: table => new { Id = table.Column(type: "text", nullable: false), DisplayName = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), IsActive = table.Column(type: "boolean", nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), EmailConfirmed = table.Column(type: "boolean", nullable: false), PasswordHash = table.Column(type: "text", nullable: true), SecurityStamp = table.Column(type: "text", nullable: true), ConcurrencyStamp = table.Column(type: "text", nullable: true), PhoneNumber = table.Column(type: "text", nullable: true), PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), TwoFactorEnabled = table.Column(type: "boolean", nullable: false), LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), LockoutEnabled = table.Column(type: "boolean", nullable: false), AccessFailedCount = table.Column(type: "integer", nullable: false) }, constraints: table => { table.PrimaryKey("PK_AspNetUsers", x => x.Id); }); migrationBuilder.CreateTable( name: "Customers", schema: "fleet", columns: table => new { Id = table.Column(type: "uuid", nullable: false), Code = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), TokenHash = table.Column(type: "character varying(64)", maxLength: 64, nullable: false), TokenIssuedAt = table.Column(type: "timestamp with time zone", nullable: false), TokenRotatedAt = table.Column(type: "timestamp with time zone", nullable: true), IsActive = table.Column(type: "boolean", nullable: false), FirstSeenAt = table.Column(type: "timestamp with time zone", nullable: true), LastSeenAt = table.Column(type: "timestamp with time zone", nullable: true), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Customers", x => x.Id); }); migrationBuilder.CreateTable( name: "WhiteLabelSettings", schema: "app", columns: table => new { Id = table.Column(type: "integer", nullable: false), ApplicationName = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), LogoUrl = table.Column(type: "character varying(500)", maxLength: 500, nullable: false), PrimaryColor = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), SecondaryColor = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), AccentColor = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), FooterText = table.Column(type: "character varying(500)", maxLength: 500, nullable: false), UpdatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_WhiteLabelSettings", x => x.Id); }); migrationBuilder.CreateTable( name: "AspNetRoleClaims", schema: "identity", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), RoleId = table.Column(type: "text", nullable: false), ClaimType = table.Column(type: "text", nullable: true), ClaimValue = table.Column(type: "text", nullable: true) }, constraints: table => { table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); table.ForeignKey( name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", column: x => x.RoleId, principalSchema: "identity", principalTable: "AspNetRoles", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "AspNetUserClaims", schema: "identity", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), UserId = table.Column(type: "text", nullable: false), ClaimType = table.Column(type: "text", nullable: true), ClaimValue = table.Column(type: "text", nullable: true) }, constraints: table => { table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); table.ForeignKey( name: "FK_AspNetUserClaims_AspNetUsers_UserId", column: x => x.UserId, principalSchema: "identity", principalTable: "AspNetUsers", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "AspNetUserLogins", schema: "identity", columns: table => new { LoginProvider = table.Column(type: "text", nullable: false), ProviderKey = table.Column(type: "text", nullable: false), ProviderDisplayName = table.Column(type: "text", nullable: true), UserId = table.Column(type: "text", 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, principalSchema: "identity", principalTable: "AspNetUsers", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "AspNetUserRoles", schema: "identity", columns: table => new { UserId = table.Column(type: "text", nullable: false), RoleId = table.Column(type: "text", 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, principalSchema: "identity", principalTable: "AspNetRoles", principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_AspNetUserRoles_AspNetUsers_UserId", column: x => x.UserId, principalSchema: "identity", principalTable: "AspNetUsers", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "AspNetUserTokens", schema: "identity", columns: table => new { UserId = table.Column(type: "text", nullable: false), LoginProvider = table.Column(type: "text", nullable: false), Name = table.Column(type: "text", nullable: false), Value = table.Column(type: "text", 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, principalSchema: "identity", principalTable: "AspNetUsers", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "IngestEvents", schema: "fleet", columns: table => new { Id = table.Column(type: "uuid", nullable: false), CustomerId = table.Column(type: "uuid", nullable: false), ReceivedAt = table.Column(type: "timestamp with time zone", nullable: false), BatchType = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), RowsAccepted = table.Column(type: "integer", nullable: false), RowsRejected = table.Column(type: "integer", nullable: false), BatchBytes = table.Column(type: "integer", nullable: false), ClientHwm = table.Column(type: "character varying(50)", maxLength: 50, nullable: true), TimeSpread = table.Column(type: "interval", nullable: true), Error = table.Column(type: "character varying(500)", maxLength: 500, nullable: true) }, constraints: table => { table.PrimaryKey("PK_IngestEvents", x => x.Id); table.ForeignKey( name: "FK_IngestEvents_Customers_CustomerId", column: x => x.CustomerId, principalSchema: "fleet", principalTable: "Customers", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "Sites", schema: "fleet", columns: table => new { Id = table.Column(type: "uuid", nullable: false), CustomerId = table.Column(type: "uuid", nullable: false), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), Address = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), LocalMunicipalityId = table.Column(type: "integer", nullable: true), IsActive = table.Column(type: "boolean", nullable: false), ReceivedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Sites", x => new { x.CustomerId, x.Id }); table.ForeignKey( name: "FK_Sites_Customers_CustomerId", column: x => x.CustomerId, principalSchema: "fleet", principalTable: "Customers", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "Devices", schema: "fleet", columns: table => new { Id = table.Column(type: "uuid", nullable: false), CustomerId = table.Column(type: "uuid", nullable: false), SiteId = table.Column(type: "uuid", nullable: false), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), ExternalId = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), Description = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), IsActive = table.Column(type: "boolean", nullable: false), ReceivedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Devices", x => new { x.CustomerId, x.Id }); table.ForeignKey( name: "FK_Devices_Customers_CustomerId", column: x => x.CustomerId, principalSchema: "fleet", principalTable: "Customers", principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_Devices_Sites_CustomerId_SiteId", columns: x => new { x.CustomerId, x.SiteId }, principalSchema: "fleet", principalTable: "Sites", principalColumns: new[] { "CustomerId", "Id" }, onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "PowerMeasurements", schema: "fleet", columns: table => new { Time = table.Column(type: "timestamp with time zone", nullable: false), CustomerId = table.Column(type: "uuid", nullable: false), DeviceId = table.Column(type: "uuid", nullable: false), ActivePowerKw = table.Column(type: "double precision", nullable: false), ReactivePowerKvar = table.Column(type: "double precision", nullable: true), ApparentPowerKva = table.Column(type: "double precision", nullable: true), PowerFactor = table.Column(type: "double precision", nullable: true), VoltageV = table.Column(type: "double precision", nullable: true), FrequencyHz = table.Column(type: "double precision", nullable: true), EnergyImportedKwh = table.Column(type: "double precision", nullable: true), EnergyExportedKwh = table.Column(type: "double precision", nullable: true), Source = table.Column(type: "character varying(50)", maxLength: 50, nullable: true) }, constraints: table => { table.PrimaryKey("PK_PowerMeasurements", x => new { x.Time, x.CustomerId, x.DeviceId }); table.ForeignKey( name: "FK_PowerMeasurements_Devices_CustomerId_DeviceId", columns: x => new { x.CustomerId, x.DeviceId }, principalSchema: "fleet", principalTable: "Devices", principalColumns: new[] { "CustomerId", "Id" }, onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( name: "IX_AspNetRoleClaims_RoleId", schema: "identity", table: "AspNetRoleClaims", column: "RoleId"); migrationBuilder.CreateIndex( name: "RoleNameIndex", schema: "identity", table: "AspNetRoles", column: "NormalizedName", unique: true); migrationBuilder.CreateIndex( name: "IX_AspNetUserClaims_UserId", schema: "identity", table: "AspNetUserClaims", column: "UserId"); migrationBuilder.CreateIndex( name: "IX_AspNetUserLogins_UserId", schema: "identity", table: "AspNetUserLogins", column: "UserId"); migrationBuilder.CreateIndex( name: "IX_AspNetUserRoles_RoleId", schema: "identity", table: "AspNetUserRoles", column: "RoleId"); migrationBuilder.CreateIndex( name: "EmailIndex", schema: "identity", table: "AspNetUsers", column: "NormalizedEmail"); migrationBuilder.CreateIndex( name: "UserNameIndex", schema: "identity", table: "AspNetUsers", column: "NormalizedUserName", unique: true); migrationBuilder.CreateIndex( name: "IX_Customers_Code", schema: "fleet", table: "Customers", column: "Code", unique: true); migrationBuilder.CreateIndex( name: "IX_Customers_TokenHash", schema: "fleet", table: "Customers", column: "TokenHash", unique: true); migrationBuilder.CreateIndex( name: "IX_Devices_CustomerId_SiteId", schema: "fleet", table: "Devices", columns: new[] { "CustomerId", "SiteId" }); migrationBuilder.CreateIndex( name: "IX_IngestEvents_CustomerId_ReceivedAt", schema: "fleet", table: "IngestEvents", columns: new[] { "CustomerId", "ReceivedAt" }, descending: new[] { false, true }); migrationBuilder.CreateIndex( name: "IX_PowerMeasurements_CustomerId_DeviceId_Time", schema: "fleet", table: "PowerMeasurements", columns: new[] { "CustomerId", "DeviceId", "Time" }, descending: new[] { false, false, true }); migrationBuilder.CreateIndex( name: "IX_PowerMeasurements_CustomerId_Time", schema: "fleet", table: "PowerMeasurements", columns: new[] { "CustomerId", "Time" }, descending: new[] { false, true }); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "AspNetRoleClaims", schema: "identity"); migrationBuilder.DropTable( name: "AspNetUserClaims", schema: "identity"); migrationBuilder.DropTable( name: "AspNetUserLogins", schema: "identity"); migrationBuilder.DropTable( name: "AspNetUserRoles", schema: "identity"); migrationBuilder.DropTable( name: "AspNetUserTokens", schema: "identity"); migrationBuilder.DropTable( name: "IngestEvents", schema: "fleet"); migrationBuilder.DropTable( name: "PowerMeasurements", schema: "fleet"); migrationBuilder.DropTable( name: "WhiteLabelSettings", schema: "app"); migrationBuilder.DropTable( name: "AspNetRoles", schema: "identity"); migrationBuilder.DropTable( name: "AspNetUsers", schema: "identity"); migrationBuilder.DropTable( name: "Devices", schema: "fleet"); migrationBuilder.DropTable( name: "Sites", schema: "fleet"); migrationBuilder.DropTable( name: "Customers", schema: "fleet"); } } }