Browse Source

Make data seeding dependent of an event

gterdem/deployment_k8s
enisn 5 years ago
parent
commit
77321c2546
  1. 11
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/ApplyDatabaseSeedsEto.cs
  2. 23
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/DataSeederEventHandler.cs
  3. 22
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs
  4. 9
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs
  5. 7
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs
  6. 7
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs

11
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
{
}
}

23
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<ApplyDatabaseSeedsEto>, ITransientDependency
{
protected IDataSeeder DataSeeder { get; }
public DataSeederEventHandler(IDataSeeder dataSeeder)
{
DataSeeder = dataSeeder;
}
public async Task HandleEventAsync(ApplyDatabaseSeedsEto eventData)
{
await DataSeeder.SeedAsync();
}
}
}

22
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<IdentityServiceDbContext>
{
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<bool> CheckAsync()
{
var isMigrationRequired = await base.CheckAsync();
if (!isMigrationRequired)
{
await LocalEventBus.PublishAsync(new ApplyDatabaseSeedsEto());
}
return isMigrationRequired;
}
}
}

9
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)

7
services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs

@ -70,13 +70,6 @@ namespace EShopOnAbp.IdentityService
.GetRequiredService<IdentityServiceDatabaseMigrationChecker>()
.CheckAsync()
);
AsyncHelper.RunSync(async () =>
{
await scope.ServiceProvider
.GetRequiredService<IDataSeeder>()
.SeedAsync();
});
}
}
}

7
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<bool> 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;
}
}
}

Loading…
Cancel
Save