From bd229cddd41c972512ca49248795dd552c38ac20 Mon Sep 17 00:00:00 2001 From: Lavardin <52592050+Lavardin@users.noreply.github.com> Date: Mon, 9 May 2022 22:38:14 -0700 Subject: [PATCH] Run migrations on server startup --- API/Program.cs | 24 +++++++++++++++++-- .../Data/Config/ProductConfiguaration.cs | 20 ++++++++++++++++ ... 20220510050050_InitialCreate.Designer.cs} | 9 +++++-- ...ate.cs => 20220510050050_InitialCreate.cs} | 8 +++---- .../Migrations/StoreContextModelSnapshot.cs | 7 +++++- Infrastructure/Data/StoreContext.cs | 6 +++++ 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 Infrastructure/Data/Config/ProductConfiguaration.cs rename Infrastructure/Data/Migrations/{20220509234932_InitialCreate.Designer.cs => 20220510050050_InitialCreate.Designer.cs} (91%) rename Infrastructure/Data/Migrations/{20220509234932_InitialCreate.cs => 20220510050050_InitialCreate.cs} (93%) diff --git a/API/Program.cs b/API/Program.cs index f606381..2719f5e 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -1,10 +1,30 @@ +using Infrastructure.Data; +using Microsoft.EntityFrameworkCore; + namespace API { public class Program { - public static void Main(string[] args) + public static async Task Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + var host = CreateHostBuilder(args).Build(); + using(var scope = host.Services.CreateScope()) + { + var services = scope.ServiceProvider; + var loggerFactory = services.GetRequiredService(); + try + { + var context = services.GetRequiredService(); + await context.Database.MigrateAsync(); + } + catch (Exception ex) + { + var logger = loggerFactory.CreateLogger(); + logger.LogError(ex, "An error occured during migration"); + } + } + + host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => diff --git a/Infrastructure/Data/Config/ProductConfiguaration.cs b/Infrastructure/Data/Config/ProductConfiguaration.cs new file mode 100644 index 0000000..f3376e5 --- /dev/null +++ b/Infrastructure/Data/Config/ProductConfiguaration.cs @@ -0,0 +1,20 @@ +using Core.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace Infrastructure.Data.Config +{ + public class ProductConfiguaration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.Property(p => p.Id).IsRequired(); + builder.Property(p => p.Name).IsRequired().HasMaxLength(100); + builder.Property(p => p.Description).IsRequired().HasMaxLength(256); + builder.Property(p => p.Price).HasColumnType("decimal(18,2)"); + builder.Property(p => p.PictureUrl).IsRequired(); + builder.HasOne(b => b.ProductBrand).WithMany().HasForeignKey(p => p.ProductBrandId); + builder.HasOne(t => t.ProductType).WithMany().HasForeignKey(p => p.ProductTypeId); + } + } +} \ No newline at end of file diff --git a/Infrastructure/Data/Migrations/20220509234932_InitialCreate.Designer.cs b/Infrastructure/Data/Migrations/20220510050050_InitialCreate.Designer.cs similarity index 91% rename from Infrastructure/Data/Migrations/20220509234932_InitialCreate.Designer.cs rename to Infrastructure/Data/Migrations/20220510050050_InitialCreate.Designer.cs index 266fe1b..30ba49a 100644 --- a/Infrastructure/Data/Migrations/20220509234932_InitialCreate.Designer.cs +++ b/Infrastructure/Data/Migrations/20220510050050_InitialCreate.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Infrastructure.Data.Migrations { [DbContext(typeof(StoreContext))] - [Migration("20220509234932_InitialCreate")] + [Migration("20220510050050_InitialCreate")] partial class InitialCreate { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -25,16 +25,21 @@ namespace Infrastructure.Data.Migrations .HasColumnType("INTEGER"); b.Property("Description") + .IsRequired() + .HasMaxLength(256) .HasColumnType("TEXT"); b.Property("Name") + .IsRequired() + .HasMaxLength(100) .HasColumnType("TEXT"); b.Property("PictureUrl") + .IsRequired() .HasColumnType("TEXT"); b.Property("Price") - .HasColumnType("TEXT"); + .HasColumnType("decimal(18,2)"); b.Property("ProductBrandId") .HasColumnType("INTEGER"); diff --git a/Infrastructure/Data/Migrations/20220509234932_InitialCreate.cs b/Infrastructure/Data/Migrations/20220510050050_InitialCreate.cs similarity index 93% rename from Infrastructure/Data/Migrations/20220509234932_InitialCreate.cs rename to Infrastructure/Data/Migrations/20220510050050_InitialCreate.cs index 3f2c51f..3a8edf5 100644 --- a/Infrastructure/Data/Migrations/20220509234932_InitialCreate.cs +++ b/Infrastructure/Data/Migrations/20220510050050_InitialCreate.cs @@ -40,10 +40,10 @@ namespace Infrastructure.Data.Migrations { Id = table.Column(type: "INTEGER", nullable: false) .Annotation("Sqlite:Autoincrement", true), - Name = table.Column(type: "TEXT", nullable: true), - Description = table.Column(type: "TEXT", nullable: true), - Price = table.Column(type: "TEXT", nullable: false), - PictureUrl = table.Column(type: "TEXT", nullable: true), + Name = table.Column(type: "TEXT", maxLength: 100, nullable: false), + Description = table.Column(type: "TEXT", maxLength: 256, nullable: false), + Price = table.Column(type: "decimal(18,2)", nullable: false), + PictureUrl = table.Column(type: "TEXT", nullable: false), ProductTypeId = table.Column(type: "INTEGER", nullable: false), ProductBrandId = table.Column(type: "INTEGER", nullable: false) }, diff --git a/Infrastructure/Data/Migrations/StoreContextModelSnapshot.cs b/Infrastructure/Data/Migrations/StoreContextModelSnapshot.cs index 3746a35..fa4012b 100644 --- a/Infrastructure/Data/Migrations/StoreContextModelSnapshot.cs +++ b/Infrastructure/Data/Migrations/StoreContextModelSnapshot.cs @@ -23,16 +23,21 @@ namespace Infrastructure.Data.Migrations .HasColumnType("INTEGER"); b.Property("Description") + .IsRequired() + .HasMaxLength(256) .HasColumnType("TEXT"); b.Property("Name") + .IsRequired() + .HasMaxLength(100) .HasColumnType("TEXT"); b.Property("PictureUrl") + .IsRequired() .HasColumnType("TEXT"); b.Property("Price") - .HasColumnType("TEXT"); + .HasColumnType("decimal(18,2)"); b.Property("ProductBrandId") .HasColumnType("INTEGER"); diff --git a/Infrastructure/Data/StoreContext.cs b/Infrastructure/Data/StoreContext.cs index bc801c5..097fbd8 100644 --- a/Infrastructure/Data/StoreContext.cs +++ b/Infrastructure/Data/StoreContext.cs @@ -1,3 +1,4 @@ +using System.Reflection; using Core.Entities; using Microsoft.EntityFrameworkCore; @@ -11,5 +12,10 @@ namespace Infrastructure.Data public DbSet Products { get; set; } public DbSet ProductBrands { get; set; } public DbSet ProductTypes { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); + } } } \ No newline at end of file