diff --git a/Directory.Build.props b/Directory.Build.props index c1665f5..3a56b69 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - - + + - 3.200.0 + 3.201.0 $(VersionSuffix)-$(BuildNumber) diff --git a/VirtoCommerce.ImportModule.sln b/VirtoCommerce.ImportModule.sln index 97c5198..04b65ac 100644 --- a/VirtoCommerce.ImportModule.sln +++ b/VirtoCommerce.ImportModule.sln @@ -28,6 +28,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtoCommerce.ImportModule.Data.MySql", "src\VirtoCommerce.ImportModule.Data.MySql\VirtoCommerce.ImportModule.Data.MySql.csproj", "{4880C5C0-9001-4F9E-AD7C-99B70E474A9D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtoCommerce.ImportModule.Data.PostgreSql", "src\VirtoCommerce.ImportModule.Data.PostgreSql\VirtoCommerce.ImportModule.Data.PostgreSql.csproj", "{4B6ABAC0-1229-419D-B15B-0A3B54388AC0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtoCommerce.ImportModule.Data.SqlServer", "src\VirtoCommerce.ImportModule.Data.SqlServer\VirtoCommerce.ImportModule.Data.SqlServer.csproj", "{67E9E646-3222-4CA3-8CED-5FDCB2C5DEFA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -58,6 +64,18 @@ Global {866C6093-7B23-473D-86AF-F5FD59970747}.Debug|Any CPU.Build.0 = Debug|Any CPU {866C6093-7B23-473D-86AF-F5FD59970747}.Release|Any CPU.ActiveCfg = Release|Any CPU {866C6093-7B23-473D-86AF-F5FD59970747}.Release|Any CPU.Build.0 = Release|Any CPU + {4880C5C0-9001-4F9E-AD7C-99B70E474A9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4880C5C0-9001-4F9E-AD7C-99B70E474A9D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4880C5C0-9001-4F9E-AD7C-99B70E474A9D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4880C5C0-9001-4F9E-AD7C-99B70E474A9D}.Release|Any CPU.Build.0 = Release|Any CPU + {4B6ABAC0-1229-419D-B15B-0A3B54388AC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B6ABAC0-1229-419D-B15B-0A3B54388AC0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B6ABAC0-1229-419D-B15B-0A3B54388AC0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B6ABAC0-1229-419D-B15B-0A3B54388AC0}.Release|Any CPU.Build.0 = Release|Any CPU + {67E9E646-3222-4CA3-8CED-5FDCB2C5DEFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {67E9E646-3222-4CA3-8CED-5FDCB2C5DEFA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {67E9E646-3222-4CA3-8CED-5FDCB2C5DEFA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {67E9E646-3222-4CA3-8CED-5FDCB2C5DEFA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -69,9 +87,12 @@ Global {9855708F-B80A-4B01-BA46-5932B267D22E} = {CFC1B42A-2604-4030-BD28-7706B47C6365} {6F6272B0-9C4F-4166-8942-BE39002194DE} = {F4A093B3-4F51-44FC-9A2E-18BE17F7541A} {866C6093-7B23-473D-86AF-F5FD59970747} = {F4A093B3-4F51-44FC-9A2E-18BE17F7541A} + {4880C5C0-9001-4F9E-AD7C-99B70E474A9D} = {CFC1B42A-2604-4030-BD28-7706B47C6365} + {4B6ABAC0-1229-419D-B15B-0A3B54388AC0} = {CFC1B42A-2604-4030-BD28-7706B47C6365} + {67E9E646-3222-4CA3-8CED-5FDCB2C5DEFA} = {CFC1B42A-2604-4030-BD28-7706B47C6365} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {46EEF8CA-0424-44E1-A7C6-AC27D8797DB5} 882C72C9-3D60-40B4-8DF5-E8630BD7511A = {882C72C9-3D60-40B4-8DF5-E8630BD7511A} + SolutionGuid = {46EEF8CA-0424-44E1-A7C6-AC27D8797DB5} EndGlobalSection EndGlobal diff --git a/src/VirtoCommerce.ImportModule.Data.MySql/DbContextOptionsBuilderExtensions.cs b/src/VirtoCommerce.ImportModule.Data.MySql/DbContextOptionsBuilderExtensions.cs new file mode 100644 index 0000000..b132980 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.MySql/DbContextOptionsBuilderExtensions.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; + +namespace VirtoCommerce.ImportModule.Data.MySql; + +public static class DbContextOptionsBuilderExtensions +{ + /// + /// Configures the context to use MySql. + /// + public static DbContextOptionsBuilder UseMySqlDatabase(this DbContextOptionsBuilder builder, string connectionString) => + builder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), db => db + .MigrationsAssembly(typeof(MySqlDbContextFactory).Assembly.GetName().Name)); +} diff --git a/src/VirtoCommerce.ImportModule.Data.MySql/Migrations/20230711152148_Initial.Designer.cs b/src/VirtoCommerce.ImportModule.Data.MySql/Migrations/20230711152148_Initial.Designer.cs new file mode 100644 index 0000000..ad952ac --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.MySql/Migrations/20230711152148_Initial.Designer.cs @@ -0,0 +1,150 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using VirtoCommerce.ImportModule.Data.Repositories; + +#nullable disable + +namespace VirtoCommerce.ImportModule.Data.MySql.Migrations +{ + [DbContext(typeof(ImportDbContext))] + [Migration("20230711152148_Initial")] + partial class Initial + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("VirtoCommerce.ImportModule.Data.Models.ImportProfileEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("CreatedBy") + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("CreatedDate") + .HasColumnType("datetime(6)"); + + b.Property("DataImporterType") + .IsRequired() + .HasMaxLength(254) + .HasColumnType("varchar(254)"); + + b.Property("ModifiedBy") + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("ModifiedDate") + .HasColumnType("datetime(6)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("varchar(1024)"); + + b.Property("UserId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("UserName") + .IsRequired() + .HasMaxLength(254) + .HasColumnType("varchar(254)"); + + b.HasKey("Id"); + + b.HasIndex("UserId", "Name"); + + b.ToTable("ImportProfile", (string)null); + }); + + modelBuilder.Entity("VirtoCommerce.ImportModule.Data.Models.ImportRunHistoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("CreatedBy") + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("CreatedDate") + .HasColumnType("datetime(6)"); + + b.Property("Errors") + .HasColumnType("longtext"); + + b.Property("ErrorsCount") + .HasColumnType("int"); + + b.Property("Executed") + .HasColumnType("datetime(6)"); + + b.Property("FileUrl") + .HasMaxLength(2048) + .HasColumnType("varchar(2048)"); + + b.Property("Finished") + .HasColumnType("datetime(6)"); + + b.Property("JobId") + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("ModifiedBy") + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("ModifiedDate") + .HasColumnType("datetime(6)"); + + b.Property("ProcessedCount") + .HasColumnType("int"); + + b.Property("ProfileId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("ProfileName") + .HasMaxLength(1024) + .HasColumnType("varchar(1024)"); + + b.Property("ReportUrl") + .HasMaxLength(2048) + .HasColumnType("varchar(2048)"); + + b.Property("TotalCount") + .HasColumnType("int"); + + b.Property("UserId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.HasKey("Id"); + + b.HasIndex("JobId"); + + b.HasIndex("ProfileId"); + + b.HasIndex("UserId"); + + b.ToTable("ImportRunHistory", (string)null); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/VirtoCommerce.ImportModule.Data.MySql/Migrations/20230711152148_Initial.cs b/src/VirtoCommerce.ImportModule.Data.MySql/Migrations/20230711152148_Initial.cs new file mode 100644 index 0000000..83aa859 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.MySql/Migrations/20230711152148_Initial.cs @@ -0,0 +1,110 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace VirtoCommerce.ImportModule.Data.MySql.Migrations +{ + public partial class Initial : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterDatabase() + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "ImportProfile", + columns: table => new + { + Id = table.Column(type: "varchar(128)", maxLength: 128, nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + Name = table.Column(type: "varchar(1024)", maxLength: 1024, nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + DataImporterType = table.Column(type: "varchar(254)", maxLength: 254, nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + UserId = table.Column(type: "varchar(128)", maxLength: 128, nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + UserName = table.Column(type: "varchar(254)", maxLength: 254, nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + CreatedDate = table.Column(type: "datetime(6)", nullable: false), + ModifiedDate = table.Column(type: "datetime(6)", nullable: true), + CreatedBy = table.Column(type: "varchar(64)", maxLength: 64, nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + ModifiedBy = table.Column(type: "varchar(64)", maxLength: 64, nullable: true) + .Annotation("MySql:CharSet", "utf8mb4") + }, + constraints: table => + { + table.PrimaryKey("PK_ImportProfile", x => x.Id); + }) + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "ImportRunHistory", + columns: table => new + { + Id = table.Column(type: "varchar(128)", maxLength: 128, nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + UserId = table.Column(type: "varchar(128)", maxLength: 128, nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + ProfileId = table.Column(type: "varchar(128)", maxLength: 128, nullable: false) + .Annotation("MySql:CharSet", "utf8mb4"), + ProfileName = table.Column(type: "varchar(1024)", maxLength: 1024, nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + JobId = table.Column(type: "varchar(128)", maxLength: 128, nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + Executed = table.Column(type: "datetime(6)", nullable: false), + Finished = table.Column(type: "datetime(6)", nullable: true), + TotalCount = table.Column(type: "int", nullable: false), + ProcessedCount = table.Column(type: "int", nullable: false), + ErrorsCount = table.Column(type: "int", nullable: false), + Errors = table.Column(type: "longtext", nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + FileUrl = table.Column(type: "varchar(2048)", maxLength: 2048, nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + ReportUrl = table.Column(type: "varchar(2048)", maxLength: 2048, nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + CreatedDate = table.Column(type: "datetime(6)", nullable: false), + ModifiedDate = table.Column(type: "datetime(6)", nullable: true), + CreatedBy = table.Column(type: "varchar(64)", maxLength: 64, nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"), + ModifiedBy = table.Column(type: "varchar(64)", maxLength: 64, nullable: true) + .Annotation("MySql:CharSet", "utf8mb4") + }, + constraints: table => + { + table.PrimaryKey("PK_ImportRunHistory", x => x.Id); + }) + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.CreateIndex( + name: "IX_ImportProfile_UserId_Name", + table: "ImportProfile", + columns: new[] { "UserId", "Name" }); + + migrationBuilder.CreateIndex( + name: "IX_ImportRunHistory_JobId", + table: "ImportRunHistory", + column: "JobId"); + + migrationBuilder.CreateIndex( + name: "IX_ImportRunHistory_ProfileId", + table: "ImportRunHistory", + column: "ProfileId"); + + migrationBuilder.CreateIndex( + name: "IX_ImportRunHistory_UserId", + table: "ImportRunHistory", + column: "UserId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ImportProfile"); + + migrationBuilder.DropTable( + name: "ImportRunHistory"); + } + } +} diff --git a/src/VirtoCommerce.ImportModule.Data.MySql/Migrations/ImportDbContextModelSnapshot.cs b/src/VirtoCommerce.ImportModule.Data.MySql/Migrations/ImportDbContextModelSnapshot.cs new file mode 100644 index 0000000..9c1b6d0 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.MySql/Migrations/ImportDbContextModelSnapshot.cs @@ -0,0 +1,148 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using VirtoCommerce.ImportModule.Data.Repositories; + +#nullable disable + +namespace VirtoCommerce.ImportModule.Data.MySql.Migrations +{ + [DbContext(typeof(ImportDbContext))] + partial class ImportDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("VirtoCommerce.ImportModule.Data.Models.ImportProfileEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("CreatedBy") + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("CreatedDate") + .HasColumnType("datetime(6)"); + + b.Property("DataImporterType") + .IsRequired() + .HasMaxLength(254) + .HasColumnType("varchar(254)"); + + b.Property("ModifiedBy") + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("ModifiedDate") + .HasColumnType("datetime(6)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("varchar(1024)"); + + b.Property("UserId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("UserName") + .IsRequired() + .HasMaxLength(254) + .HasColumnType("varchar(254)"); + + b.HasKey("Id"); + + b.HasIndex("UserId", "Name"); + + b.ToTable("ImportProfile", (string)null); + }); + + modelBuilder.Entity("VirtoCommerce.ImportModule.Data.Models.ImportRunHistoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("CreatedBy") + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("CreatedDate") + .HasColumnType("datetime(6)"); + + b.Property("Errors") + .HasColumnType("longtext"); + + b.Property("ErrorsCount") + .HasColumnType("int"); + + b.Property("Executed") + .HasColumnType("datetime(6)"); + + b.Property("FileUrl") + .HasMaxLength(2048) + .HasColumnType("varchar(2048)"); + + b.Property("Finished") + .HasColumnType("datetime(6)"); + + b.Property("JobId") + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("ModifiedBy") + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("ModifiedDate") + .HasColumnType("datetime(6)"); + + b.Property("ProcessedCount") + .HasColumnType("int"); + + b.Property("ProfileId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.Property("ProfileName") + .HasMaxLength(1024) + .HasColumnType("varchar(1024)"); + + b.Property("ReportUrl") + .HasMaxLength(2048) + .HasColumnType("varchar(2048)"); + + b.Property("TotalCount") + .HasColumnType("int"); + + b.Property("UserId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("varchar(128)"); + + b.HasKey("Id"); + + b.HasIndex("JobId"); + + b.HasIndex("ProfileId"); + + b.HasIndex("UserId"); + + b.ToTable("ImportRunHistory", (string)null); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/VirtoCommerce.ImportModule.Data.MySql/MySqlDbContextFactory.cs b/src/VirtoCommerce.ImportModule.Data.MySql/MySqlDbContextFactory.cs new file mode 100644 index 0000000..0114c43 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.MySql/MySqlDbContextFactory.cs @@ -0,0 +1,37 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using VirtoCommerce.ImportModule.Data.Repositories; + +namespace VirtoCommerce.ImportModule.Data.MySql; + +public class MySqlDbContextFactory : IDesignTimeDbContextFactory +{ + public ImportDbContext CreateDbContext(string[] args) + { + var builder = new DbContextOptionsBuilder(); + var connectionString = args.Any() ? args[0] : "server=localhost;user=root;password=virto;database=VirtoCommerce3;"; + var serverVersion = args.Length >= 2 ? args[1] : null; + + builder.UseMySql( + connectionString, + ResolveServerVersion(serverVersion, connectionString), + db => db.MigrationsAssembly(typeof(MySqlDbContextFactory).Assembly.GetName().Name)); + + return new ImportDbContext(builder.Options); + } + + private static ServerVersion ResolveServerVersion(string? serverVersion, string connectionString) + { + if (serverVersion == "AutoDetect") + { + return ServerVersion.AutoDetect(connectionString); + } + + if (serverVersion != null) + { + return ServerVersion.Parse(serverVersion); + } + + return new MySqlServerVersion(new Version(5, 7)); + } +} diff --git a/src/VirtoCommerce.ImportModule.Data.MySql/Readme.md b/src/VirtoCommerce.ImportModule.Data.MySql/Readme.md new file mode 100644 index 0000000..8363d20 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.MySql/Readme.md @@ -0,0 +1,27 @@ +## Package manager + +``` +Add-Migration Initial -Context VirtoCommerce.ImportModule.Data.Repositories.ImportDbContext -Verbose -OutputDir Migrations -Project VirtoCommerce.ImportModule.Data.MySql -StartupProject VirtoCommerce.ImportModule.Data.MySql -Debug +``` + +### Entity Framework Core Commands + +``` +dotnet tool install --global dotnet-ef --version 6.* +``` + +#### Generate Migrations + +``` +dotnet ef migrations add Initial -- "{connection string}" +dotnet ef migrations add Update1 -- "{connection string}" +dotnet ef migrations add Update2 -- "{connection string}" +``` + +etc.. + +#### Apply Migrations + +``` +dotnet ef database update -- "{connection string}" +``` diff --git a/src/VirtoCommerce.ImportModule.Data.MySql/VirtoCommerce.ImportModule.Data.MySql.csproj b/src/VirtoCommerce.ImportModule.Data.MySql/VirtoCommerce.ImportModule.Data.MySql.csproj new file mode 100644 index 0000000..5da0591 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.MySql/VirtoCommerce.ImportModule.Data.MySql.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/src/VirtoCommerce.ImportModule.Data.PostgreSql/DbContextOptionsBuilderExtensions.cs b/src/VirtoCommerce.ImportModule.Data.PostgreSql/DbContextOptionsBuilderExtensions.cs new file mode 100644 index 0000000..f7d4b7f --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.PostgreSql/DbContextOptionsBuilderExtensions.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; + +namespace VirtoCommerce.ImportModule.Data.PostgreSql; + +public static class DbContextOptionsBuilderExtensions +{ + /// + /// Configures the context to use PostgreSql. + /// + public static DbContextOptionsBuilder UsePostgreSqlDatabase(this DbContextOptionsBuilder builder, string connectionString) => + builder.UseNpgsql(connectionString, db => db + .MigrationsAssembly(typeof(PostgreSqlDbContextFactory).Assembly.GetName().Name)); +} diff --git a/src/VirtoCommerce.ImportModule.Data.PostgreSql/Migrations/20230711121742_Initial.Designer.cs b/src/VirtoCommerce.ImportModule.Data.PostgreSql/Migrations/20230711121742_Initial.Designer.cs new file mode 100644 index 0000000..8141321 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.PostgreSql/Migrations/20230711121742_Initial.Designer.cs @@ -0,0 +1,153 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using VirtoCommerce.ImportModule.Data.Repositories; + +#nullable disable + +namespace VirtoCommerce.ImportModule.Data.PostgreSql.Migrations +{ + [DbContext(typeof(ImportDbContext))] + [Migration("20230711121742_Initial")] + partial class Initial + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("VirtoCommerce.ImportModule.Data.Models.ImportProfileEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("CreatedBy") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("DataImporterType") + .IsRequired() + .HasMaxLength(254) + .HasColumnType("character varying(254)"); + + b.Property("ModifiedBy") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("ModifiedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("character varying(1024)"); + + b.Property("UserId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("UserName") + .IsRequired() + .HasMaxLength(254) + .HasColumnType("character varying(254)"); + + b.HasKey("Id"); + + b.HasIndex("UserId", "Name"); + + b.ToTable("ImportProfile", (string)null); + }); + + modelBuilder.Entity("VirtoCommerce.ImportModule.Data.Models.ImportRunHistoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("CreatedBy") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("Errors") + .HasColumnType("text"); + + b.Property("ErrorsCount") + .HasColumnType("integer"); + + b.Property("Executed") + .HasColumnType("timestamp with time zone"); + + b.Property("FileUrl") + .HasMaxLength(2048) + .HasColumnType("character varying(2048)"); + + b.Property("Finished") + .HasColumnType("timestamp with time zone"); + + b.Property("JobId") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ModifiedBy") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("ModifiedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("ProcessedCount") + .HasColumnType("integer"); + + b.Property("ProfileId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ProfileName") + .HasMaxLength(1024) + .HasColumnType("character varying(1024)"); + + b.Property("ReportUrl") + .HasMaxLength(2048) + .HasColumnType("character varying(2048)"); + + b.Property("TotalCount") + .HasColumnType("integer"); + + b.Property("UserId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("JobId"); + + b.HasIndex("ProfileId"); + + b.HasIndex("UserId"); + + b.ToTable("ImportRunHistory", (string)null); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/VirtoCommerce.ImportModule.Data.PostgreSql/Migrations/20230711121742_Initial.cs b/src/VirtoCommerce.ImportModule.Data.PostgreSql/Migrations/20230711121742_Initial.cs new file mode 100644 index 0000000..4d577c7 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.PostgreSql/Migrations/20230711121742_Initial.cs @@ -0,0 +1,88 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace VirtoCommerce.ImportModule.Data.PostgreSql.Migrations +{ + public partial class Initial : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "ImportProfile", + columns: table => new + { + Id = table.Column(type: "character varying(128)", maxLength: 128, nullable: false), + Name = table.Column(type: "character varying(1024)", maxLength: 1024, nullable: false), + DataImporterType = table.Column(type: "character varying(254)", maxLength: 254, nullable: false), + UserId = table.Column(type: "character varying(128)", maxLength: 128, nullable: false), + UserName = table.Column(type: "character varying(254)", maxLength: 254, nullable: false), + CreatedDate = table.Column(type: "timestamp with time zone", nullable: false), + ModifiedDate = table.Column(type: "timestamp with time zone", nullable: true), + CreatedBy = table.Column(type: "character varying(64)", maxLength: 64, nullable: true), + ModifiedBy = table.Column(type: "character varying(64)", maxLength: 64, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ImportProfile", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ImportRunHistory", + columns: table => new + { + Id = table.Column(type: "character varying(128)", maxLength: 128, nullable: false), + UserId = table.Column(type: "character varying(128)", maxLength: 128, nullable: false), + ProfileId = table.Column(type: "character varying(128)", maxLength: 128, nullable: false), + ProfileName = table.Column(type: "character varying(1024)", maxLength: 1024, nullable: true), + JobId = table.Column(type: "character varying(128)", maxLength: 128, nullable: true), + Executed = table.Column(type: "timestamp with time zone", nullable: false), + Finished = table.Column(type: "timestamp with time zone", nullable: true), + TotalCount = table.Column(type: "integer", nullable: false), + ProcessedCount = table.Column(type: "integer", nullable: false), + ErrorsCount = table.Column(type: "integer", nullable: false), + Errors = table.Column(type: "text", nullable: true), + FileUrl = table.Column(type: "character varying(2048)", maxLength: 2048, nullable: true), + ReportUrl = table.Column(type: "character varying(2048)", maxLength: 2048, nullable: true), + CreatedDate = table.Column(type: "timestamp with time zone", nullable: false), + ModifiedDate = table.Column(type: "timestamp with time zone", nullable: true), + CreatedBy = table.Column(type: "character varying(64)", maxLength: 64, nullable: true), + ModifiedBy = table.Column(type: "character varying(64)", maxLength: 64, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ImportRunHistory", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_ImportProfile_UserId_Name", + table: "ImportProfile", + columns: new[] { "UserId", "Name" }); + + migrationBuilder.CreateIndex( + name: "IX_ImportRunHistory_JobId", + table: "ImportRunHistory", + column: "JobId"); + + migrationBuilder.CreateIndex( + name: "IX_ImportRunHistory_ProfileId", + table: "ImportRunHistory", + column: "ProfileId"); + + migrationBuilder.CreateIndex( + name: "IX_ImportRunHistory_UserId", + table: "ImportRunHistory", + column: "UserId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ImportProfile"); + + migrationBuilder.DropTable( + name: "ImportRunHistory"); + } + } +} diff --git a/src/VirtoCommerce.ImportModule.Data.PostgreSql/Migrations/ImportDbContextModelSnapshot.cs b/src/VirtoCommerce.ImportModule.Data.PostgreSql/Migrations/ImportDbContextModelSnapshot.cs new file mode 100644 index 0000000..d02e960 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.PostgreSql/Migrations/ImportDbContextModelSnapshot.cs @@ -0,0 +1,151 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using VirtoCommerce.ImportModule.Data.Repositories; + +#nullable disable + +namespace VirtoCommerce.ImportModule.Data.PostgreSql.Migrations +{ + [DbContext(typeof(ImportDbContext))] + partial class ImportDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("VirtoCommerce.ImportModule.Data.Models.ImportProfileEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("CreatedBy") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("DataImporterType") + .IsRequired() + .HasMaxLength(254) + .HasColumnType("character varying(254)"); + + b.Property("ModifiedBy") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("ModifiedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(1024) + .HasColumnType("character varying(1024)"); + + b.Property("UserId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("UserName") + .IsRequired() + .HasMaxLength(254) + .HasColumnType("character varying(254)"); + + b.HasKey("Id"); + + b.HasIndex("UserId", "Name"); + + b.ToTable("ImportProfile", (string)null); + }); + + modelBuilder.Entity("VirtoCommerce.ImportModule.Data.Models.ImportRunHistoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("CreatedBy") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("Errors") + .HasColumnType("text"); + + b.Property("ErrorsCount") + .HasColumnType("integer"); + + b.Property("Executed") + .HasColumnType("timestamp with time zone"); + + b.Property("FileUrl") + .HasMaxLength(2048) + .HasColumnType("character varying(2048)"); + + b.Property("Finished") + .HasColumnType("timestamp with time zone"); + + b.Property("JobId") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ModifiedBy") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("ModifiedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("ProcessedCount") + .HasColumnType("integer"); + + b.Property("ProfileId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ProfileName") + .HasMaxLength(1024) + .HasColumnType("character varying(1024)"); + + b.Property("ReportUrl") + .HasMaxLength(2048) + .HasColumnType("character varying(2048)"); + + b.Property("TotalCount") + .HasColumnType("integer"); + + b.Property("UserId") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("JobId"); + + b.HasIndex("ProfileId"); + + b.HasIndex("UserId"); + + b.ToTable("ImportRunHistory", (string)null); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/VirtoCommerce.ImportModule.Data.PostgreSql/PostgreSqlDbContextFactory.cs b/src/VirtoCommerce.ImportModule.Data.PostgreSql/PostgreSqlDbContextFactory.cs new file mode 100644 index 0000000..941ca61 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.PostgreSql/PostgreSqlDbContextFactory.cs @@ -0,0 +1,20 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using VirtoCommerce.ImportModule.Data.Repositories; + +namespace VirtoCommerce.ImportModule.Data.PostgreSql; + +public class PostgreSqlDbContextFactory : IDesignTimeDbContextFactory +{ + public ImportDbContext CreateDbContext(string[] args) + { + var builder = new DbContextOptionsBuilder(); + var connectionString = args.Any() ? args[0] : "User ID = postgres; Password = password; Host = localhost; Port = 5432; Database = virtocommerce3;"; + + builder.UseNpgsql( + connectionString, + db => db.MigrationsAssembly(typeof(PostgreSqlDbContextFactory).Assembly.GetName().Name)); + + return new ImportDbContext(builder.Options); + } +} diff --git a/src/VirtoCommerce.ImportModule.Data.PostgreSql/Readme.md b/src/VirtoCommerce.ImportModule.Data.PostgreSql/Readme.md new file mode 100644 index 0000000..478d683 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.PostgreSql/Readme.md @@ -0,0 +1,27 @@ +## Package manager + +``` +Add-Migration Initial -Context VirtoCommerce.ImportModule.Data.Repositories.ImportDbContext -Verbose -OutputDir Migrations -Project VirtoCommerce.ImportModule.Data.PostgreSql -StartupProject VirtoCommerce.ImportModule.Data.PostgreSql -Debug +``` + +### Entity Framework Core Commands + +``` +dotnet tool install --global dotnet-ef --version 6.* +``` + +#### Generate Migrations + +``` +dotnet ef migrations add Initial -- "{connection string}" +dotnet ef migrations add Update1 -- "{connection string}" +dotnet ef migrations add Update2 -- "{connection string}" +``` + +etc.. + +#### Apply Migrations + +``` +dotnet ef database update -- "{connection string}" +``` diff --git a/src/VirtoCommerce.ImportModule.Data.PostgreSql/VirtoCommerce.ImportModule.Data.PostgreSql.csproj b/src/VirtoCommerce.ImportModule.Data.PostgreSql/VirtoCommerce.ImportModule.Data.PostgreSql.csproj new file mode 100644 index 0000000..46c3dc9 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.PostgreSql/VirtoCommerce.ImportModule.Data.PostgreSql.csproj @@ -0,0 +1,26 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/src/VirtoCommerce.ImportModule.Data.SqlServer/DbContextOptionsBuilderExtensions.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/DbContextOptionsBuilderExtensions.cs new file mode 100644 index 0000000..7f6146d --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/DbContextOptionsBuilderExtensions.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore; + +namespace VirtoCommerce.ImportModule.Data.SqlServer; + +public static class DbContextOptionsBuilderExtensions +{ + /// + /// Configures the context to use SqlServer. + /// + public static DbContextOptionsBuilder UseSqlServerDatabase(this DbContextOptionsBuilder builder, string connectionString) + { + return builder.UseSqlServer(connectionString, db => db + .MigrationsAssembly(typeof(SqlServerDbContextFactory).Assembly.GetName().Name)); + } +} diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20220527085414_AddImport.Designer.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220527085414_AddImport.Designer.cs similarity index 98% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20220527085414_AddImport.Designer.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220527085414_AddImport.Designer.cs index 757e68a..1d83993 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20220527085414_AddImport.Designer.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220527085414_AddImport.Designer.cs @@ -9,7 +9,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { [DbContext(typeof(ImportDbContext))] [Migration("20220527085414_AddImport")] diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20220527085414_AddImport.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220527085414_AddImport.cs similarity index 98% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20220527085414_AddImport.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220527085414_AddImport.cs index 0bd2120..8362ff6 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20220527085414_AddImport.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220527085414_AddImport.cs @@ -3,7 +3,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { public partial class AddImport : Migration { diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20220603103138_RenameSellerId.Designer.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220603103138_RenameSellerId.Designer.cs similarity index 98% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20220603103138_RenameSellerId.Designer.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220603103138_RenameSellerId.Designer.cs index ea558c5..079b1b0 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20220603103138_RenameSellerId.Designer.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220603103138_RenameSellerId.Designer.cs @@ -9,7 +9,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { [DbContext(typeof(ImportDbContext))] [Migration("20220603103138_RenameSellerId")] diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20220603103138_RenameSellerId.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220603103138_RenameSellerId.cs similarity index 96% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20220603103138_RenameSellerId.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220603103138_RenameSellerId.cs index a88326c..c4578dc 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20220603103138_RenameSellerId.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20220603103138_RenameSellerId.cs @@ -2,7 +2,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { public partial class RenameSellerId : Migration { diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20230125090000_AddErrorReport.Designer.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230125090000_AddErrorReport.Designer.cs similarity index 98% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20230125090000_AddErrorReport.Designer.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230125090000_AddErrorReport.Designer.cs index 2f03fe4..e94b655 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20230125090000_AddErrorReport.Designer.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230125090000_AddErrorReport.Designer.cs @@ -9,7 +9,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { [DbContext(typeof(ImportDbContext))] [Migration("20230125090000_AddErrorReport")] diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20230125090000_AddErrorReport.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230125090000_AddErrorReport.cs similarity index 91% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20230125090000_AddErrorReport.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230125090000_AddErrorReport.cs index 1e37357..3b50bc8 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20230125090000_AddErrorReport.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230125090000_AddErrorReport.cs @@ -2,7 +2,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { public partial class AddErrorReport : Migration { diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20230412143004_AddImportFileUrl.Designer.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230412143004_AddImportFileUrl.Designer.cs similarity index 98% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20230412143004_AddImportFileUrl.Designer.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230412143004_AddImportFileUrl.Designer.cs index 381a2b8..960ddfa 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20230412143004_AddImportFileUrl.Designer.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230412143004_AddImportFileUrl.Designer.cs @@ -9,7 +9,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { [DbContext(typeof(ImportDbContext))] [Migration("20230412143004_AddImportFileUrl")] diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20230412143004_AddImportFileUrl.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230412143004_AddImportFileUrl.cs similarity index 91% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20230412143004_AddImportFileUrl.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230412143004_AddImportFileUrl.cs index c070ac5..7774f6a 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20230412143004_AddImportFileUrl.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230412143004_AddImportFileUrl.cs @@ -2,7 +2,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { public partial class AddImportFileUrl : Migration { diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20230615115744_AddRunHistoryIndexes.Designer.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230615115744_AddRunHistoryIndexes.Designer.cs similarity index 98% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20230615115744_AddRunHistoryIndexes.Designer.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230615115744_AddRunHistoryIndexes.Designer.cs index 66e5d5a..12d5798 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20230615115744_AddRunHistoryIndexes.Designer.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230615115744_AddRunHistoryIndexes.Designer.cs @@ -9,7 +9,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { [DbContext(typeof(ImportDbContext))] [Migration("20230615115744_AddRunHistoryIndexes")] diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/20230615115744_AddRunHistoryIndexes.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230615115744_AddRunHistoryIndexes.cs similarity index 95% rename from src/VirtoCommerce.ImportModule.Data/Migrations/20230615115744_AddRunHistoryIndexes.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230615115744_AddRunHistoryIndexes.cs index ea12406..1d94ac8 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/20230615115744_AddRunHistoryIndexes.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/20230615115744_AddRunHistoryIndexes.cs @@ -2,7 +2,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { public partial class AddRunHistoryIndexes : Migration { diff --git a/src/VirtoCommerce.ImportModule.Data/Migrations/ImportDbContextModelSnapshot.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/ImportDbContextModelSnapshot.cs similarity index 98% rename from src/VirtoCommerce.ImportModule.Data/Migrations/ImportDbContextModelSnapshot.cs rename to src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/ImportDbContextModelSnapshot.cs index a4fa224..f09411a 100644 --- a/src/VirtoCommerce.ImportModule.Data/Migrations/ImportDbContextModelSnapshot.cs +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Migrations/ImportDbContextModelSnapshot.cs @@ -8,7 +8,7 @@ #nullable disable -namespace VirtoCommerce.ImportModule.Data.Migrations +namespace VirtoCommerce.ImportModule.Data.SqlServer.Migrations { [DbContext(typeof(ImportDbContext))] partial class ImportDbContextModelSnapshot : ModelSnapshot diff --git a/src/VirtoCommerce.ImportModule.Data.SqlServer/Readme.md b/src/VirtoCommerce.ImportModule.Data.SqlServer/Readme.md new file mode 100644 index 0000000..d64faf4 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/Readme.md @@ -0,0 +1,27 @@ +## Package manager + +``` +Add-Migration Initial -Context VirtoCommerce.ImportModule.Data.Repositories.CoreDbContext -Verbose -OutputDir Migrations -Project VirtoCommerce.ImportModule.Data.SqlServerSql -StartupProject VirtoCommerce.ImportModule.Data.SqlServerSql -Debug +``` + +### Entity Framework Core Commands + +``` +dotnet tool install --global dotnet-ef --version 6.* +``` + +#### Generate Migrations + +``` +dotnet ef migrations add Initial -- "{connection string}" +dotnet ef migrations add Update1 -- "{connection string}" +dotnet ef migrations add Update2 -- "{connection string}" +``` + +etc.. + +#### Apply Migrations + +``` +dotnet ef database update -- "{connection string}" +``` diff --git a/src/VirtoCommerce.ImportModule.Data.SqlServer/SqlServerDbContextFactory.cs b/src/VirtoCommerce.ImportModule.Data.SqlServer/SqlServerDbContextFactory.cs new file mode 100644 index 0000000..ee2f444 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/SqlServerDbContextFactory.cs @@ -0,0 +1,20 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using VirtoCommerce.ImportModule.Data.Repositories; + +namespace VirtoCommerce.ImportModule.Data.SqlServer; + +public class SqlServerDbContextFactory : IDesignTimeDbContextFactory +{ + public ImportDbContext CreateDbContext(string[] args) + { + var builder = new DbContextOptionsBuilder(); + var connectionString = args.Any() ? args[0] : "Data Source=(local);Initial Catalog=VirtoCommerce3;Persist Security Info=True;User ID=virto;Password=virto;MultipleActiveResultSets=True;Connect Timeout=30"; + + builder.UseSqlServer( + connectionString, + db => db.MigrationsAssembly(typeof(SqlServerDbContextFactory).Assembly.GetName().Name)); + + return new ImportDbContext(builder.Options); + } +} diff --git a/src/VirtoCommerce.ImportModule.Data.SqlServer/VirtoCommerce.ImportModule.Data.SqlServer.csproj b/src/VirtoCommerce.ImportModule.Data.SqlServer/VirtoCommerce.ImportModule.Data.SqlServer.csproj new file mode 100644 index 0000000..a0d3727 --- /dev/null +++ b/src/VirtoCommerce.ImportModule.Data.SqlServer/VirtoCommerce.ImportModule.Data.SqlServer.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + enable + enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/src/VirtoCommerce.ImportModule.Data/Repositories/DesignTimeDbContextFactory.cs b/src/VirtoCommerce.ImportModule.Data/Repositories/DesignTimeDbContextFactory.cs deleted file mode 100644 index 5719a9a..0000000 --- a/src/VirtoCommerce.ImportModule.Data/Repositories/DesignTimeDbContextFactory.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Design; - -namespace VirtoCommerce.ImportModule.Data.Repositories -{ - public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory - { - public ImportDbContext CreateDbContext(string[] args) - { - var builder = new DbContextOptionsBuilder(); - - builder.UseSqlServer("Data Source=DESKTOP-2EEHPNV\\SQLEXPRESS;Initial Catalog=VCRnD;Persist Security Info=True;User ID=virto;Password=virto;MultipleActiveResultSets=True;Connect Timeout=30"); - - return new ImportDbContext(builder.Options); - } - } -} diff --git a/src/VirtoCommerce.ImportModule.Data/Repositories/ImportDbContext.cs b/src/VirtoCommerce.ImportModule.Data/Repositories/ImportDbContext.cs index 56c03f8..de3c7e3 100644 --- a/src/VirtoCommerce.ImportModule.Data/Repositories/ImportDbContext.cs +++ b/src/VirtoCommerce.ImportModule.Data/Repositories/ImportDbContext.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Reflection; using EntityFrameworkCore.Triggers; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; @@ -20,12 +21,15 @@ protected ImportDbContext(DbContextOptions options) protected override void OnModelCreating(ModelBuilder modelBuilder) { #region ImportProfileEntity + modelBuilder.Entity().ToTable("ImportProfile").HasKey(x => x.Id); modelBuilder.Entity().Property(x => x.Id).HasMaxLength(128).ValueGeneratedOnAdd(); modelBuilder.Entity().HasIndex(x => new { x.UserId, x.Name }); + #endregion #region ImportRunHistoryEntity + modelBuilder.Entity().ToTable("ImportRunHistory").HasKey(x => x.Id); modelBuilder.Entity().Property(x => x.Id).HasMaxLength(128).ValueGeneratedOnAdd(); modelBuilder.Entity().Property(x => x.Errors) @@ -33,7 +37,25 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity().HasIndex(x => x.ProfileId); modelBuilder.Entity().HasIndex(x => x.UserId); modelBuilder.Entity().HasIndex(x => x.JobId); + #endregion + + base.OnModelCreating(modelBuilder); + + // Allows configuration for an entity type for different database types. + // Applies configuration from all + switch (Database.ProviderName) + { + case "Pomelo.EntityFrameworkCore.MySql": + modelBuilder.ApplyConfigurationsFromAssembly(Assembly.Load("VirtoCommerce.ImportModule.Data.MySql")); + break; + case "Npgsql.EntityFrameworkCore.PostgreSQL": + modelBuilder.ApplyConfigurationsFromAssembly(Assembly.Load("VirtoCommerce.ImportModule.Data.PostgreSql")); + break; + case "Microsoft.EntityFrameworkCore.SqlServer": + modelBuilder.ApplyConfigurationsFromAssembly(Assembly.Load("VirtoCommerce.ImportModule.Data.SqlServer")); + break; + } } } } diff --git a/src/VirtoCommerce.ImportModule.Data/VirtoCommerce.ImportModule.Data.csproj b/src/VirtoCommerce.ImportModule.Data/VirtoCommerce.ImportModule.Data.csproj index 8bfaca1..9f71f8b 100644 --- a/src/VirtoCommerce.ImportModule.Data/VirtoCommerce.ImportModule.Data.csproj +++ b/src/VirtoCommerce.ImportModule.Data/VirtoCommerce.ImportModule.Data.csproj @@ -11,11 +11,6 @@ - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/src/VirtoCommerce.ImportModule.Web/Module.cs b/src/VirtoCommerce.ImportModule.Web/Module.cs index 604b894..28778ca 100644 --- a/src/VirtoCommerce.ImportModule.Web/Module.cs +++ b/src/VirtoCommerce.ImportModule.Web/Module.cs @@ -11,8 +11,11 @@ using VirtoCommerce.ImportModule.Core.Services; using VirtoCommerce.ImportModule.CsvHelper; using VirtoCommerce.ImportModule.Data.BackgroundJobs; +using VirtoCommerce.ImportModule.Data.MySql; +using VirtoCommerce.ImportModule.Data.PostgreSql; using VirtoCommerce.ImportModule.Data.Repositories; using VirtoCommerce.ImportModule.Data.Services; +using VirtoCommerce.ImportModule.Data.SqlServer; using VirtoCommerce.NotificationsModule.Core.Services; using VirtoCommerce.NotificationsModule.TemplateLoader.FileSystem; using VirtoCommerce.Platform.Core.JsonConverters; @@ -30,11 +33,24 @@ public class Module : IModule, IHasConfiguration public void Initialize(IServiceCollection serviceCollection) { - // database initialization - serviceCollection.AddDbContext((provider, options) => + // Database initialization + serviceCollection.AddDbContext((_, options) => { - var configuration = provider.GetRequiredService(); - options.UseSqlServer(configuration.GetConnectionString(ModuleInfo.Id) ?? configuration.GetConnectionString("VirtoCommerce")); + var databaseProvider = Configuration.GetValue("DatabaseProvider", "SqlServer"); + var connectionString = Configuration.GetConnectionString(ModuleInfo.Id) ?? Configuration.GetConnectionString("VirtoCommerce"); + + switch (databaseProvider) + { + case "MySql": + options.UseMySqlDatabase(connectionString); + break; + case "PostgreSql": + options.UsePostgreSqlDatabase(connectionString); + break; + default: + options.UseSqlServerDatabase(connectionString); + break; + } }); serviceCollection.AddTransient(); diff --git a/src/VirtoCommerce.ImportModule.Web/VirtoCommerce.ImportModule.Web.csproj b/src/VirtoCommerce.ImportModule.Web/VirtoCommerce.ImportModule.Web.csproj index 9186ac2..c4f7b57 100644 --- a/src/VirtoCommerce.ImportModule.Web/VirtoCommerce.ImportModule.Web.csproj +++ b/src/VirtoCommerce.ImportModule.Web/VirtoCommerce.ImportModule.Web.csproj @@ -17,6 +17,9 @@ + + + diff --git a/src/VirtoCommerce.ImportModule.Web/module.manifest b/src/VirtoCommerce.ImportModule.Web/module.manifest index 318d0b5..751b5ec 100644 --- a/src/VirtoCommerce.ImportModule.Web/module.manifest +++ b/src/VirtoCommerce.ImportModule.Web/module.manifest @@ -1,7 +1,7 @@ VirtoCommerce.Import - 3.200.0 + 3.201.0 3.293.0