Fix custom dbcontexts extention methods (#14937)
* test: Create failing test * feat: New extension methods for adding Umbraco DBContexts * test: Cleaned up integration tests
This commit is contained in:
committed by
GitHub
parent
b1b48ec5cd
commit
c19e747bbe
@@ -10,7 +10,7 @@ public class SqlServerMigrationProvider : IMigrationProvider
|
||||
|
||||
public SqlServerMigrationProvider(IDbContextFactory<UmbracoDbContext> dbContextFactory) => _dbContextFactory = dbContextFactory;
|
||||
|
||||
public string ProviderName => "Microsoft.Data.SqlClient";
|
||||
public string ProviderName => Constants.ProviderNames.SQLServer;
|
||||
|
||||
public async Task MigrateAsync(EFCoreMigration migration)
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Umbraco.Cms.Persistence.EFCore.SqlServer;
|
||||
|
||||
public class SqlServerMigrationProviderSetup : IMigrationProviderSetup
|
||||
{
|
||||
public string ProviderName => "Microsoft.Data.SqlClient";
|
||||
public string ProviderName => Constants.ProviderNames.SQLServer;
|
||||
|
||||
public void Setup(DbContextOptionsBuilder builder, string? connectionString)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Umbraco.Cms.Persistence.EFCore.Migrations;
|
||||
using Umbraco.Extensions;
|
||||
using Umbraco.Cms.Persistence.EFCore;
|
||||
|
||||
namespace Umbraco.Cms.Persistence.EFCore.Sqlite;
|
||||
|
||||
@@ -11,7 +12,7 @@ public class SqliteMigrationProvider : IMigrationProvider
|
||||
public SqliteMigrationProvider(IDbContextFactory<UmbracoDbContext> dbContextFactory)
|
||||
=> _dbContextFactory = dbContextFactory;
|
||||
|
||||
public string ProviderName => "Microsoft.Data.Sqlite";
|
||||
public string ProviderName => Constants.ProviderNames.SQLLite;
|
||||
|
||||
public async Task MigrateAsync(EFCoreMigration migration)
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Umbraco.Cms.Persistence.EFCore.Sqlite;
|
||||
|
||||
public class SqliteMigrationProviderSetup : IMigrationProviderSetup
|
||||
{
|
||||
public string ProviderName => "Microsoft.Data.Sqlite";
|
||||
public string ProviderName => Constants.ProviderNames.SQLLite;
|
||||
|
||||
public void Setup(DbContextOptionsBuilder builder, string? connectionString)
|
||||
{
|
||||
|
||||
@@ -20,7 +20,7 @@ public class UmbracoEFCoreComposer : IComposer
|
||||
builder.AddNotificationAsyncHandler<DatabaseSchemaAndDataCreatedNotification, EFCoreCreateTablesNotificationHandler>();
|
||||
builder.AddNotificationAsyncHandler<UnattendedInstallNotification, EFCoreCreateTablesNotificationHandler>();
|
||||
|
||||
builder.Services.AddUmbracoEFCoreContext<UmbracoDbContext>((options, connectionString, providerName) =>
|
||||
builder.Services.AddUmbracoDbContext<UmbracoDbContext>((options) =>
|
||||
{
|
||||
// Register the entity sets needed by OpenIddict.
|
||||
options.UseOpenIddict();
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Umbraco.Cms.Persistence.EFCore;
|
||||
|
||||
public static partial class Constants
|
||||
{
|
||||
public static class ProviderNames
|
||||
{
|
||||
public const string SQLLite = "Microsoft.Data.Sqlite";
|
||||
|
||||
public const string SQLServer = "Microsoft.Data.SqlClient";
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Serilog;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.DistributedLocking;
|
||||
@@ -16,6 +18,7 @@ public static class UmbracoEFCoreServiceCollectionExtensions
|
||||
{
|
||||
public delegate void DefaultEFCoreOptionsAction(DbContextOptionsBuilder options, string? providerName, string? connectionString);
|
||||
|
||||
[Obsolete("Use AddUmbracoDbContext<T>(this IServiceCollection services, Action<DbContextOptionsBuilder>? optionsAction = null) instead.")]
|
||||
public static IServiceCollection AddUmbracoEFCoreContext<T>(this IServiceCollection services, DefaultEFCoreOptionsAction? defaultEFCoreOptionsAction = null)
|
||||
where T : DbContext
|
||||
{
|
||||
@@ -24,7 +27,7 @@ public static class UmbracoEFCoreServiceCollectionExtensions
|
||||
sp =>
|
||||
{
|
||||
SetupDbContext(defaultEFCoreOptionsAction, sp, optionsBuilder);
|
||||
return new UmbracoPooledDbContextFactory<T>(sp.GetRequiredService<IRuntimeState>(),optionsBuilder.Options);
|
||||
return new UmbracoPooledDbContextFactory<T>(sp.GetRequiredService<IRuntimeState>(), optionsBuilder.Options);
|
||||
});
|
||||
services.AddPooledDbContextFactory<T>((provider, builder) => SetupDbContext(defaultEFCoreOptionsAction, provider, builder));
|
||||
services.AddTransient(services => services.GetRequiredService<IDbContextFactory<T>>().CreateDbContext());
|
||||
@@ -38,6 +41,7 @@ public static class UmbracoEFCoreServiceCollectionExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
[Obsolete("Use AddUmbracoDbContext<T>(this IServiceCollection services, Action<DbContextOptionsBuilder>? optionsAction = null) instead.")]
|
||||
public static IServiceCollection AddUmbracoEFCoreContext<T>(this IServiceCollection services, string connectionString, string providerName, DefaultEFCoreOptionsAction? defaultEFCoreOptionsAction = null)
|
||||
where T : DbContext
|
||||
{
|
||||
@@ -52,8 +56,8 @@ public static class UmbracoEFCoreServiceCollectionExtensions
|
||||
services.TryAddSingleton<IDbContextFactory<T>>(
|
||||
sp =>
|
||||
{
|
||||
SetupDbContext(defaultEFCoreOptionsAction, sp, optionsBuilder);
|
||||
return new UmbracoPooledDbContextFactory<T>(sp.GetRequiredService<IRuntimeState>(),optionsBuilder.Options);
|
||||
defaultEFCoreOptionsAction?.Invoke(optionsBuilder, providerName, connectionString);
|
||||
return new UmbracoPooledDbContextFactory<T>(sp.GetRequiredService<IRuntimeState>(), optionsBuilder.Options);
|
||||
});
|
||||
services.AddPooledDbContextFactory<T>(options => defaultEFCoreOptionsAction?.Invoke(options, providerName, connectionString));
|
||||
services.AddTransient(services => services.GetRequiredService<IDbContextFactory<T>>().CreateDbContext());
|
||||
@@ -67,12 +71,117 @@ public static class UmbracoEFCoreServiceCollectionExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a EFCore DbContext with all the services needed to integrate with Umbraco scopes.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="optionsAction"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddUmbracoDbContext<T>(this IServiceCollection services, Action<DbContextOptionsBuilder>? optionsAction = null)
|
||||
where T : DbContext
|
||||
{
|
||||
return AddUmbracoDbContext<T>(services, (IServiceProvider _, DbContextOptionsBuilder options) =>
|
||||
{
|
||||
optionsAction?.Invoke(options);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a EFCore DbContext with all the services needed to integrate with Umbraco scopes.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="optionsAction"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddUmbracoDbContext<T>(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction = null)
|
||||
where T : DbContext
|
||||
{
|
||||
optionsAction ??= (sp, options) => { };
|
||||
|
||||
var optionsBuilder = new DbContextOptionsBuilder<T>();
|
||||
|
||||
services.TryAddSingleton<IDbContextFactory<T>>(sp =>
|
||||
{
|
||||
optionsAction.Invoke(sp, optionsBuilder);
|
||||
return new UmbracoPooledDbContextFactory<T>(sp.GetRequiredService<IRuntimeState>(), optionsBuilder.Options);
|
||||
});
|
||||
services.AddPooledDbContextFactory<T>(optionsAction);
|
||||
services.AddTransient(services => services.GetRequiredService<IDbContextFactory<T>>().CreateDbContext());
|
||||
|
||||
services.AddUnique<IAmbientEFCoreScopeStack<T>, AmbientEFCoreScopeStack<T>>();
|
||||
services.AddUnique<IEFCoreScopeAccessor<T>, EFCoreScopeAccessor<T>>();
|
||||
services.AddUnique<IEFCoreScopeProvider<T>, EFCoreScopeProvider<T>>();
|
||||
services.AddSingleton<IDistributedLockingMechanism, SqliteEFCoreDistributedLockingMechanism<T>>();
|
||||
services.AddSingleton<IDistributedLockingMechanism, SqlServerEFCoreDistributedLockingMechanism<T>>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the database provider. I.E UseSqlite or UseSqlServer based on the provider name.
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="providerName"></param>
|
||||
/// <param name="connectionString"></param>
|
||||
/// <exception cref="InvalidDataException"></exception>
|
||||
/// <remarks>
|
||||
/// Only supports the databases normally supported in Umbraco.
|
||||
/// </remarks>
|
||||
public static void UseDatabaseProvider(this DbContextOptionsBuilder builder, string providerName, string connectionString)
|
||||
{
|
||||
switch (providerName)
|
||||
{
|
||||
case Cms.Persistence.EFCore.Constants.ProviderNames.SQLServer:
|
||||
builder.UseSqlServer(connectionString);
|
||||
break;
|
||||
case Cms.Persistence.EFCore.Constants.ProviderNames.SQLLite:
|
||||
builder.UseSqlite(connectionString);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidDataException($"The provider {providerName} is not supported. Manually add the add the UseXXX statement to the options. I.E UseNpgsql()");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the database provider to use based on the Umbraco connection string.
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="serviceProvider"></param>
|
||||
public static void UseUmbracoDatabaseProvider(this DbContextOptionsBuilder builder, IServiceProvider serviceProvider)
|
||||
{
|
||||
ConnectionStrings connectionStrings = serviceProvider.GetRequiredService<IOptionsMonitor<ConnectionStrings>>().CurrentValue;
|
||||
|
||||
// Replace data directory
|
||||
string? dataDirectory = AppDomain.CurrentDomain.GetData(Constants.System.DataDirectoryName)?.ToString();
|
||||
if (string.IsNullOrEmpty(dataDirectory) is false)
|
||||
{
|
||||
connectionStrings.ConnectionString = connectionStrings.ConnectionString?.Replace(Constants.System.DataDirectoryPlaceholder, dataDirectory);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(connectionStrings.ProviderName))
|
||||
{
|
||||
Log.Warning("No database provider was set. ProviderName is null");
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(connectionStrings.ConnectionString))
|
||||
{
|
||||
Log.Warning("No database provider was set. Connection string is null");
|
||||
return;
|
||||
}
|
||||
|
||||
builder.UseDatabaseProvider(connectionStrings.ProviderName, connectionStrings.ConnectionString);
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
private static void SetupDbContext(DefaultEFCoreOptionsAction? defaultEFCoreOptionsAction, IServiceProvider provider, DbContextOptionsBuilder builder)
|
||||
{
|
||||
ConnectionStrings connectionStrings = GetConnectionStringAndProviderName(provider);
|
||||
defaultEFCoreOptionsAction?.Invoke(builder, connectionStrings.ConnectionString, connectionStrings.ProviderName);
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
private static ConnectionStrings GetConnectionStringAndProviderName(IServiceProvider serviceProvider)
|
||||
{
|
||||
ConnectionStrings connectionStrings = serviceProvider.GetRequiredService<IOptionsMonitor<ConnectionStrings>>().CurrentValue;
|
||||
|
||||
@@ -3,7 +3,6 @@ using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.DependencyInjection;
|
||||
using Umbraco.Cms.Persistence.EFCore.Migrations;
|
||||
@@ -77,7 +76,7 @@ public class UmbracoDbContext : DbContext
|
||||
|
||||
foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())
|
||||
{
|
||||
entity.SetTableName(Constants.DatabaseSchema.TableNamePrefix + entity.GetTableName());
|
||||
entity.SetTableName(Core.Constants.DatabaseSchema.TableNamePrefix + entity.GetTableName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Tests.Common.Testing;
|
||||
using Umbraco.Cms.Tests.Integration.Testing;
|
||||
|
||||
namespace Umbraco.Cms.Tests.Integration.Umbraco.Persistence.EFCore.DbContext;
|
||||
|
||||
[TestFixture]
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console)]
|
||||
public class CustomDbContextUmbracoProviderTests : UmbracoIntegrationTest
|
||||
{
|
||||
[Test]
|
||||
public void Can_Register_Custom_DbContext_And_Resolve()
|
||||
{
|
||||
var dbContext = Services.GetRequiredService<CustomDbContext>();
|
||||
|
||||
Assert.IsNotNull(dbContext);
|
||||
Assert.IsNotEmpty(dbContext.Database.GetConnectionString());
|
||||
}
|
||||
|
||||
protected override void CustomTestSetup(IUmbracoBuilder builder)
|
||||
{
|
||||
builder.Services.AddUmbracoDbContext<CustomDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
options.UseUmbracoDatabaseProvider(serviceProvider);
|
||||
});
|
||||
}
|
||||
|
||||
internal class CustomDbContext : Microsoft.EntityFrameworkCore.DbContext
|
||||
{
|
||||
public CustomDbContext(DbContextOptions<CustomDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[TestFixture]
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console)]
|
||||
public class CustomDbContextCustomSqliteProviderTests : UmbracoIntegrationTest
|
||||
{
|
||||
[Test]
|
||||
public void Can_Register_Custom_DbContext_And_Resolve()
|
||||
{
|
||||
var dbContext = Services.GetRequiredService<CustomDbContext>();
|
||||
|
||||
Assert.IsNotNull(dbContext);
|
||||
Assert.IsNotEmpty(dbContext.Database.GetConnectionString());
|
||||
}
|
||||
|
||||
protected override void CustomTestSetup(IUmbracoBuilder builder)
|
||||
{
|
||||
builder.Services.AddUmbracoDbContext<CustomDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
options.UseSqlite("Data Source=:memory:;Version=3;New=True;");
|
||||
});
|
||||
}
|
||||
|
||||
internal class CustomDbContext : Microsoft.EntityFrameworkCore.DbContext
|
||||
{
|
||||
public CustomDbContext(DbContextOptions<CustomDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
[TestFixture]
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console)]
|
||||
public class CustomDbContextLegacyExtensionProviderTests : UmbracoIntegrationTest
|
||||
{
|
||||
[Test]
|
||||
public void Can_Register_Custom_DbContext_And_Resolve()
|
||||
{
|
||||
var dbContext = Services.GetRequiredService<CustomDbContext>();
|
||||
|
||||
Assert.IsNotNull(dbContext);
|
||||
Assert.IsNotEmpty(dbContext.Database.GetConnectionString());
|
||||
}
|
||||
|
||||
protected override void CustomTestSetup(IUmbracoBuilder builder)
|
||||
{
|
||||
builder.Services.AddUmbracoEFCoreContext<CustomDbContext>("Data Source=:memory:;Version=3;New=True;", "Microsoft.Data.Sqlite", (options, connectionString, providerName) =>
|
||||
{
|
||||
options.UseSqlite(connectionString);
|
||||
});
|
||||
}
|
||||
|
||||
internal class CustomDbContext : Microsoft.EntityFrameworkCore.DbContext
|
||||
{
|
||||
public CustomDbContext(DbContextOptions<CustomDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user