enisn 5 years ago
parent
commit
d8725ce943
  1. 4
      services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationChecker.cs
  2. 4
      services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs
  3. 28
      services/catalog/EShopOnAbp.CatalogService.sln
  4. 25
      services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EShopOnAbp.CatalogService.EntityFrameworkCore.csproj
  5. 34
      services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EntityFrameworkCore/CatalogServiceDbContext.cs
  6. 38
      services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EntityFrameworkCore/CatalogServiceDbContextFactory.cs
  7. 39
      services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EntityFrameworkCore/CatalogServiceEfCoreEntityExtensionMappings.cs
  8. 37
      services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EntityFrameworkCore/CatalogServiceEntityFrameworkCoreModule.cs
  9. 30
      services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/Migrations/20211125124215_Initial.Designer.cs
  10. 19
      services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/Migrations/20211125124215_Initial.cs
  11. 28
      services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/Migrations/CatalogServiceDbContextModelSnapshot.cs
  12. 2
      services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/Properties/AssemblyInfo.cs
  13. 4
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs
  14. 7
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationChecker.cs
  15. 12
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs
  16. 2
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj
  17. 15
      services/catalog/src/EShopOnAbp.CatalogService.MongoDB/EShopOnAbp.CatalogService.MongoDB.csproj
  18. 20
      services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbContext.cs
  19. 14
      services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbContextExtensions.cs
  20. 23
      services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbModule.cs
  21. 13
      services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/ICatalogServiceMongoDbContext.cs
  22. 10
      services/catalog/test/EShopOnAbp.CatalogService.Domain.Tests/CatalogServiceDomainCollection.cs
  23. 4
      services/catalog/test/EShopOnAbp.CatalogService.Domain.Tests/CatalogServiceDomainTestModule.cs
  24. 2
      services/catalog/test/EShopOnAbp.CatalogService.Domain.Tests/EShopOnAbp.CatalogService.Domain.Tests.csproj
  25. 9
      services/catalog/test/EShopOnAbp.CatalogService.EntityFrameworkCore.Tests/EntityFrameworkCore/CatalogServiceEntityFrameworkCoreTestBase.cs
  26. 62
      services/catalog/test/EShopOnAbp.CatalogService.EntityFrameworkCore.Tests/EntityFrameworkCore/CatalogServiceEntityFrameworkCoreTestModule.cs
  27. 9
      services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/EShopOnAbp.CatalogService.MongoDB.Tests.csproj
  28. 9
      services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoCollection.cs
  29. 8
      services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoDbCollectionFixtureBase.cs
  30. 22
      services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoDbFixture.cs
  31. 10
      services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoDbTestBase.cs
  32. 27
      services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoDbTestModule.cs
  33. 6
      services/catalog/test/EShopOnAbp.CatalogService.TestBase/CatalogServiceTestConsts.cs
  34. 4
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs
  35. 4
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs
  36. 4
      services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationChecker.cs
  37. 4
      services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs
  38. 8
      services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationChecker.cs
  39. 4
      services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs
  40. 188
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/DatabaseMigrationEventHandlerBase.cs
  41. 188
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs
  42. 67
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs
  43. 204
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs
  44. 52
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs
  45. 65
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs
  46. 1
      shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj

4
services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationChecker.cs

