Browse Source

Removed all tenant dependencies

pull/88/head
malik masis 5 years ago
parent
commit
a250cd55f0
  1. 44
      services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs
  2. 2
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs
  3. 28
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs
  4. 2
      services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs
  5. 2
      services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs
  6. 84
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs
  7. 90
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs

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

@ -57,8 +57,8 @@ namespace EShopOnAbp.AdministrationService.DbMigrations
if (handle != null) if (handle != null)
{ {
await MigrateDatabaseSchemaAsync(eventData.TenantId); await MigrateDatabaseSchemaAsync();
await SeedDataAsync(eventData.TenantId); await SeedDataAsync();
} }
} }
} }
@ -68,34 +68,30 @@ namespace EShopOnAbp.AdministrationService.DbMigrations
} }
} }
private async Task SeedDataAsync(Guid? tenantId) private async Task SeedDataAsync()
{ {
using (CurrentTenant.Change(tenantId))
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
{ {
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true)) var multiTenancySide = MultiTenancySides.Host;
{
var multiTenancySide = tenantId == null
? MultiTenancySides.Host
: MultiTenancySides.Tenant;
var permissionNames = _permissionDefinitionManager var permissionNames = _permissionDefinitionManager
.GetPermissions() .GetPermissions()
.Where(p => p.MultiTenancySide.HasFlag(multiTenancySide)) .Where(p => p.MultiTenancySide.HasFlag(multiTenancySide))
.Where(p => !p.Providers.Any() || .Where(p => !p.Providers.Any() ||
p.Providers.Contains(RolePermissionValueProvider.ProviderName)) p.Providers.Contains(RolePermissionValueProvider.ProviderName))
.Select(p => p.Name) .Select(p => p.Name)
.ToArray(); .ToArray();
await _permissionDataSeeder.SeedAsync( await _permissionDataSeeder.SeedAsync(
RolePermissionValueProvider.ProviderName, RolePermissionValueProvider.ProviderName,
"admin", "admin",
permissionNames, permissionNames
tenantId );
);
await uow.CompleteAsync(); await uow.CompleteAsync();
}
} }
} }
} }
} }

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

@ -54,7 +54,7 @@ namespace EShopOnAbp.CatalogService.DbMigrations
if (handle != null) if (handle != null)
{ {
Log.Information("CatalogService is migrating database..."); Log.Information("CatalogService is migrating database...");
await MigrateDatabaseSchemaAsync(null); await MigrateDatabaseSchemaAsync();
} }
} }
} }

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

@ -59,10 +59,9 @@ namespace EShopOnAbp.IdentityService.DbMigrations
if (handle != null) if (handle != null)
{ {
Log.Information("IdentityService is migrating database..."); Log.Information("IdentityService is migrating database...");
await MigrateDatabaseSchemaAsync(eventData.TenantId); await MigrateDatabaseSchemaAsync();
Log.Information("IdentityService is seeding data..."); Log.Information("IdentityService is seeding data...");
await SeedDataAsync( await SeedDataAsync(
tenantId: eventData.TenantId,
adminEmail: IdentityServiceDbProperties.DefaultAdminEmailAddress, adminEmail: IdentityServiceDbProperties.DefaultAdminEmailAddress,
adminPassword: IdentityServiceDbProperties.DefaultAdminPassword adminPassword: IdentityServiceDbProperties.DefaultAdminPassword
); );
@ -77,22 +76,17 @@ namespace EShopOnAbp.IdentityService.DbMigrations
} }
} }
private async Task SeedDataAsync(Guid? tenantId, string adminEmail, string adminPassword) private async Task SeedDataAsync(string adminEmail, string adminPassword)
{ {
using (CurrentTenant.Change(tenantId)) Log.Information($"Seeding IdentityServer data...");
{ await _identityServerDataSeeder.SeedAsync();
if (tenantId == null)
{ Log.Information($"Seeding user data...");
Log.Information($"Seeding IdentityServer data..."); await _identityDataSeeder.SeedAsync(
await _identityServerDataSeeder.SeedAsync(); adminEmail,
} adminPassword
Log.Information($"Seeding user data..."); );
await _identityDataSeeder.SeedAsync(
adminEmail,
adminPassword,
tenantId
);
}
} }
} }
} }

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

@ -56,7 +56,7 @@ namespace EShopOnAbp.OrderingService.DbMigrations
if (handle != null) if (handle != null)
{ {
Log.Information("OrderingService is migrating database..."); Log.Information("OrderingService is migrating database...");
await MigrateDatabaseSchemaAsync(null); await MigrateDatabaseSchemaAsync();
Log.Information("OrderingService is seeding data..."); Log.Information("OrderingService is seeding data...");
await _dataSeeder.SeedAsync(); await _dataSeeder.SeedAsync();
} }

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

@ -52,7 +52,7 @@ namespace EShopOnAbp.PaymentService.DbMigrations
if (handle != null) if (handle != null)
{ {
Log.Information("PaymentService is migrating database..."); Log.Information("PaymentService is migrating database...");
await MigrateDatabaseSchemaAsync(null); await MigrateDatabaseSchemaAsync();
} }
} }
} }

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

