From 6d28773c0672dafe042077f8733be483172d1e38 Mon Sep 17 00:00:00 2001 From: Motimat Date: Wed, 24 Sep 2025 19:12:58 +0330 Subject: [PATCH] changing database to postgres --- .env | 4 +- Directory.Packages.props | 1 + .../AppSettings/ConnectionOptions.cs | 5 +- .../Data/Context/BaseDbContext.cs | 4 +- .../Extensions/EntityTypeBuilderExtensions.cs | 3 + .../Data/Extensions/ModelBuilderExtensions.cs | 24 +++++ src/Shop.Infrastructure/Data/UnitOfWork.cs | 11 ++- .../ServicesCollectionExtensions.cs | 2 +- .../20230207164309_Initial.Designer.cs | 89 ------------------- .../Migrations/20230207164309_Initial.cs | 39 -------- .../Migrations/20241128202054_EF_Update.cs | 39 -------- ....cs => 20250924110748_Initial.Designer.cs} | 28 +++--- .../Migrations/20250924110748_Initial.cs | 44 +++++++++ .../20230226145437_EventStore.Designer.cs | 58 ------------ .../EventStoreDb/20230226145437_EventStore.cs | 31 ------- .../EventStoreDb/20241128202230_EF_Update.cs | 43 --------- .../20250924110826_Initial.Designer.cs} | 25 +++--- .../20250924110826_Initial.cs | 37 ++++++++ .../EventStoreDbContextModelSnapshot.cs | 21 +++-- .../Migrations/WriteDbContextModelSnapshot.cs | 24 +++-- src/Shop.PublicApi/Program.cs | 1 + src/Shop.PublicApi/Shop.PublicApi.csproj | 1 + src/Shop.PublicApi/appsettings.json | 6 ++ 23 files changed, 180 insertions(+), 360 deletions(-) delete mode 100644 src/Shop.PublicApi/Migrations/20230207164309_Initial.Designer.cs delete mode 100644 src/Shop.PublicApi/Migrations/20230207164309_Initial.cs delete mode 100644 src/Shop.PublicApi/Migrations/20241128202054_EF_Update.cs rename src/Shop.PublicApi/Migrations/{20241128202054_EF_Update.Designer.cs => 20250924110748_Initial.Designer.cs} (75%) create mode 100644 src/Shop.PublicApi/Migrations/20250924110748_Initial.cs delete mode 100644 src/Shop.PublicApi/Migrations/EventStoreDb/20230226145437_EventStore.Designer.cs delete mode 100644 src/Shop.PublicApi/Migrations/EventStoreDb/20230226145437_EventStore.cs delete mode 100644 src/Shop.PublicApi/Migrations/EventStoreDb/20241128202230_EF_Update.cs rename src/Shop.PublicApi/Migrations/{EventStoreDb/20241128202230_EF_Update.Designer.cs => EventStoreMigrations/20250924110826_Initial.Designer.cs} (69%) create mode 100644 src/Shop.PublicApi/Migrations/EventStoreMigrations/20250924110826_Initial.cs rename src/Shop.PublicApi/Migrations/{EventStoreDb => EventStoreMigrations}/EventStoreDbContextModelSnapshot.cs (71%) diff --git a/.env b/.env index 422998e..47a5ae7 100644 --- a/.env +++ b/.env @@ -1,6 +1,6 @@ -MSSQL_SA_PASSWORD=YOUR_STRONG_!Passw0rd +MSSQL_SA_PASSWORD=Programmer.369 MSSQL_PORT=1433 -REDIS_PASSWORD=YOUR_STRONG_!Passw0rd +REDIS_PASSWORD=Programmer.369 REDIS_PORT=6379 MONGO_PORT=27017 ASPNETCORE_ENVIRONMENT=Development \ No newline at end of file diff --git a/Directory.Packages.props b/Directory.Packages.props index 91663d8..8f83ebf 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -55,6 +55,7 @@ + diff --git a/src/Shop.Core/AppSettings/ConnectionOptions.cs b/src/Shop.Core/AppSettings/ConnectionOptions.cs index 75580c4..feca483 100644 --- a/src/Shop.Core/AppSettings/ConnectionOptions.cs +++ b/src/Shop.Core/AppSettings/ConnectionOptions.cs @@ -8,8 +8,11 @@ public sealed class ConnectionOptions : IAppOptions { static string IAppOptions.ConfigSectionPath => "ConnectionStrings"; + // [Required] + // public string SqlConnection { get; private init; } + [Required] - public string SqlConnection { get; private init; } + public string PostgresConnection { get; private init; } [Required] public string NoSqlConnection { get; private init; } diff --git a/src/Shop.Infrastructure/Data/Context/BaseDbContext.cs b/src/Shop.Infrastructure/Data/Context/BaseDbContext.cs index 8f20a6e..643d55a 100644 --- a/src/Shop.Infrastructure/Data/Context/BaseDbContext.cs +++ b/src/Shop.Infrastructure/Data/Context/BaseDbContext.cs @@ -7,7 +7,7 @@ namespace Shop.Infrastructure.Data.Context; public abstract class BaseDbContext(DbContextOptions dbOptions) : DbContext(dbOptions) where TContext : DbContext { - private const string Collation = "Latin1_General_CI_AI"; + //private const string Collation = "en_US.UTF-8"; public override ChangeTracker ChangeTracker { @@ -33,7 +33,7 @@ public abstract class BaseDbContext(DbContextOptions dbOptio protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder - .UseCollation(Collation) + // .UseCollation(Collation) .RemoveCascadeDeleteConvention(); base.OnModelCreating(modelBuilder); diff --git a/src/Shop.Infrastructure/Data/Extensions/EntityTypeBuilderExtensions.cs b/src/Shop.Infrastructure/Data/Extensions/EntityTypeBuilderExtensions.cs index d2496e5..79d7398 100644 --- a/src/Shop.Infrastructure/Data/Extensions/EntityTypeBuilderExtensions.cs +++ b/src/Shop.Infrastructure/Data/Extensions/EntityTypeBuilderExtensions.cs @@ -26,5 +26,8 @@ internal static class EntityTypeBuilderExtensions // Ignores the DomainEvents property for the entity. builder .Ignore(entity => entity.DomainEvents); + + + } } \ No newline at end of file diff --git a/src/Shop.Infrastructure/Data/Extensions/ModelBuilderExtensions.cs b/src/Shop.Infrastructure/Data/Extensions/ModelBuilderExtensions.cs index 4b07f4e..8298887 100644 --- a/src/Shop.Infrastructure/Data/Extensions/ModelBuilderExtensions.cs +++ b/src/Shop.Infrastructure/Data/Extensions/ModelBuilderExtensions.cs @@ -1,5 +1,7 @@ +using System; using System.Linq; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Shop.Infrastructure.Data.Extensions; @@ -21,5 +23,27 @@ internal static class ModelBuilderExtensions // Change the delete behavior of each foreign key to restrict foreach (var fk in foreignKeys) fk.DeleteBehavior = DeleteBehavior.Restrict; + + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) + { + foreach (var property in entityType.GetProperties()) + { + if (property.ClrType == typeof(DateTime)) + { + property.SetValueConverter(new ValueConverter( + v => v.Kind == DateTimeKind.Utc ? v : v.ToUniversalTime(), + v => DateTime.SpecifyKind(v, DateTimeKind.Utc) + )); + } + + else if (property.ClrType == typeof(DateTime?)) + { + property.SetValueConverter(new ValueConverter( + v => v.HasValue ? (v.Value.Kind == DateTimeKind.Utc ? v : v.Value.ToUniversalTime()) : v, + v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc) : v + )); + } + } + } } } \ No newline at end of file diff --git a/src/Shop.Infrastructure/Data/UnitOfWork.cs b/src/Shop.Infrastructure/Data/UnitOfWork.cs index e3c7a4b..2cd88c0 100644 --- a/src/Shop.Infrastructure/Data/UnitOfWork.cs +++ b/src/Shop.Infrastructure/Data/UnitOfWork.cs @@ -33,10 +33,13 @@ internal sealed class UnitOfWork( logger.LogInformation("----- Begin transaction: '{TransactionId}'", transaction.TransactionId); + IReadOnlyList domainEvents = Array.Empty(); + IReadOnlyList eventStores = Array.Empty(); + try { // Getting the domain events and event stores from the tracked entities in the EF Core context. - var (domainEvents, eventStores) = BeforeSaveChanges(); + (domainEvents, eventStores) = BeforeSaveChanges(); var rowsAffected = await writeDbContext.SaveChangesAsync(); @@ -44,8 +47,7 @@ internal sealed class UnitOfWork( await transaction.CommitAsync(); - // Triggering the events and saving the stores. - await AfterSaveChangesAsync(domainEvents, eventStores); + logger.LogInformation( "----- Transaction successfully confirmed: '{TransactionId}', Rows Affected: {RowsAffected}", @@ -64,6 +66,9 @@ internal sealed class UnitOfWork( throw; } + + // Triggering the events and saving the stores. + await AfterSaveChangesAsync(domainEvents, eventStores); }); } diff --git a/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs b/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs index 1c25df6..54cb51c 100644 --- a/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs +++ b/src/Shop.PublicApi/Extensions/ServicesCollectionExtensions.cs @@ -89,7 +89,7 @@ internal static class ServicesCollectionExtensions var envIsDevelopment = environment.IsDevelopment(); optionsBuilder - .UseSqlServer(connectionOptions.SqlConnection, sqlServerOptions => + .UseNpgsql(connectionOptions.PostgresConnection, sqlServerOptions => { sqlServerOptions .MigrationsAssembly(DbMigrationAssemblyName) diff --git a/src/Shop.PublicApi/Migrations/20230207164309_Initial.Designer.cs b/src/Shop.PublicApi/Migrations/20230207164309_Initial.Designer.cs deleted file mode 100644 index cf8975e..0000000 --- a/src/Shop.PublicApi/Migrations/20230207164309_Initial.Designer.cs +++ /dev/null @@ -1,89 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Shop.Infrastructure.Data.Context; - -#nullable disable - -namespace Shop.PublicApi.Migrations -{ - [DbContext(typeof(WriteDbContext))] - [Migration("20230207164309_Initial")] - partial class Initial - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .UseCollation("Latin1_General_CI_AI") - .HasAnnotation("ProductVersion", "7.0.2") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("Shop.Domain.Entities.Customer.Customer", b => - { - b.Property("Id") - .HasColumnType("uniqueidentifier"); - - b.Property("DateOfBirth") - .HasColumnType("DATE"); - - b.Property("FirstName") - .IsRequired() - .HasMaxLength(100) - .IsUnicode(false) - .HasColumnType("varchar(100)"); - - b.Property("Gender") - .IsRequired() - .HasMaxLength(6) - .IsUnicode(false) - .HasColumnType("varchar(6)"); - - b.Property("LastName") - .IsRequired() - .HasMaxLength(100) - .IsUnicode(false) - .HasColumnType("varchar(100)"); - - b.HasKey("Id"); - - b.ToTable("Customers"); - }); - - modelBuilder.Entity("Shop.Domain.Entities.Customer.Customer", b => - { - b.OwnsOne("Shop.Domain.ValueObjects.Email", "Email", b1 => - { - b1.Property("CustomerId") - .HasColumnType("uniqueidentifier"); - - b1.Property("Address") - .IsRequired() - .HasMaxLength(254) - .IsUnicode(false) - .HasColumnType("varchar(254)") - .HasColumnName("Email"); - - b1.HasKey("CustomerId"); - - b1.HasIndex("Address") - .IsUnique() - .HasFilter("[Email] IS NOT NULL"); - - b1.ToTable("Customers"); - - b1.WithOwner() - .HasForeignKey("CustomerId"); - }); - - b.Navigation("Email"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/Shop.PublicApi/Migrations/20230207164309_Initial.cs b/src/Shop.PublicApi/Migrations/20230207164309_Initial.cs deleted file mode 100644 index 81e07d2..0000000 --- a/src/Shop.PublicApi/Migrations/20230207164309_Initial.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace Shop.PublicApi.Migrations; - -/// -public partial class Initial : Migration -{ - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Customers", - columns: table => new - { - Id = table.Column(type: "uniqueidentifier", nullable: false), - FirstName = table.Column(type: "varchar(100)", unicode: false, maxLength: 100, nullable: false), - LastName = table.Column(type: "varchar(100)", unicode: false, maxLength: 100, nullable: false), - Gender = table.Column(type: "varchar(6)", unicode: false, maxLength: 6, nullable: false), - Email = table.Column(type: "varchar(254)", unicode: false, maxLength: 254, nullable: true), - DateOfBirth = table.Column(type: "DATE", nullable: false) - }, - constraints: table => table.PrimaryKey("PK_Customers", x => x.Id)); - - migrationBuilder.CreateIndex( - name: "IX_Customers_Email", - table: "Customers", - column: "Email", - unique: true, - filter: "[Email] IS NOT NULL"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Customers"); - } -} \ No newline at end of file diff --git a/src/Shop.PublicApi/Migrations/20241128202054_EF_Update.cs b/src/Shop.PublicApi/Migrations/20241128202054_EF_Update.cs deleted file mode 100644 index 3b2de85..0000000 --- a/src/Shop.PublicApi/Migrations/20241128202054_EF_Update.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace Shop.PublicApi.Migrations; - -/// -public partial class EF_Update : Migration -{ - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.AlterColumn( - name: "Gender", - table: "Customers", - type: "nvarchar(6)", - maxLength: 6, - nullable: false, - oldClrType: typeof(string), - oldType: "varchar(6)", - oldUnicode: false, - oldMaxLength: 6); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.AlterColumn( - name: "Gender", - table: "Customers", - type: "varchar(6)", - unicode: false, - maxLength: 6, - nullable: false, - oldClrType: typeof(string), - oldType: "nvarchar(6)", - oldMaxLength: 6); - } -} \ No newline at end of file diff --git a/src/Shop.PublicApi/Migrations/20241128202054_EF_Update.Designer.cs b/src/Shop.PublicApi/Migrations/20250924110748_Initial.Designer.cs similarity index 75% rename from src/Shop.PublicApi/Migrations/20241128202054_EF_Update.Designer.cs rename to src/Shop.PublicApi/Migrations/20250924110748_Initial.Designer.cs index 0f0888a..d491c73 100644 --- a/src/Shop.PublicApi/Migrations/20241128202054_EF_Update.Designer.cs +++ b/src/Shop.PublicApi/Migrations/20250924110748_Initial.Designer.cs @@ -2,9 +2,9 @@ using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; using Shop.Infrastructure.Data.Context; #nullable disable @@ -12,24 +12,23 @@ using Shop.Infrastructure.Data.Context; namespace Shop.PublicApi.Migrations { [DbContext(typeof(WriteDbContext))] - [Migration("20241128202054_EF_Update")] - partial class EF_Update + [Migration("20250924110748_Initial")] + partial class Initial { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder - .UseCollation("Latin1_General_CI_AI") - .HasAnnotation("ProductVersion", "9.0.0") - .HasAnnotation("Relational:MaxIdentifierLength", 128); + .HasAnnotation("ProductVersion", "9.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); modelBuilder.Entity("Shop.Domain.Entities.CustomerAggregate.Customer", b => { b.Property("Id") - .HasColumnType("uniqueidentifier"); + .HasColumnType("uuid"); b.Property("DateOfBirth") .HasColumnType("DATE"); @@ -38,18 +37,18 @@ namespace Shop.PublicApi.Migrations .IsRequired() .HasMaxLength(100) .IsUnicode(false) - .HasColumnType("varchar(100)"); + .HasColumnType("character varying(100)"); b.Property("Gender") .IsRequired() .HasMaxLength(6) - .HasColumnType("nvarchar(6)"); + .HasColumnType("character varying(6)"); b.Property("LastName") .IsRequired() .HasMaxLength(100) .IsUnicode(false) - .HasColumnType("varchar(100)"); + .HasColumnType("character varying(100)"); b.HasKey("Id"); @@ -61,20 +60,19 @@ namespace Shop.PublicApi.Migrations b.OwnsOne("Shop.Domain.ValueObjects.Email", "Email", b1 => { b1.Property("CustomerId") - .HasColumnType("uniqueidentifier"); + .HasColumnType("uuid"); b1.Property("Address") .IsRequired() .HasMaxLength(254) .IsUnicode(false) - .HasColumnType("varchar(254)") + .HasColumnType("character varying(254)") .HasColumnName("Email"); b1.HasKey("CustomerId"); b1.HasIndex("Address") - .IsUnique() - .HasFilter("[Email] IS NOT NULL"); + .IsUnique(); b1.ToTable("Customers"); diff --git a/src/Shop.PublicApi/Migrations/20250924110748_Initial.cs b/src/Shop.PublicApi/Migrations/20250924110748_Initial.cs new file mode 100644 index 0000000..811ca9b --- /dev/null +++ b/src/Shop.PublicApi/Migrations/20250924110748_Initial.cs @@ -0,0 +1,44 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Shop.PublicApi.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Customers", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + FirstName = table.Column(type: "character varying(100)", unicode: false, maxLength: 100, nullable: false), + LastName = table.Column(type: "character varying(100)", unicode: false, maxLength: 100, nullable: false), + Gender = table.Column(type: "character varying(6)", maxLength: 6, nullable: false), + Email = table.Column(type: "character varying(254)", unicode: false, maxLength: 254, nullable: true), + DateOfBirth = table.Column(type: "DATE", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Customers", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Customers_Email", + table: "Customers", + column: "Email", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Customers"); + } + } +} diff --git a/src/Shop.PublicApi/Migrations/EventStoreDb/20230226145437_EventStore.Designer.cs b/src/Shop.PublicApi/Migrations/EventStoreDb/20230226145437_EventStore.Designer.cs deleted file mode 100644 index c71d037..0000000 --- a/src/Shop.PublicApi/Migrations/EventStoreDb/20230226145437_EventStore.Designer.cs +++ /dev/null @@ -1,58 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Shop.Infrastructure.Data.Context; - -#nullable disable - -namespace Shop.PublicApi.Migrations.EventStoreDb -{ - [DbContext(typeof(EventStoreDbContext))] - [Migration("20230226145437_EventStore")] - partial class EventStore - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .UseCollation("Latin1_General_CI_AI") - .HasAnnotation("ProductVersion", "7.0.3") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("Shop.Core.Events.EventStore", b => - { - b.Property("Id") - .HasColumnType("uniqueidentifier"); - - b.Property("AggregateId") - .HasColumnType("uniqueidentifier"); - - b.Property("Data") - .IsRequired() - .IsUnicode(false) - .HasColumnType("VARCHAR(MAX)") - .HasComment("JSON serialized event"); - - b.Property("MessageType") - .IsRequired() - .HasMaxLength(100) - .IsUnicode(false) - .HasColumnType("varchar(100)"); - - b.Property("OccurredOn") - .HasColumnType("datetime2") - .HasColumnName("CreatedAt"); - - b.HasKey("Id"); - - b.ToTable("EventStores"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/Shop.PublicApi/Migrations/EventStoreDb/20230226145437_EventStore.cs b/src/Shop.PublicApi/Migrations/EventStoreDb/20230226145437_EventStore.cs deleted file mode 100644 index 8fb765d..0000000 --- a/src/Shop.PublicApi/Migrations/EventStoreDb/20230226145437_EventStore.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace Shop.PublicApi.Migrations.EventStoreDb; - -/// -public partial class EventStore : Migration -{ - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "EventStores", - columns: table => new - { - Id = table.Column(type: "uniqueidentifier", nullable: false), - Data = table.Column(type: "VARCHAR(MAX)", unicode: false, nullable: false, comment: "JSON serialized event"), - MessageType = table.Column(type: "varchar(100)", unicode: false, maxLength: 100, nullable: false), - AggregateId = table.Column(type: "uniqueidentifier", nullable: false), - CreatedAt = table.Column(type: "datetime2", nullable: false) - }, - constraints: table => table.PrimaryKey("PK_EventStores", x => x.Id)); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "EventStores"); - } -} \ No newline at end of file diff --git a/src/Shop.PublicApi/Migrations/EventStoreDb/20241128202230_EF_Update.cs b/src/Shop.PublicApi/Migrations/EventStoreDb/20241128202230_EF_Update.cs deleted file mode 100644 index ee5edb8..0000000 --- a/src/Shop.PublicApi/Migrations/EventStoreDb/20241128202230_EF_Update.cs +++ /dev/null @@ -1,43 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace Shop.PublicApi.Migrations.EventStoreDb; - -/// -public partial class EF_Update : Migration -{ - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.AlterColumn( - name: "Data", - table: "EventStores", - type: "varchar(255)", - unicode: false, - maxLength: 255, - nullable: false, - oldClrType: typeof(string), - oldType: "VARCHAR(MAX)", - oldUnicode: false, - comment: "JSON serialized event", - oldComment: "JSON serialized event"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.AlterColumn( - name: "Data", - table: "EventStores", - type: "VARCHAR(MAX)", - unicode: false, - nullable: false, - oldClrType: typeof(string), - oldType: "varchar(255)", - oldUnicode: false, - oldMaxLength: 255, - comment: "JSON serialized event", - oldComment: "JSON serialized event"); - } -} \ No newline at end of file diff --git a/src/Shop.PublicApi/Migrations/EventStoreDb/20241128202230_EF_Update.Designer.cs b/src/Shop.PublicApi/Migrations/EventStoreMigrations/20250924110826_Initial.Designer.cs similarity index 69% rename from src/Shop.PublicApi/Migrations/EventStoreDb/20241128202230_EF_Update.Designer.cs rename to src/Shop.PublicApi/Migrations/EventStoreMigrations/20250924110826_Initial.Designer.cs index f4cf229..c626236 100644 --- a/src/Shop.PublicApi/Migrations/EventStoreDb/20241128202230_EF_Update.Designer.cs +++ b/src/Shop.PublicApi/Migrations/EventStoreMigrations/20250924110826_Initial.Designer.cs @@ -2,53 +2,52 @@ using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; using Shop.Infrastructure.Data.Context; #nullable disable -namespace Shop.PublicApi.Migrations.EventStoreDb +namespace Shop.PublicApi.Migrations.EventStoreMigrations { [DbContext(typeof(EventStoreDbContext))] - [Migration("20241128202230_EF_Update")] - partial class EF_Update + [Migration("20250924110826_Initial")] + partial class Initial { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder - .UseCollation("Latin1_General_CI_AI") - .HasAnnotation("ProductVersion", "9.0.0") - .HasAnnotation("Relational:MaxIdentifierLength", 128); + .HasAnnotation("ProductVersion", "9.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); modelBuilder.Entity("Shop.Core.SharedKernel.EventStore", b => { b.Property("Id") - .HasColumnType("uniqueidentifier"); + .HasColumnType("uuid"); b.Property("AggregateId") - .HasColumnType("uniqueidentifier"); + .HasColumnType("uuid"); b.Property("Data") .IsRequired() .HasMaxLength(255) .IsUnicode(false) - .HasColumnType("varchar(255)") + .HasColumnType("character varying(255)") .HasComment("JSON serialized event"); b.Property("MessageType") .IsRequired() .HasMaxLength(100) .IsUnicode(false) - .HasColumnType("varchar(100)"); + .HasColumnType("character varying(100)"); b.Property("OccurredOn") - .HasColumnType("datetime2") + .HasColumnType("timestamp with time zone") .HasColumnName("CreatedAt"); b.HasKey("Id"); diff --git a/src/Shop.PublicApi/Migrations/EventStoreMigrations/20250924110826_Initial.cs b/src/Shop.PublicApi/Migrations/EventStoreMigrations/20250924110826_Initial.cs new file mode 100644 index 0000000..86e3a9b --- /dev/null +++ b/src/Shop.PublicApi/Migrations/EventStoreMigrations/20250924110826_Initial.cs @@ -0,0 +1,37 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Shop.PublicApi.Migrations.EventStoreMigrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "EventStores", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Data = table.Column(type: "character varying(255)", unicode: false, maxLength: 255, nullable: false, comment: "JSON serialized event"), + MessageType = table.Column(type: "character varying(100)", unicode: false, maxLength: 100, nullable: false), + AggregateId = table.Column(type: "uuid", nullable: false), + CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_EventStores", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "EventStores"); + } + } +} diff --git a/src/Shop.PublicApi/Migrations/EventStoreDb/EventStoreDbContextModelSnapshot.cs b/src/Shop.PublicApi/Migrations/EventStoreMigrations/EventStoreDbContextModelSnapshot.cs similarity index 71% rename from src/Shop.PublicApi/Migrations/EventStoreDb/EventStoreDbContextModelSnapshot.cs rename to src/Shop.PublicApi/Migrations/EventStoreMigrations/EventStoreDbContextModelSnapshot.cs index 6f6d371..1dbda5c 100644 --- a/src/Shop.PublicApi/Migrations/EventStoreDb/EventStoreDbContextModelSnapshot.cs +++ b/src/Shop.PublicApi/Migrations/EventStoreMigrations/EventStoreDbContextModelSnapshot.cs @@ -2,13 +2,13 @@ using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; using Shop.Infrastructure.Data.Context; #nullable disable -namespace Shop.PublicApi.Migrations.EventStoreDb +namespace Shop.PublicApi.Migrations.EventStoreMigrations { [DbContext(typeof(EventStoreDbContext))] partial class EventStoreDbContextModelSnapshot : ModelSnapshot @@ -17,35 +17,34 @@ namespace Shop.PublicApi.Migrations.EventStoreDb { #pragma warning disable 612, 618 modelBuilder - .UseCollation("Latin1_General_CI_AI") - .HasAnnotation("ProductVersion", "9.0.0") - .HasAnnotation("Relational:MaxIdentifierLength", 128); + .HasAnnotation("ProductVersion", "9.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); modelBuilder.Entity("Shop.Core.SharedKernel.EventStore", b => { b.Property("Id") - .HasColumnType("uniqueidentifier"); + .HasColumnType("uuid"); b.Property("AggregateId") - .HasColumnType("uniqueidentifier"); + .HasColumnType("uuid"); b.Property("Data") .IsRequired() .HasMaxLength(255) .IsUnicode(false) - .HasColumnType("varchar(255)") + .HasColumnType("character varying(255)") .HasComment("JSON serialized event"); b.Property("MessageType") .IsRequired() .HasMaxLength(100) .IsUnicode(false) - .HasColumnType("varchar(100)"); + .HasColumnType("character varying(100)"); b.Property("OccurredOn") - .HasColumnType("datetime2") + .HasColumnType("timestamp with time zone") .HasColumnName("CreatedAt"); b.HasKey("Id"); diff --git a/src/Shop.PublicApi/Migrations/WriteDbContextModelSnapshot.cs b/src/Shop.PublicApi/Migrations/WriteDbContextModelSnapshot.cs index 74fb547..d301b8f 100644 --- a/src/Shop.PublicApi/Migrations/WriteDbContextModelSnapshot.cs +++ b/src/Shop.PublicApi/Migrations/WriteDbContextModelSnapshot.cs @@ -2,8 +2,8 @@ using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; using Shop.Infrastructure.Data.Context; #nullable disable @@ -17,16 +17,15 @@ namespace Shop.PublicApi.Migrations { #pragma warning disable 612, 618 modelBuilder - .UseCollation("Latin1_General_CI_AI") - .HasAnnotation("ProductVersion", "9.0.0") - .HasAnnotation("Relational:MaxIdentifierLength", 128); + .HasAnnotation("ProductVersion", "9.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); modelBuilder.Entity("Shop.Domain.Entities.CustomerAggregate.Customer", b => { b.Property("Id") - .HasColumnType("uniqueidentifier"); + .HasColumnType("uuid"); b.Property("DateOfBirth") .HasColumnType("DATE"); @@ -35,18 +34,18 @@ namespace Shop.PublicApi.Migrations .IsRequired() .HasMaxLength(100) .IsUnicode(false) - .HasColumnType("varchar(100)"); + .HasColumnType("character varying(100)"); b.Property("Gender") .IsRequired() .HasMaxLength(6) - .HasColumnType("nvarchar(6)"); + .HasColumnType("character varying(6)"); b.Property("LastName") .IsRequired() .HasMaxLength(100) .IsUnicode(false) - .HasColumnType("varchar(100)"); + .HasColumnType("character varying(100)"); b.HasKey("Id"); @@ -58,20 +57,19 @@ namespace Shop.PublicApi.Migrations b.OwnsOne("Shop.Domain.ValueObjects.Email", "Email", b1 => { b1.Property("CustomerId") - .HasColumnType("uniqueidentifier"); + .HasColumnType("uuid"); b1.Property("Address") .IsRequired() .HasMaxLength(254) .IsUnicode(false) - .HasColumnType("varchar(254)") + .HasColumnType("character varying(254)") .HasColumnName("Email"); b1.HasKey("CustomerId"); b1.HasIndex("Address") - .IsUnique() - .HasFilter("[Email] IS NOT NULL"); + .IsUnique(); b1.ToTable("Customers"); diff --git a/src/Shop.PublicApi/Program.cs b/src/Shop.PublicApi/Program.cs index 3838d0f..59526af 100644 --- a/src/Shop.PublicApi/Program.cs +++ b/src/Shop.PublicApi/Program.cs @@ -1,3 +1,4 @@ +using System; using System.Globalization; using System.IO.Compression; using Asp.Versioning; diff --git a/src/Shop.PublicApi/Shop.PublicApi.csproj b/src/Shop.PublicApi/Shop.PublicApi.csproj index ad9789e..47fb87d 100644 --- a/src/Shop.PublicApi/Shop.PublicApi.csproj +++ b/src/Shop.PublicApi/Shop.PublicApi.csproj @@ -40,6 +40,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Shop.PublicApi/appsettings.json b/src/Shop.PublicApi/appsettings.json index 3408cda..7aa0f33 100644 --- a/src/Shop.PublicApi/appsettings.json +++ b/src/Shop.PublicApi/appsettings.json @@ -6,5 +6,11 @@ "Microsoft.Hosting.Lifetime": "Information" } }, + "ConnectionStrings": { + "PostgresConnection" : "Host=localhost;Port=5432;Database=ShopDDD;Username=mmdi;Password=mmdi", + "SqlConnection": "Server=localhost,1433;Database=ShopContext;User Id=sa;Password=Programmer.369;TrustServerCertificate=True;", + "NoSqlConnection" : "mongodb://localhost:27017", + "CacheConnection" : "localhost:6379,password=Programmer.369,abortConnect=false" + }, "AllowedHosts": "*" }