diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/ApplyDatabaseSeedsEto.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/ApplyDatabaseSeedsEto.cs new file mode 100644 index 00000000..75322372 --- /dev/null +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/ApplyDatabaseSeedsEto.cs @@ -0,0 +1,11 @@ +using Volo.Abp.Domain.Entities.Events.Distributed; +using Volo.Abp.EventBus; + +namespace EShopOnAbp.IdentityService.DbMigrations +{ + + [EventName("abp.data.apply_database_migrations")] + public class ApplyDatabaseSeedsEto : EtoBase + { + } +} diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/DataSeederEventHandler.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/DataSeederEventHandler.cs new file mode 100644 index 00000000..3bf1d3f4 --- /dev/null +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/DataSeederEventHandler.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading.Tasks; +using Volo.Abp.Data; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus; + +namespace EShopOnAbp.IdentityService.DbMigrations +{ + public class DataSeederEventHandler : ILocalEventHandler, ITransientDependency + { + protected IDataSeeder DataSeeder { get; } + + public DataSeederEventHandler(IDataSeeder dataSeeder) + { + DataSeeder = dataSeeder; + } + + public async Task HandleEventAsync(ApplyDatabaseSeedsEto eventData) + { + await DataSeeder.SeedAsync(); + } + } +} diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs index 8305c223..a71d613b 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs @@ -1,7 +1,9 @@ using EShopOnAbp.IdentityService.EntityFrameworkCore; using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations; using System; +using System.Threading.Tasks; using Volo.Abp.EventBus.Distributed; +using Volo.Abp.EventBus.Local; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; @@ -9,11 +11,14 @@ namespace EShopOnAbp.IdentityService.DbMigrations { public class IdentityServiceDatabaseMigrationChecker : PendingMigrationsCheckerBase { + protected ILocalEventBus LocalEventBus { get; } + public IdentityServiceDatabaseMigrationChecker( IUnitOfWorkManager unitOfWorkManager, IServiceProvider serviceProvider, ICurrentTenant currentTenant, - IDistributedEventBus distributedEventBus) + IDistributedEventBus distributedEventBus, + ILocalEventBus localEventBus) : base( unitOfWorkManager, serviceProvider, @@ -21,7 +26,20 @@ namespace EShopOnAbp.IdentityService.DbMigrations distributedEventBus, IdentityServiceDbProperties.ConnectionStringName) { - + LocalEventBus = localEventBus; + } + + + public override async Task CheckAsync() + { + var isMigrationRequired = await base.CheckAsync(); + + if (!isMigrationRequired) + { + await LocalEventBus.PublishAsync(new ApplyDatabaseSeedsEto()); + } + + return isMigrationRequired; } } } \ No newline at end of file diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs index ad418510..c63ec2f5 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Data; using Volo.Abp.EventBus.Distributed; +using Volo.Abp.EventBus.Local; using Volo.Abp.Identity; using Volo.Abp.MultiTenancy; using Volo.Abp.TenantManagement; @@ -20,6 +21,7 @@ namespace EShopOnAbp.IdentityService.DbMigrations { private readonly IIdentityDataSeeder _identityDataSeeder; private readonly IdentityServerDataSeeder _identityServerDataSeeder; + private readonly ILocalEventBus _localEventBus; public IdentityServiceDatabaseMigrationEventHandler( ICurrentTenant currentTenant, @@ -28,7 +30,8 @@ namespace EShopOnAbp.IdentityService.DbMigrations IIdentityDataSeeder identityDataSeeder, IdentityServerDataSeeder identityServerDataSeeder, ITenantRepository tenantRepository, - IDistributedEventBus distributedEventBus + IDistributedEventBus distributedEventBus, + ILocalEventBus localEventBus ) : base( currentTenant, unitOfWorkManager, @@ -39,6 +42,7 @@ namespace EShopOnAbp.IdentityService.DbMigrations { _identityDataSeeder = identityDataSeeder; _identityServerDataSeeder = identityServerDataSeeder; + _localEventBus = localEventBus; } public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData) @@ -62,11 +66,14 @@ namespace EShopOnAbp.IdentityService.DbMigrations /* Migrate tenant databases after host migration */ await QueueTenantMigrationsAsync(); } + + await _localEventBus.PublishAsync(new ApplyDatabaseSeedsEto()); } catch (Exception ex) { await HandleErrorOnApplyDatabaseMigrationAsync(eventData, ex); } + } public async Task HandleEventAsync(TenantCreatedEto eventData) diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs index 906ecea8..2f70a5ae 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs @@ -70,13 +70,6 @@ namespace EShopOnAbp.IdentityService .GetRequiredService() .CheckAsync() ); - - AsyncHelper.RunSync(async () => - { - await scope.ServiceProvider - .GetRequiredService() - .SeedAsync(); - }); } } } diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs index c910d28e..7b168b75 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs @@ -34,8 +34,10 @@ namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations DatabaseName = databaseName; } - public virtual async Task CheckAsync() + public virtual async Task CheckAsync() { + var isMigrationRequired = false; + using (CurrentTenant.Change(null)) { // Create database tables if needed @@ -54,10 +56,13 @@ namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations DatabaseName = DatabaseName } ); + isMigrationRequired = true; } await uow.CompleteAsync(); } + + return isMigrationRequired; } } }