@ -1,13 +1,12 @@
using System; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Medallion.Threading;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Serilog; using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.DistributedLocking; using Volo.Abp.DistributedLocking;
@ -56,51 +55,36 @@ public abstract class DatabaseEfCoreMigrationEventHandler<TDbContext> : Database
/// Apply pending EF Core schema migrations to the database. /// Apply pending EF Core schema migrations to the database.
/// Returns true if any migration has applied. /// Returns true if any migration has applied.
/// </summary> /// </summary>
protected virtual async Task<bool> MigrateDatabaseSchemaAsync(Guid? tenantId) protected virtual async Task<bool> MigrateDatabaseSchemaAsync()
{ {
var result = false; 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<bool> MigrateDatabaseSchemaWithDbContextAsync()
{ {
async Task<bool> MigrateDatabaseSchemaWithDbContextAsync() var dbContext = await uow.ServiceProvider
{ .GetRequiredService<IDbContextProvider<TDbContext>>()
var dbContext = await uow.ServiceProvider .GetDbContextAsync();
.GetRequiredService<IDbContextProvider<TDbContext>>()
.GetDbContextAsync();
if ((await dbContext.Database.GetPendingMigrationsAsync()).Any()) if ((await dbContext.Database.GetPendingMigrationsAsync()).Any())
{
await dbContext.Database.MigrateAsync();
return true;
}
return false;
}
if (tenantId == null)
{ {
//Migrating the host database await dbContext.Database.MigrateAsync();
Log.Information($"There is no tenant. Migrating {DatabaseName}..."); return true;
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 uow.CompleteAsync(); return false;
} }
//Migrating the host database
Log.Information($"There is no tenant. Migrating {DatabaseName}...");
result = await MigrateDatabaseSchemaWithDbContextAsync();
await uow.CompleteAsync();
} }
return result; return result;
} }
@ -127,28 +111,6 @@ public abstract class DatabaseEfCoreMigrationEventHandler<TDbContext> : Database
} }
} }
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) private static int GetEventTryCount(EtoBase eventData)
{ {
var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName); var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName);

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

@ -37,7 +37,7 @@ public abstract class DatabaseMongoDbMigrationEventHandler<TDbContext> : Databas
IUnitOfWorkManager unitOfWorkManager, IUnitOfWorkManager unitOfWorkManager,
ITenantStore tenantStore, ITenantStore tenantStore,
IDistributedEventBus distributedEventBus, IDistributedEventBus distributedEventBus,
string databaseName, string databaseName,
IServiceProvider serviceProvider, IServiceProvider serviceProvider,
IAbpDistributedLock distributedLockProvider IAbpDistributedLock distributedLockProvider
) )
@ -57,62 +57,46 @@ public abstract class DatabaseMongoDbMigrationEventHandler<TDbContext> : Databas
/// Apply pending EF Core schema migrations to the database. /// Apply pending EF Core schema migrations to the database.
/// Returns true if any migration has applied. /// Returns true if any migration has applied.
/// </summary> /// </summary>
protected virtual async Task<bool> MigrateDatabaseSchemaAsync(Guid? tenantId) protected virtual async Task<bool> MigrateDatabaseSchemaAsync()
{ {
var result = false; 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<bool> MigrateDatabaseSchemaWithDbContextAsync()
{ {
async Task<bool> MigrateDatabaseSchemaWithDbContextAsync() var dbContexts = ServiceProvider.GetServices<IAbpMongoDbContext>();
{ var connectionStringResolver = ServiceProvider.GetRequiredService<IConnectionStringResolver>();
var dbContexts = ServiceProvider.GetServices<IAbpMongoDbContext>();
var connectionStringResolver = ServiceProvider.GetRequiredService<IConnectionStringResolver>();
foreach (var dbContext in dbContexts) foreach (var dbContext in dbContexts)
{
var connectionString =
await connectionStringResolver.ResolveAsync(
ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType()));
if (connectionString.IsNullOrWhiteSpace())
{ {
var connectionString = continue;
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; var mongoUrl = new MongoUrl(connectionString);
} var databaseName = mongoUrl.DatabaseName;
var client = new MongoClient(mongoUrl);
if (tenantId == null) if (databaseName.IsNullOrWhiteSpace())
{
//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) databaseName = ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType());
result = await MigrateDatabaseSchemaWithDbContextAsync();
} }
(dbContext as AbpMongoDbContext)?.InitializeCollections(client.GetDatabase(databaseName));
} }
await uow.CompleteAsync(); return true;
} }
//Migrating the host database
result = await MigrateDatabaseSchemaWithDbContextAsync();
await uow.CompleteAsync();
} }
return result; return result;
@ -140,28 +124,6 @@ public abstract class DatabaseMongoDbMigrationEventHandler<TDbContext> : Databas
} }
} }
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) private static int GetEventTryCount(EtoBase eventData)
{ {
var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName); var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName);

Loading…
Cancel
Save