@ -1,13 +1,13 @@
using EShopOnAbp.AdministrationService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using System;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace EShopOnAbp.AdministrationService.DbMigrations
{
public class AdministrationServiceDatabaseMigrationChecker : PendingMigrationsCheckerBase<AdministrationServiceDbContext>
public class AdministrationServiceDatabaseMigrationChecker : PendingEfCoreMigrationsChecker<AdministrationServiceDbContext>
{
public AdministrationServiceDatabaseMigrationChecker(
IUnitOfWorkManager unitOfWorkManager,

4
services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs

@ -1,8 +1,8 @@
using EShopOnAbp.AdministrationService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using System;
using System.Linq;
using System.Threading.Tasks;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
@ -13,7 +13,7 @@ using Volo.Abp.Uow;
namespace EShopOnAbp.AdministrationService.DbMigrations
{
public class AdministrationServiceDatabaseMigrationEventHandler
: DatabaseMigrationEventHandlerBase<AdministrationServiceDbContext>,
: DatabaseEfCoreMigrationEventHandler<AdministrationServiceDbContext>,
IDistributedEventHandler<TenantCreatedEto>,
IDistributedEventHandler<ApplyDatabaseMigrationsEto>
{

28
services/catalog/EShopOnAbp.CatalogService.sln

@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.D
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.Application", "src\EShopOnAbp.CatalogService.Application\EShopOnAbp.CatalogService.Application.csproj", "{1A94A50E-06DC-43C1-80B5-B662820EC3EB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.EntityFrameworkCore", "src\EShopOnAbp.CatalogService.EntityFrameworkCore\EShopOnAbp.CatalogService.EntityFrameworkCore.csproj", "{C956DD76-69C8-4A9C-83EA-D17DF83340FD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{CA9AC87F-097E-4F15-8393-4BC07735A5B0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{04DBDB01-70F4-4E06-B468-8F87850B22BE}"
@ -23,8 +21,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.H
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.HttpApi.Client", "src\EShopOnAbp.CatalogService.HttpApi.Client\EShopOnAbp.CatalogService.HttpApi.Client.csproj", "{3B5A0094-670D-4BB1-BFDD-61B88A8773DC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.EntityFrameworkCore.Tests", "test\EShopOnAbp.CatalogService.EntityFrameworkCore.Tests\EShopOnAbp.CatalogService.EntityFrameworkCore.Tests.csproj", "{1FE30EB9-74A9-47F5-A9F6-7B1FAB672D81}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.TestBase", "test\EShopOnAbp.CatalogService.TestBase\EShopOnAbp.CatalogService.TestBase.csproj", "{91853F21-9CD9-4132-BC29-A7D5D84FFFE7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.Domain.Tests", "test\EShopOnAbp.CatalogService.Domain.Tests\EShopOnAbp.CatalogService.Domain.Tests.csproj", "{E512F4D9-9375-480F-A2F6-A46509F9D824}"
@ -33,6 +29,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.H
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EShopOnAbp.CatalogService.HttpApi.Host", "src\EShopOnAbp.CatalogService.HttpApi.Host\EShopOnAbp.CatalogService.HttpApi.Host.csproj", "{748584B1-BA69-4F6A-81AA-F4BDE6BCE29D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EShopOnAbp.CatalogService.MongoDB", "src\EShopOnAbp.CatalogService.MongoDB\EShopOnAbp.CatalogService.MongoDB.csproj", "{5D15000F-E6C4-45ED-96BC-6B6A029BA584}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EShopOnAbp.CatalogService.MongoDB.Tests", "test\EShopOnAbp.CatalogService.MongoDB.Tests\EShopOnAbp.CatalogService.MongoDB.Tests.csproj", "{353E567B-0519-4768-92DD-DC88E318E5B6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -47,10 +47,6 @@ Global
{1A94A50E-06DC-43C1-80B5-B662820EC3EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A94A50E-06DC-43C1-80B5-B662820EC3EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A94A50E-06DC-43C1-80B5-B662820EC3EB}.Release|Any CPU.Build.0 = Release|Any CPU
{C956DD76-69C8-4A9C-83EA-D17DF83340FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C956DD76-69C8-4A9C-83EA-D17DF83340FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C956DD76-69C8-4A9C-83EA-D17DF83340FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C956DD76-69C8-4A9C-83EA-D17DF83340FD}.Release|Any CPU.Build.0 = Release|Any CPU
{50B2631D-129C-47B3-A587-029CCD6099BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50B2631D-129C-47B3-A587-029CCD6099BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50B2631D-129C-47B3-A587-029CCD6099BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -71,10 +67,6 @@ Global
{3B5A0094-670D-4BB1-BFDD-61B88A8773DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B5A0094-670D-4BB1-BFDD-61B88A8773DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B5A0094-670D-4BB1-BFDD-61B88A8773DC}.Release|Any CPU.Build.0 = Release|Any CPU
{1FE30EB9-74A9-47F5-A9F6-7B1FAB672D81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FE30EB9-74A9-47F5-A9F6-7B1FAB672D81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FE30EB9-74A9-47F5-A9F6-7B1FAB672D81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FE30EB9-74A9-47F5-A9F6-7B1FAB672D81}.Release|Any CPU.Build.0 = Release|Any CPU
{91853F21-9CD9-4132-BC29-A7D5D84FFFE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91853F21-9CD9-4132-BC29-A7D5D84FFFE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91853F21-9CD9-4132-BC29-A7D5D84FFFE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -91,6 +83,14 @@ Global
{748584B1-BA69-4F6A-81AA-F4BDE6BCE29D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{748584B1-BA69-4F6A-81AA-F4BDE6BCE29D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{748584B1-BA69-4F6A-81AA-F4BDE6BCE29D}.Release|Any CPU.Build.0 = Release|Any CPU
{5D15000F-E6C4-45ED-96BC-6B6A029BA584}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D15000F-E6C4-45ED-96BC-6B6A029BA584}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D15000F-E6C4-45ED-96BC-6B6A029BA584}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D15000F-E6C4-45ED-96BC-6B6A029BA584}.Release|Any CPU.Build.0 = Release|Any CPU
{353E567B-0519-4768-92DD-DC88E318E5B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{353E567B-0519-4768-92DD-DC88E318E5B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{353E567B-0519-4768-92DD-DC88E318E5B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{353E567B-0519-4768-92DD-DC88E318E5B6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -98,17 +98,17 @@ Global
GlobalSection(NestedProjects) = preSolution
{554AD327-6DBA-4F8F-96F8-81CE7A0C863F} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{1A94A50E-06DC-43C1-80B5-B662820EC3EB} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{C956DD76-69C8-4A9C-83EA-D17DF83340FD} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{50B2631D-129C-47B3-A587-029CCD6099BC} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
{42F719ED-8413-4895-B5B4-5AB56079BC66} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{520659C8-C734-4298-A3DA-B539DB9DFC0B} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{4164BDF7-F527-4E85-9CE6-E3C2D7426A27} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{3B5A0094-670D-4BB1-BFDD-61B88A8773DC} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{1FE30EB9-74A9-47F5-A9F6-7B1FAB672D81} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
{91853F21-9CD9-4132-BC29-A7D5D84FFFE7} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
{E512F4D9-9375-480F-A2F6-A46509F9D824} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
{EF480016-9127-4916-8735-D2466BDBC582} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
{748584B1-BA69-4F6A-81AA-F4BDE6BCE29D} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{5D15000F-E6C4-45ED-96BC-6B6A029BA584} = {CA9AC87F-097E-4F15-8393-4BC07735A5B0}
{353E567B-0519-4768-92DD-DC88E318E5B6} = {04DBDB01-70F4-4E06-B468-8F87850B22BE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6AAFA1C6-603E-13FA-45E5-7910AA9F661D}

25
services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EShopOnAbp.CatalogService.EntityFrameworkCore.csproj

@ -1,25 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\..\common.props" />
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>EShopOnAbp.CatalogService</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\EShopOnAbp.CatalogService.Domain\EShopOnAbp.CatalogService.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.EntityFrameworkCore.PostgreSql" Version="5.0.0-rc.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>compile; contentFiles; build; buildMultitargeting; buildTransitive; analyzers; native</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>

34
services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EntityFrameworkCore/CatalogServiceDbContext.cs

@ -1,34 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
namespace EShopOnAbp.CatalogService.EntityFrameworkCore
{
[ConnectionStringName(CatalogServiceDbProperties.ConnectionStringName)]
public class CatalogServiceDbContext :
AbpDbContext<CatalogServiceDbContext>
{
/* Add DbSet properties for your Aggregate Roots / Entities here. */
public CatalogServiceDbContext(DbContextOptions<CatalogServiceDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
/* Include modules to your migration db context */
/* Configure your own tables/entities inside here */
//builder.Entity<YourEntity>(b =>
//{
// b.ToTable(CatalogServiceConsts.DbTablePrefix + "YourEntities", CatalogServiceConsts.DbSchema);
// b.ConfigureByConvention(); //auto configure for the base class props
// //...
//});
}
}
}

38
services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EntityFrameworkCore/CatalogServiceDbContextFactory.cs

@ -1,38 +0,0 @@
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
namespace EShopOnAbp.CatalogService.EntityFrameworkCore
{
/* This class is needed for EF Core console commands
* (like Add-Migration and Update-Database commands) */
public class CatalogServiceDbContextFactory : IDesignTimeDbContextFactory<CatalogServiceDbContext>
{
public CatalogServiceDbContext CreateDbContext(string[] args)
{
CatalogServiceEfCoreEntityExtensionMappings.Configure();
var configuration = BuildConfiguration();
var builder = new DbContextOptionsBuilder<CatalogServiceDbContext>()
.UseNpgsql(
configuration.GetConnectionString(CatalogServiceDbProperties.ConnectionStringName),
b =>
{
b.MigrationsHistoryTable("__CatalogService_Migrations");
});
return new CatalogServiceDbContext(builder.Options);
}
private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../EShopOnAbp.CatalogService.HttpApi.Host"))
.AddJsonFile("appsettings.json", optional: false);
return builder.Build();
}
}
}

39
services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EntityFrameworkCore/CatalogServiceEfCoreEntityExtensionMappings.cs

@ -1,39 +0,0 @@
using Volo.Abp.Threading;
namespace EShopOnAbp.CatalogService.EntityFrameworkCore
{
public static class CatalogServiceEfCoreEntityExtensionMappings
{
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
public static void Configure()
{
OneTimeRunner.Run(() =>
{
/* You can configure extra properties for the
* entities defined in the modules used by your application.
*
* This class can be used to map these extra properties to table fields in the database.
*
* USE THIS CLASS ONLY TO CONFIGURE EF CORE RELATED MAPPING.
* USE CatalogServiceModuleExtensionConfigurator CLASS (in the Domain.Shared project)
* FOR A HIGH LEVEL API TO DEFINE EXTRA PROPERTIES TO ENTITIES OF THE USED MODULES
*
* Example: Map a property to a table field:
ObjectExtensionManager.Instance
.MapEfCoreProperty<IdentityUser, string>(
"MyProperty",
(entityBuilder, propertyBuilder) =>
{
propertyBuilder.HasMaxLength(128);
}
);
* See the documentation for more:
* https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities
*/
});
}
}
}

37
services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/EntityFrameworkCore/CatalogServiceEntityFrameworkCoreModule.cs

@ -1,37 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.PostgreSql;
using Volo.Abp.Modularity;
namespace EShopOnAbp.CatalogService.EntityFrameworkCore
{
[DependsOn(
typeof(CatalogServiceDomainModule),
typeof(AbpEntityFrameworkCorePostgreSqlModule)
)]
public class CatalogServiceEntityFrameworkCoreModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
CatalogServiceEfCoreEntityExtensionMappings.Configure();
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpDbContext<CatalogServiceDbContext>(options =>
{
/* Remove "includeAllEntities: true" to create
* default repositories only for aggregate roots */
options.AddDefaultRepositories(includeAllEntities: true);
});
Configure<AbpDbContextOptions>(options =>
{
options.UseNpgsql(b =>
{
b.MigrationsHistoryTable("__CatalogService_Migrations");
});
});
}
}
}

30
services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/Migrations/20211125124215_Initial.Designer.cs

@ -1,30 +0,0 @@
// <auto-generated />
using EShopOnAbp.CatalogService.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Volo.Abp.EntityFrameworkCore;
#nullable disable
namespace EShopOnAbp.CatalogService.Migrations
{
[DbContext(typeof(CatalogServiceDbContext))]
[Migration("20211125124215_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.PostgreSql)
.HasAnnotation("ProductVersion", "6.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
#pragma warning restore 612, 618
}
}
}

19
services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/Migrations/20211125124215_Initial.cs

@ -1,19 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EShopOnAbp.CatalogService.Migrations
{
public partial class Initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

28
services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/Migrations/CatalogServiceDbContextModelSnapshot.cs

@ -1,28 +0,0 @@
// <auto-generated />
using EShopOnAbp.CatalogService.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Volo.Abp.EntityFrameworkCore;
#nullable disable
namespace EShopOnAbp.CatalogService.Migrations
{
[DbContext(typeof(CatalogServiceDbContext))]
partial class CatalogServiceDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.PostgreSql)
.HasAnnotation("ProductVersion", "6.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
#pragma warning restore 612, 618
}
}
}

2
services/catalog/src/EShopOnAbp.CatalogService.EntityFrameworkCore/Properties/AssemblyInfo.cs

@ -1,2 +0,0 @@
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleToAttribute("EShopOnAbp.CatalogService.EntityFrameworkCore.Tests")]

4
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs

@ -1,5 +1,4 @@
using EShopOnAbp.CatalogService.DbMigrations;
using EShopOnAbp.CatalogService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.AspNetCore;
using EShopOnAbp.Shared.Hosting.Microservices;
using Microsoft.AspNetCore.Builder;
@ -9,6 +8,7 @@ using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using EShopOnAbp.CatalogService.MongoDB;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Modularity;
@ -19,7 +19,7 @@ namespace EShopOnAbp.CatalogService
[DependsOn(
typeof(CatalogServiceHttpApiModule),
typeof(CatalogServiceApplicationModule),
typeof(CatalogServiceEntityFrameworkCoreModule),
typeof(CatalogServiceMongoDbModule),
typeof(EShopOnAbpSharedHostingMicroservicesModule)
)]
public class CatalogServiceHttpApiHostModule : AbpModule

7
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationChecker.cs

@ -1,14 +1,13 @@
using EShopOnAbp.CatalogService.EntityFrameworkCore;
using System;
using EShopOnAbp.CatalogService.MongoDB;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using System;
using System.Threading.Tasks;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace EShopOnAbp.CatalogService.DbMigrations
{
public class CatalogServiceDatabaseMigrationChecker : PendingMigrationsCheckerBase<CatalogServiceDbContext>
public class CatalogServiceDatabaseMigrationChecker : PendingMongoDbMigrationsChecker<CatalogServiceMongoDbContext>
{
public CatalogServiceDatabaseMigrationChecker(
IUnitOfWorkManager unitOfWorkManager,

12
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs

@ -1,7 +1,8 @@
using EShopOnAbp.CatalogService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using System;
using System.Threading.Tasks;
using EShopOnAbp.CatalogService.MongoDB;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.MongoDb;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
@ -10,20 +11,21 @@ using Volo.Abp.Uow;
namespace EShopOnAbp.CatalogService.DbMigrations
{
public class CatalogServiceDatabaseMigrationEventHandler
: DatabaseMigrationEventHandlerBase<CatalogServiceDbContext>,
: DatabaseMongoDbMigrationEventHandler<CatalogServiceMongoDbContext>,
IDistributedEventHandler<ApplyDatabaseMigrationsEto>
{
public CatalogServiceDatabaseMigrationEventHandler(
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager,
ITenantStore tenantStore,
IDistributedEventBus distributedEventBus
IDistributedEventBus distributedEventBus,
IServiceProvider serviceProvider
) : base(
currentTenant,
unitOfWorkManager,
tenantStore,
distributedEventBus,
CatalogServiceDbProperties.ConnectionStringName)
CatalogServiceDbProperties.ConnectionStringName,serviceProvider)
{
}

2
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj

@ -11,7 +11,7 @@
<ItemGroup>
<ProjectReference Include="..\EShopOnAbp.CatalogService.Application\EShopOnAbp.CatalogService.Application.csproj" />
<ProjectReference Include="..\EShopOnAbp.CatalogService.EntityFrameworkCore\EShopOnAbp.CatalogService.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\EShopOnAbp.CatalogService.MongoDB\EShopOnAbp.CatalogService.MongoDB.csproj" />
<ProjectReference Include="..\EShopOnAbp.CatalogService.HttpApi\EShopOnAbp.CatalogService.HttpApi.csproj" />
</ItemGroup>

15
services/catalog/src/EShopOnAbp.CatalogService.MongoDB/EShopOnAbp.CatalogService.MongoDB.csproj

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\..\common.props" />
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>EShopOnAbp.CatalogService</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.MongoDB" Version="5.0.0-rc.1" />
<ProjectReference Include="..\EShopOnAbp.CatalogService.Domain\EShopOnAbp.CatalogService.Domain.csproj" />
</ItemGroup>
</Project>

20
services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbContext.cs

@ -0,0 +1,20 @@
using Volo.Abp.Data;
using Volo.Abp.MongoDB;
namespace EShopOnAbp.CatalogService.MongoDB
{
[ConnectionStringName(CatalogServiceDbProperties.ConnectionStringName)]
public class CatalogServiceMongoDbContext : AbpMongoDbContext, ICatalogServiceMongoDbContext
{
/* Add mongo collections here. Example:
* public IMongoCollection<Question> Questions => Collection<Question>();
*/
protected override void CreateModel(IMongoModelBuilder modelBuilder)
{
base.CreateModel(modelBuilder);
modelBuilder.ConfigureCatalogService();
}
}
}

14
services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbContextExtensions.cs

@ -0,0 +1,14 @@
using Volo.Abp;
using Volo.Abp.MongoDB;
namespace EShopOnAbp.CatalogService.MongoDB
{
public static class CatalogServiceMongoDbContextExtensions
{
public static void ConfigureCatalogService(
this IMongoModelBuilder builder)
{
Check.NotNull(builder, nameof(builder));
}
}
}

23
services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbModule.cs

@ -0,0 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.MongoDB;
namespace EShopOnAbp.CatalogService.MongoDB
{
[DependsOn(
typeof(CatalogServiceDomainModule),
typeof(AbpMongoDbModule)
)]
public class CatalogServiceMongoDbModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddMongoDbContext<CatalogServiceMongoDbContext>(options =>
{
/* Add custom repositories here. Example:
* options.AddRepository<Question, MongoQuestionRepository>();
*/
});
}
}
}

13
services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/ICatalogServiceMongoDbContext.cs

@ -0,0 +1,13 @@
using Volo.Abp.Data;
using Volo.Abp.MongoDB;
namespace EShopOnAbp.CatalogService.MongoDB
{
[ConnectionStringName(CatalogServiceDbProperties.ConnectionStringName)]
public interface ICatalogServiceMongoDbContext : IAbpMongoDbContext
{
/* Define mongo collections here. Example:
* IMongoCollection<Question> Questions { get; }
*/
}
}

10
services/catalog/test/EShopOnAbp.CatalogService.Domain.Tests/CatalogServiceDomainCollection.cs

@ -0,0 +1,10 @@
using EShopOnAbp.CatalogService.MongoDB;
using Xunit;
namespace EShopOnAbp.CatalogService;
[CollectionDefinition(CatalogServiceTestConsts.CollectionDefinitionName)]
public class CatalogServiceDomainCollection : CatalogServiceMongoDbCollectionFixtureBase
{
}

4
services/catalog/test/EShopOnAbp.CatalogService.Domain.Tests/CatalogServiceDomainTestModule.cs

@ -1,10 +1,10 @@
using EShopOnAbp.CatalogService.EntityFrameworkCore;
using EShopOnAbp.CatalogService.MongoDB;
using Volo.Abp.Modularity;
namespace EShopOnAbp.CatalogService
{
[DependsOn(
typeof(CatalogServiceEntityFrameworkCoreTestModule)
typeof(CatalogServiceMongoDbTestModule)
)]
public class CatalogServiceDomainTestModule : AbpModule
{

2
services/catalog/test/EShopOnAbp.CatalogService.Domain.Tests/EShopOnAbp.CatalogService.Domain.Tests.csproj

@ -8,7 +8,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\EShopOnAbp.CatalogService.EntityFrameworkCore.Tests\EShopOnAbp.CatalogService.EntityFrameworkCore.Tests.csproj" />
<ProjectReference Include="..\EShopOnAbp.CatalogService.MongoDB.Tests\EShopOnAbp.CatalogService.MongoDB.Tests.csproj" />
</ItemGroup>
<ItemGroup>

9
services/catalog/test/EShopOnAbp.CatalogService.EntityFrameworkCore.Tests/EntityFrameworkCore/CatalogServiceEntityFrameworkCoreTestBase.cs

@ -1,9 +0,0 @@
using Volo.Abp;
namespace EShopOnAbp.CatalogService.EntityFrameworkCore
{
public abstract class CatalogServiceEntityFrameworkCoreTestBase : CatalogServiceTestBase<CatalogServiceEntityFrameworkCoreTestModule>
{
}
}

62
services/catalog/test/EShopOnAbp.CatalogService.EntityFrameworkCore.Tests/EntityFrameworkCore/CatalogServiceEntityFrameworkCoreTestModule.cs

@ -1,62 +0,0 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Sqlite;
using Volo.Abp.Modularity;
namespace EShopOnAbp.CatalogService.EntityFrameworkCore
{
[DependsOn(
typeof(CatalogServiceEntityFrameworkCoreModule),
typeof(CatalogServiceTestBaseModule),
typeof(AbpEntityFrameworkCoreSqliteModule)
)]
public class CatalogServiceEntityFrameworkCoreTestModule : AbpModule
{
private SqliteConnection _sqliteConnection;
public override void ConfigureServices(ServiceConfigurationContext context)
{
ConfigureInMemorySqlite(context.Services);
}
private void ConfigureInMemorySqlite(IServiceCollection services)
{
_sqliteConnection = CreateDatabaseAndGetConnection();
services.Configure<AbpDbContextOptions>(options =>
{
options.Configure(context =>
{
context.DbContextOptions.UseSqlite(_sqliteConnection);
});
});
}
public override void OnApplicationShutdown(ApplicationShutdownContext context)
{
_sqliteConnection.Dispose();
}
private static SqliteConnection CreateDatabaseAndGetConnection()
{
var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<CatalogServiceDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new CatalogServiceDbContext(options))
{
context.GetService<IRelationalDatabaseCreator>().CreateTables();
}
return connection;
}
}
}

9
services/catalog/test/EShopOnAbp.CatalogService.EntityFrameworkCore.Tests/EShopOnAbp.CatalogService.EntityFrameworkCore.Tests.csproj → services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/EShopOnAbp.CatalogService.MongoDB.Tests.csproj

@ -7,14 +7,11 @@
<RootNamespace>EShopOnAbp.CatalogService</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\EShopOnAbp.CatalogService.EntityFrameworkCore\EShopOnAbp.CatalogService.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\EShopOnAbp.CatalogService.TestBase\EShopOnAbp.CatalogService.TestBase.csproj" />
<PackageReference Include="Volo.Abp.EntityFrameworkCore.Sqlite" Version="5.0.0-rc.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<ProjectReference Include="..\..\src\EShopOnAbp.CatalogService.MongoDB\EShopOnAbp.CatalogService.MongoDB.csproj" />
<ProjectReference Include="..\EShopOnAbp.CatalogService.TestBase\EShopOnAbp.CatalogService.TestBase.csproj" />
</ItemGroup>
</Project>

9
services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoCollection.cs

@ -0,0 +1,9 @@
using Xunit;
namespace EShopOnAbp.CatalogService.MongoDB;
[CollectionDefinition(CatalogServiceTestConsts.CollectionDefinitionName)]
public class CatalogServiceMongoCollection : CatalogServiceMongoDbCollectionFixtureBase
{
}

8
services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoDbCollectionFixtureBase.cs

@ -0,0 +1,8 @@
using Xunit;
namespace EShopOnAbp.CatalogService.MongoDB;
public class CatalogServiceMongoDbCollectionFixtureBase : ICollectionFixture<CatalogServiceMongoDbFixture>
{
}

22
services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoDbFixture.cs

@ -0,0 +1,22 @@
using System;
using Mongo2Go;
namespace EShopOnAbp.CatalogService.MongoDB
{
public class CatalogServiceMongoDbFixture : IDisposable
{
private static readonly MongoDbRunner MongoDbRunner;
public static readonly string ConnectionString;
static CatalogServiceMongoDbFixture()
{
MongoDbRunner = MongoDbRunner.Start(singleNodeReplSet: true, singleNodeReplSetWaitTimeout: 20);
ConnectionString = MongoDbRunner.ConnectionString;
}
public void Dispose()
{
MongoDbRunner?.Dispose();
}
}
}

10
services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoDbTestBase.cs

@ -0,0 +1,10 @@
namespace EShopOnAbp.CatalogService.MongoDB
{
/* This class can be used as a base class for MongoDB integration tests,
* while SampleRepository_Tests uses a different approach.
*/
public abstract class CatalogServiceMongoDbTestBase : CatalogServiceTestBase<CatalogServiceMongoDbTestModule>
{
}
}

27
services/catalog/test/EShopOnAbp.CatalogService.MongoDB.Tests/MongoDB/CatalogServiceMongoDbTestModule.cs

@ -0,0 +1,27 @@
using System;
using Volo.Abp.Data;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
namespace EShopOnAbp.CatalogService.MongoDB
{
[DependsOn(
typeof(CatalogServiceTestBaseModule),
typeof(CatalogServiceMongoDbModule)
)]
public class CatalogServiceMongoDbTestModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var stringArray = CatalogServiceMongoDbFixture.ConnectionString.Split('?');
var connectionString = stringArray[0].EnsureEndsWith('/') +
"Db_" +
Guid.NewGuid().ToString("N") + "/?" + stringArray[1];
Configure<AbpDbConnectionOptions>(options =>
{
options.ConnectionStrings.Default = connectionString;
});
}
}
}

6
services/catalog/test/EShopOnAbp.CatalogService.TestBase/CatalogServiceTestConsts.cs

@ -0,0 +1,6 @@
namespace EShopOnAbp.CatalogService;
public static class CatalogServiceTestConsts
{
public const string CollectionDefinitionName = "CatalogService collection";
}

4
services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs

@ -1,7 +1,7 @@
using EShopOnAbp.IdentityService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using System;
using System.Threading.Tasks;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.EventBus.Local;
using Volo.Abp.MultiTenancy;
@ -9,7 +9,7 @@ using Volo.Abp.Uow;
namespace EShopOnAbp.IdentityService.DbMigrations
{
public class IdentityServiceDatabaseMigrationChecker : PendingMigrationsCheckerBase<IdentityServiceDbContext>
public class IdentityServiceDatabaseMigrationChecker : PendingEfCoreMigrationsChecker<IdentityServiceDbContext>
{
protected ILocalEventBus LocalEventBus { get; }

4
services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs

@ -1,9 +1,9 @@
using EShopOnAbp.IdentityService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using Serilog;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.EventBus.Local;
@ -14,7 +14,7 @@ using Volo.Abp.Uow;
namespace EShopOnAbp.IdentityService.DbMigrations
{
public class IdentityServiceDatabaseMigrationEventHandler
: DatabaseMigrationEventHandlerBase<IdentityServiceDbContext>,
: DatabaseEfCoreMigrationEventHandler<IdentityServiceDbContext>,
IDistributedEventHandler<TenantCreatedEto>,
IDistributedEventHandler<ApplyDatabaseMigrationsEto>
{

4
services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationChecker.cs

@ -1,13 +1,13 @@
using EShopOnAbp.OrderingService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using System;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace EShopOnAbp.OrderingService.DbMigrations
{
public class OrderingServiceDatabaseMigrationChecker : PendingMigrationsCheckerBase<OrderingServiceDbContext>
public class OrderingServiceDatabaseMigrationChecker : PendingEfCoreMigrationsChecker<OrderingServiceDbContext>
{
public OrderingServiceDatabaseMigrationChecker(
IUnitOfWorkManager unitOfWorkManager,

4
services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs

@ -1,7 +1,7 @@
using EShopOnAbp.OrderingService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using System;
using System.Threading.Tasks;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
@ -10,7 +10,7 @@ using Volo.Abp.Uow;
namespace EShopOnAbp.OrderingService.DbMigrations
{
public class OrderingServiceDatabaseMigrationEventHandler
: DatabaseMigrationEventHandlerBase<OrderingServiceDbContext>,
: DatabaseEfCoreMigrationEventHandler<OrderingServiceDbContext>,
IDistributedEventHandler<ApplyDatabaseMigrationsEto>
{
public OrderingServiceDatabaseMigrationEventHandler(

8
services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationChecker.cs

@ -1,17 +1,13 @@
using EShopOnAbp.PaymentService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace EShopOnAbp.PaymentService.DbMigrations
{
public class PaymentServiceDatabaseMigrationChecker : PendingMigrationsCheckerBase<PaymentServiceDbContext>
public class PaymentServiceDatabaseMigrationChecker : PendingEfCoreMigrationsChecker<PaymentServiceDbContext>
{
public PaymentServiceDatabaseMigrationChecker(
IUnitOfWorkManager unitOfWorkManager,

4
services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs

@ -1,7 +1,7 @@
using EShopOnAbp.PaymentService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
using System;
using System.Threading.Tasks;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
@ -10,7 +10,7 @@ using Volo.Abp.Uow;
namespace EShopOnAbp.PaymentService.DbMigrations
{
public class PaymentServiceDatabaseMigrationEventHandler
: DatabaseMigrationEventHandlerBase<PaymentServiceDbContext>,
: DatabaseEfCoreMigrationEventHandler<PaymentServiceDbContext>,
IDistributedEventHandler<ApplyDatabaseMigrationsEto>
{
public PaymentServiceDatabaseMigrationEventHandler(

188
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/DatabaseMigrationEventHandlerBase.cs

@ -1,185 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Serilog;
using Volo.Abp;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
using Volo.Abp.DependencyInjection;
namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations
namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
public abstract class DatabaseMigrationEventHandlerBase : ITransientDependency
{
public abstract class DatabaseMigrationEventHandlerBase<TDbContext> :
ITransientDependency
where TDbContext : DbContext, IEfCoreDbContext
{
protected const string TryCountPropertyName = "TryCount";
protected const int MaxEventTryCount = 3;
protected ICurrentTenant CurrentTenant { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected ITenantStore TenantStore { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected ILogger<DatabaseMigrationEventHandlerBase<TDbContext>> Logger { get; set; }
protected string DatabaseName { get; }
protected DatabaseMigrationEventHandlerBase(
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager,
ITenantStore tenantStore,
IDistributedEventBus distributedEventBus,
string databaseName)
{
CurrentTenant = currentTenant;
UnitOfWorkManager = unitOfWorkManager;
TenantStore = tenantStore;
DatabaseName = databaseName;
DistributedEventBus = distributedEventBus;
Logger = NullLogger<DatabaseMigrationEventHandlerBase<TDbContext>>.Instance;
}
/// <summary>
/// Apply pending EF Core schema migrations to the database.
/// Returns true if any migration has applied.
/// </summary>
protected virtual async Task<bool> MigrateDatabaseSchemaAsync(Guid? tenantId)
{
var result = false;
using (CurrentTenant.Change(tenantId))
{
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
async Task<bool> MigrateDatabaseSchemaWithDbContextAsync()
{
var dbContext = await uow.ServiceProvider
.GetRequiredService<IDbContextProvider<TDbContext>>()
.GetDbContextAsync();
if ((await dbContext.Database.GetPendingMigrationsAsync()).Any())
{
await dbContext.Database.MigrateAsync();
return true;
}
return false;
}
if (tenantId == null)
{
//Migrating the host database
result = await MigrateDatabaseSchemaWithDbContextAsync();
}
else
{
var tenantConfiguration = await TenantStore.FindAsync(tenantId.Value);
if (!tenantConfiguration.ConnectionStrings.Default.IsNullOrWhiteSpace() ||
!tenantConfiguration.ConnectionStrings.GetOrDefault(DatabaseName).IsNullOrWhiteSpace())
{
//Migrating the tenant database (only if tenant has a separate database)
result = await MigrateDatabaseSchemaWithDbContextAsync();
}
}
await uow.CompleteAsync();
}
}
return result;
}
protected virtual async Task HandleErrorOnApplyDatabaseMigrationAsync(
ApplyDatabaseMigrationsEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Log.Warning($"Could not apply database migrations. Re-queueing the operation. TenantId = {eventData.TenantId}, Database Name = {eventData.DatabaseName}.");
Log.Error(exception.ToString());
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
Log.Warning("Re publishing the event!");
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Log.Error($"Could not apply database migrations. Canceling the operation. TenantId = {eventData.TenantId}, DatabaseName = {eventData.DatabaseName}.");
Log.Error(exception.ToString());
}
}
protected virtual async Task HandleErrorTenantCreatedAsync(
TenantCreatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning($"Could not perform tenant created event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError($"Could not perform tenant created event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
protected virtual async Task HandleErrorTenantConnectionStringUpdatedAsync(
TenantConnectionStringUpdatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning($"Could not perform tenant connection string updated event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError($"Could not perform tenant connection string updated event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
private static int GetEventTryCount(EtoBase eventData)
{
var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName);
if (tryCountAsString.IsNullOrEmpty())
{
return 0;
}
return int.Parse(tryCountAsString);
}
private static void SetEventTryCount(EtoBase eventData, int count)
{
eventData.Properties[TryCountPropertyName] = count.ToString();
}
private static int IncrementEventTryCount(EtoBase eventData)
{
var count = GetEventTryCount(eventData) + 1;
SetEventTryCount(eventData, count);
return count;
}
}
}
}

188
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs

@ -0,0 +1,188 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Serilog;
using Volo.Abp;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
public abstract class DatabaseEfCoreMigrationEventHandler<TDbContext> : DatabaseMigrationEventHandlerBase
where TDbContext : DbContext, IEfCoreDbContext
{
protected const string TryCountPropertyName = "TryCount";
protected const int MaxEventTryCount = 3;
protected ICurrentTenant CurrentTenant { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected ITenantStore TenantStore { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected ILogger<DatabaseEfCoreMigrationEventHandler<TDbContext>> Logger { get; set; }
protected string DatabaseName { get; }
protected DatabaseEfCoreMigrationEventHandler(
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager,
ITenantStore tenantStore,
IDistributedEventBus distributedEventBus,
string databaseName)
{
CurrentTenant = currentTenant;
UnitOfWorkManager = unitOfWorkManager;
TenantStore = tenantStore;
DatabaseName = databaseName;
DistributedEventBus = distributedEventBus;
Logger = NullLogger<DatabaseEfCoreMigrationEventHandler<TDbContext>>.Instance;
}
/// <summary>
/// Apply pending EF Core schema migrations to the database.
/// Returns true if any migration has applied.
/// </summary>
protected virtual async Task<bool> MigrateDatabaseSchemaAsync(Guid? tenantId)
{
var result = false;
using (CurrentTenant.Change(tenantId))
{
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
async Task<bool> MigrateDatabaseSchemaWithDbContextAsync()
{
var dbContext = await uow.ServiceProvider
.GetRequiredService<IDbContextProvider<TDbContext>>()
.GetDbContextAsync();
if ((await dbContext.Database.GetPendingMigrationsAsync()).Any())
{
await dbContext.Database.MigrateAsync();
return true;
}
return false;
}
if (tenantId == null)
{
//Migrating the host database
result = await MigrateDatabaseSchemaWithDbContextAsync();
}
else
{
var tenantConfiguration = await TenantStore.FindAsync(tenantId.Value);
if (!tenantConfiguration.ConnectionStrings.Default.IsNullOrWhiteSpace() ||
!tenantConfiguration.ConnectionStrings.GetOrDefault(DatabaseName).IsNullOrWhiteSpace())
{
//Migrating the tenant database (only if tenant has a separate database)
result = await MigrateDatabaseSchemaWithDbContextAsync();
}
}
await uow.CompleteAsync();
}
}
return result;
}
protected virtual async Task HandleErrorOnApplyDatabaseMigrationAsync(
ApplyDatabaseMigrationsEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Log.Warning(
$"Could not apply database migrations. Re-queueing the operation. TenantId = {eventData.TenantId}, Database Name = {eventData.DatabaseName}.");
Log.Error(exception.ToString());
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
Log.Warning("Re publishing the event!");
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Log.Error(
$"Could not apply database migrations. Canceling the operation. TenantId = {eventData.TenantId}, DatabaseName = {eventData.DatabaseName}.");
Log.Error(exception.ToString());
}
}
protected virtual async Task HandleErrorTenantCreatedAsync(
TenantCreatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning(
$"Could not perform tenant created event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError(
$"Could not perform tenant created event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
protected virtual async Task HandleErrorTenantConnectionStringUpdatedAsync(
TenantConnectionStringUpdatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning(
$"Could not perform tenant connection string updated event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError(
$"Could not perform tenant connection string updated event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
private static int GetEventTryCount(EtoBase eventData)
{
var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName);
if (tryCountAsString.IsNullOrEmpty())
{
return 0;
}
return int.Parse(tryCountAsString);
}
private static void SetEventTryCount(EtoBase eventData, int count)
{
eventData.Properties[TryCountPropertyName] = count.ToString();
}
private static int IncrementEventTryCount(EtoBase eventData)
{
var count = GetEventTryCount(eventData) + 1;
SetEventTryCount(eventData, count);
return count;
}
}

67
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs

@ -0,0 +1,67 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
public abstract class PendingEfCoreMigrationsChecker<TDbContext> : PendingMigrationsCheckerBase
where TDbContext : DbContext
{
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IServiceProvider ServiceProvider { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected string DatabaseName { get; }
protected PendingEfCoreMigrationsChecker(
IUnitOfWorkManager unitOfWorkManager,
IServiceProvider serviceProvider,
ICurrentTenant currentTenant,
IDistributedEventBus distributedEventBus,
string databaseName)
{
UnitOfWorkManager = unitOfWorkManager;
ServiceProvider = serviceProvider;
CurrentTenant = currentTenant;
DistributedEventBus = distributedEventBus;
DatabaseName = databaseName;
}
public virtual async Task<bool> CheckAsync()
{
var isMigrationRequired = false;
using (CurrentTenant.Change(null))
{
// Create database tables if needed
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
var pendingMigrations = await ServiceProvider
.GetRequiredService<TDbContext>()
.Database
.GetPendingMigrationsAsync();
if (pendingMigrations.Any())
{
await DistributedEventBus.PublishAsync(
new ApplyDatabaseMigrationsEto
{
DatabaseName = DatabaseName
}
);
isMigrationRequired = true;
}
await uow.CompleteAsync();
}
return isMigrationRequired;
}
}
}

204
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs

@ -0,0 +1,204 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using MongoDB.Driver;
using Volo.Abp;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MongoDB;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.MongoDb;
public abstract class DatabaseMongoDbMigrationEventHandler<TDbContext> : DatabaseMigrationEventHandlerBase
where TDbContext : AbpMongoDbContext, IAbpMongoDbContext
{
protected const string TryCountPropertyName = "TryCount";
protected const int MaxEventTryCount = 3;
protected ICurrentTenant CurrentTenant { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected ITenantStore TenantStore { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected ILogger<DatabaseMongoDbMigrationEventHandler<TDbContext>> Logger { get; set; }
protected IServiceProvider ServiceProvider { get; }
protected string DatabaseName { get; }
protected DatabaseMongoDbMigrationEventHandler(
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager,
ITenantStore tenantStore,
IDistributedEventBus distributedEventBus,
string databaseName, IServiceProvider serviceProvider)
{
CurrentTenant = currentTenant;
UnitOfWorkManager = unitOfWorkManager;
TenantStore = tenantStore;
DatabaseName = databaseName;
ServiceProvider = serviceProvider;
DistributedEventBus = distributedEventBus;
Logger = NullLogger<DatabaseMongoDbMigrationEventHandler<TDbContext>>.Instance;
}
/// <summary>
/// Apply pending EF Core schema migrations to the database.
/// Returns true if any migration has applied.
/// </summary>
protected virtual async Task<bool> MigrateDatabaseSchemaAsync(Guid? tenantId)
{
var result = false;
using (CurrentTenant.Change(tenantId))
{
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
async Task<bool> MigrateDatabaseSchemaWithDbContextAsync()
{
var dbContexts = ServiceProvider.GetServices<IAbpMongoDbContext>();
var connectionStringResolver = ServiceProvider.GetRequiredService<IConnectionStringResolver>();
foreach (var dbContext in dbContexts)
{
var connectionString =
await connectionStringResolver.ResolveAsync(
ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType()));
if (connectionString.IsNullOrWhiteSpace())
{
continue;
}
var mongoUrl = new MongoUrl(connectionString);
var databaseName = mongoUrl.DatabaseName;
var client = new MongoClient(mongoUrl);
if (databaseName.IsNullOrWhiteSpace())
{
databaseName = ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType());
}
(dbContext as AbpMongoDbContext)?.InitializeCollections(client.GetDatabase(databaseName));
}
return true;
}
if (tenantId == null)
{
//Migrating the host database
result = await MigrateDatabaseSchemaWithDbContextAsync();
}
else
{
var tenantConfiguration = await TenantStore.FindAsync(tenantId.Value);
if (!tenantConfiguration.ConnectionStrings.Default.IsNullOrWhiteSpace() ||
!tenantConfiguration.ConnectionStrings.GetOrDefault(DatabaseName).IsNullOrWhiteSpace())
{
//Migrating the tenant database (only if tenant has a separate database)
result = await MigrateDatabaseSchemaWithDbContextAsync();
}
}
await uow.CompleteAsync();
}
}
return result;
}
protected virtual async Task HandleErrorOnApplyDatabaseMigrationAsync(
ApplyDatabaseMigrationsEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning(
$"Could not apply database migrations. Re-queueing the operation. TenantId = {eventData.TenantId}, Database Name = {eventData.DatabaseName}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError(
$"Could not apply database migrations. Canceling the operation. TenantId = {eventData.TenantId}, DatabaseName = {eventData.DatabaseName}.");
Logger.LogException(exception);
}
}
protected virtual async Task HandleErrorTenantCreatedAsync(
TenantCreatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning(
$"Could not perform tenant created event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError(
$"Could not perform tenant created event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
protected virtual async Task HandleErrorTenantConnectionStringUpdatedAsync(
TenantConnectionStringUpdatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning(
$"Could not perform tenant connection string updated event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError(
$"Could not perform tenant connection string updated event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
private static int GetEventTryCount(EtoBase eventData)
{
var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName);
if (tryCountAsString.IsNullOrEmpty())
{
return 0;
}
return int.Parse(tryCountAsString);
}
private static void SetEventTryCount(EtoBase eventData, int count)
{
eventData.Properties[TryCountPropertyName] = count.ToString();
}
private static int IncrementEventTryCount(EtoBase eventData)
{
var count = GetEventTryCount(eventData) + 1;
SetEventTryCount(eventData, count);
return count;
}
}

52
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs

@ -0,0 +1,52 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MongoDB;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
public class PendingMongoDbMigrationsChecker<TDbContext> : PendingMigrationsCheckerBase
where TDbContext : AbpMongoDbContext
{
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IServiceProvider ServiceProvider { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected string DatabaseName { get; }
protected PendingMongoDbMigrationsChecker(
IUnitOfWorkManager unitOfWorkManager,
IServiceProvider serviceProvider,
ICurrentTenant currentTenant,
IDistributedEventBus distributedEventBus,
string databaseName)
{
UnitOfWorkManager = unitOfWorkManager;
ServiceProvider = serviceProvider;
CurrentTenant = currentTenant;
DistributedEventBus = distributedEventBus;
DatabaseName = databaseName;
}
public virtual async Task CheckAsync()
{
using (CurrentTenant.Change(null))
{
// Create database tables if needed
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
await DistributedEventBus.PublishAsync(
new ApplyDatabaseMigrationsEto
{
DatabaseName = DatabaseName
}
);
await uow.CompleteAsync();
}
}
}
}

65
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs

@ -1,69 +1,8 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
using Volo.Abp.DependencyInjection;
namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations
{
public abstract class PendingMigrationsCheckerBase<TDbContext> : ITransientDependency
where TDbContext : DbContext
public abstract class PendingMigrationsCheckerBase : ITransientDependency
{
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IServiceProvider ServiceProvider { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected string DatabaseName { get; }
protected PendingMigrationsCheckerBase(
IUnitOfWorkManager unitOfWorkManager,
IServiceProvider serviceProvider,
ICurrentTenant currentTenant,
IDistributedEventBus distributedEventBus,
string databaseName)
{
UnitOfWorkManager = unitOfWorkManager;
ServiceProvider = serviceProvider;
CurrentTenant = currentTenant;
DistributedEventBus = distributedEventBus;
DatabaseName = databaseName;
}
public virtual async Task<bool> CheckAsync()
{
var isMigrationRequired = false;
using (CurrentTenant.Change(null))
{
// Create database tables if needed
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
var pendingMigrations = await ServiceProvider
.GetRequiredService<TDbContext>()
.Database
.GetPendingMigrationsAsync();
if (pendingMigrations.Any())
{
await DistributedEventBus.PublishAsync(
new ApplyDatabaseMigrationsEto
{
DatabaseName = DatabaseName
}
);
isMigrationRequired = true;
}
await uow.CompleteAsync();
}
return isMigrationRequired;
}
}
}
}

1
shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj

@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.1" />
<PackageReference Include="Volo.Abp.MongoDB" Version="5.0.0-rc.1" />
</ItemGroup>
<ItemGroup>

Loading…
Cancel
Save