diff --git a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs index 2fb26184..41172faa 100644 --- a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs +++ b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs @@ -1,10 +1,9 @@ using EShopOnAbp.AdministrationService.EntityFrameworkCore; using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; -using Microsoft.Extensions.Logging; +using Serilog; using System; using System.Linq; using System.Threading.Tasks; -using Serilog; using Volo.Abp.Authorization.Permissions; using Volo.Abp.Data; using Volo.Abp.DistributedLocking; @@ -17,7 +16,6 @@ namespace EShopOnAbp.AdministrationService.DbMigrations { public class AdministrationServiceDatabaseMigrationEventHandler : DatabaseEfCoreMigrationEventHandler, - IDistributedEventHandler, IDistributedEventHandler { private readonly IPermissionDefinitionManager _permissionDefinitionManager; @@ -59,8 +57,8 @@ namespace EShopOnAbp.AdministrationService.DbMigrations if (handle != null) { - await MigrateDatabaseSchemaAsync(eventData.TenantId); - await SeedDataAsync(eventData.TenantId); + await MigrateDatabaseSchemaAsync(); + await SeedDataAsync(); } } } @@ -70,48 +68,30 @@ namespace EShopOnAbp.AdministrationService.DbMigrations } } - public async Task HandleEventAsync(TenantCreatedEto eventData) + private async Task SeedDataAsync() { - try - { - await MigrateDatabaseSchemaAsync(eventData.Id); - Logger.LogInformation("Starting AdministrationService DataSeeder..."); - await SeedDataAsync(eventData.Id); - } - catch (Exception ex) - { - await HandleErrorTenantCreatedAsync(eventData, ex); - } - } - private async Task SeedDataAsync(Guid? tenantId) - { - using (CurrentTenant.Change(tenantId)) + using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true)) { - using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true)) - { - var multiTenancySide = tenantId == null - ? MultiTenancySides.Host - : MultiTenancySides.Tenant; + var multiTenancySide = MultiTenancySides.Host; - var permissionNames = _permissionDefinitionManager - .GetPermissions() - .Where(p => p.MultiTenancySide.HasFlag(multiTenancySide)) - .Where(p => !p.Providers.Any() || - p.Providers.Contains(RolePermissionValueProvider.ProviderName)) - .Select(p => p.Name) - .ToArray(); + var permissionNames = _permissionDefinitionManager + .GetPermissions() + .Where(p => p.MultiTenancySide.HasFlag(multiTenancySide)) + .Where(p => !p.Providers.Any() || + p.Providers.Contains(RolePermissionValueProvider.ProviderName)) + .Select(p => p.Name) + .ToArray(); - await _permissionDataSeeder.SeedAsync( - RolePermissionValueProvider.ProviderName, - "admin", - permissionNames, - tenantId - ); + await _permissionDataSeeder.SeedAsync( + RolePermissionValueProvider.ProviderName, + "admin", + permissionNames + ); - await uow.CompleteAsync(); - } + await uow.CompleteAsync(); } + } } } \ No newline at end of file diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs index 0ad78438..9b4e2647 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs @@ -54,7 +54,7 @@ namespace EShopOnAbp.CatalogService.DbMigrations if (handle != null) { Log.Information("CatalogService is migrating database..."); - await MigrateDatabaseSchemaAsync(null); + await MigrateDatabaseSchemaAsync(); } } } 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 71c99268..728112b4 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs @@ -1,22 +1,20 @@ using EShopOnAbp.IdentityService.EntityFrameworkCore; +using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; 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.DistributedLocking; using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Local; using Volo.Abp.Identity; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; -using Volo.Abp.DistributedLocking; namespace EShopOnAbp.IdentityService.DbMigrations { public class IdentityServiceDatabaseMigrationEventHandler : DatabaseEfCoreMigrationEventHandler, - IDistributedEventHandler, IDistributedEventHandler { private readonly IIdentityDataSeeder _identityDataSeeder; @@ -57,14 +55,13 @@ namespace EShopOnAbp.IdentityService.DbMigrations await using (var handle = await DistributedLockProvider.TryAcquireAsync(DatabaseName)) { Log.Information("IdentityService has acquired lock for db migration..."); - + if (handle != null) { Log.Information("IdentityService is migrating database..."); - await MigrateDatabaseSchemaAsync(eventData.TenantId); + await MigrateDatabaseSchemaAsync(); Log.Information("IdentityService is seeding data..."); await SeedDataAsync( - tenantId: eventData.TenantId, adminEmail: IdentityServiceDbProperties.DefaultAdminEmailAddress, adminPassword: IdentityServiceDbProperties.DefaultAdminPassword ); @@ -79,38 +76,17 @@ namespace EShopOnAbp.IdentityService.DbMigrations } } - public async Task HandleEventAsync(TenantCreatedEto eventData) + private async Task SeedDataAsync(string adminEmail, string adminPassword) { - try - { - await MigrateDatabaseSchemaAsync(eventData.Id); - await SeedDataAsync( - tenantId: eventData.Id, - adminEmail: eventData.Properties.GetOrDefault(IdentityDataSeedContributor.AdminEmailPropertyName) ?? IdentityServiceDbProperties.DefaultAdminEmailAddress, - adminPassword: eventData.Properties.GetOrDefault(IdentityDataSeedContributor.AdminPasswordPropertyName) ?? IdentityServiceDbProperties.DefaultAdminPassword - ); - } - catch (Exception ex) - { - await HandleErrorTenantCreatedAsync(eventData, ex); - } - } - private async Task SeedDataAsync(Guid? tenantId, string adminEmail, string adminPassword) - { - using (CurrentTenant.Change(tenantId)) - { - if (tenantId == null) - { - Log.Information($"Seeding IdentityServer data..."); - await _identityServerDataSeeder.SeedAsync(); - } - Log.Information($"Seeding user data..."); - await _identityDataSeeder.SeedAsync( - adminEmail, - adminPassword, - tenantId - ); - } + Log.Information($"Seeding IdentityServer data..."); + await _identityServerDataSeeder.SeedAsync(); + + Log.Information($"Seeding user data..."); + await _identityDataSeeder.SeedAsync( + adminEmail, + adminPassword + ); + } } } diff --git a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs index b2402578..ee060303 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs @@ -56,7 +56,7 @@ namespace EShopOnAbp.OrderingService.DbMigrations if (handle != null) { Log.Information("OrderingService is migrating database..."); - await MigrateDatabaseSchemaAsync(null); + await MigrateDatabaseSchemaAsync(); Log.Information("OrderingService is seeding data..."); await _dataSeeder.SeedAsync(); } diff --git a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs index 2f447e7b..cc30e4df 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs @@ -52,7 +52,7 @@ namespace EShopOnAbp.PaymentService.DbMigrations if (handle != null) { Log.Information("PaymentService is migrating database..."); - await MigrateDatabaseSchemaAsync(null); + await MigrateDatabaseSchemaAsync(); } } } diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs index d35f2b91..2c758b8a 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs @@ -1,13 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Medallion.Threading; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Serilog; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Data; using Volo.Abp.DistributedLocking; @@ -56,51 +55,36 @@ public abstract class DatabaseEfCoreMigrationEventHandler : Database /// Apply pending EF Core schema migrations to the database. /// Returns true if any migration has applied. /// - protected virtual async Task MigrateDatabaseSchemaAsync(Guid? tenantId) + protected virtual async Task MigrateDatabaseSchemaAsync() { var result = false; - using (CurrentTenant.Change(tenantId)) + + using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false)) { - using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false)) + async Task MigrateDatabaseSchemaWithDbContextAsync() { - async Task MigrateDatabaseSchemaWithDbContextAsync() - { - var dbContext = await uow.ServiceProvider - .GetRequiredService>() - .GetDbContextAsync(); - - if ((await dbContext.Database.GetPendingMigrationsAsync()).Any()) - { - await dbContext.Database.MigrateAsync(); - return true; - } + var dbContext = await uow.ServiceProvider + .GetRequiredService>() + .GetDbContextAsync(); - return false; - } - - if (tenantId == null) + if ((await dbContext.Database.GetPendingMigrationsAsync()).Any()) { - //Migrating the host database - Log.Information($"There is no tenant. Migrating {DatabaseName}..."); - 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) - Log.Information($"Migrating tenant database:{DatabaseName} with tenantId:{tenantId}..."); - result = await MigrateDatabaseSchemaWithDbContextAsync(); - } + await dbContext.Database.MigrateAsync(); + return true; } - await uow.CompleteAsync(); + return false; } + + //Migrating the host database + Log.Information($"There is no tenant. Migrating {DatabaseName}..."); + result = await MigrateDatabaseSchemaWithDbContextAsync(); + + await uow.CompleteAsync(); } + return result; } @@ -127,50 +111,6 @@ public abstract class DatabaseEfCoreMigrationEventHandler : Database } } - protected virtual async Task HandleErrorTenantCreatedAsync( - TenantCreatedEto eventData, - Exception exception) - { - var tryCount = IncrementEventTryCount(eventData); - if (tryCount <= MaxEventTryCount) - { - Log.Warning( - $"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); diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs index 18235262..79784821 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs @@ -37,7 +37,7 @@ public abstract class DatabaseMongoDbMigrationEventHandler : Databas IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, IDistributedEventBus distributedEventBus, - string databaseName, + string databaseName, IServiceProvider serviceProvider, IAbpDistributedLock distributedLockProvider ) @@ -57,62 +57,46 @@ public abstract class DatabaseMongoDbMigrationEventHandler : Databas /// Apply pending EF Core schema migrations to the database. /// Returns true if any migration has applied. /// - protected virtual async Task MigrateDatabaseSchemaAsync(Guid? tenantId) + protected virtual async Task MigrateDatabaseSchemaAsync() { var result = false; - using (CurrentTenant.Change(tenantId)) + using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false)) { - using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false)) + async Task MigrateDatabaseSchemaWithDbContextAsync() { - async Task MigrateDatabaseSchemaWithDbContextAsync() - { - var dbContexts = ServiceProvider.GetServices(); - var connectionStringResolver = ServiceProvider.GetRequiredService(); + var dbContexts = ServiceProvider.GetServices(); + var connectionStringResolver = ServiceProvider.GetRequiredService(); - foreach (var dbContext in dbContexts) + foreach (var dbContext in dbContexts) + { + var connectionString = + await connectionStringResolver.ResolveAsync( + ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType())); + if (connectionString.IsNullOrWhiteSpace()) { - 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)); + continue; } - return true; - } + var mongoUrl = new MongoUrl(connectionString); + var databaseName = mongoUrl.DatabaseName; + var client = new MongoClient(mongoUrl); - 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()) + if (databaseName.IsNullOrWhiteSpace()) { - //Migrating the tenant database (only if tenant has a separate database) - result = await MigrateDatabaseSchemaWithDbContextAsync(); + databaseName = ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType()); } + + (dbContext as AbpMongoDbContext)?.InitializeCollections(client.GetDatabase(databaseName)); } - await uow.CompleteAsync(); + return true; } + + //Migrating the host database + result = await MigrateDatabaseSchemaWithDbContextAsync(); + + await uow.CompleteAsync(); } return result; @@ -140,50 +124,6 @@ public abstract class DatabaseMongoDbMigrationEventHandler : Databas } } - 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);