Simplifyed the ConnectionStrings config
This commit is contained in:
@@ -1,16 +1,61 @@
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
|
||||
namespace Umbraco.Core.Configuration
|
||||
{
|
||||
public class ConfigConnectionString
|
||||
{
|
||||
public ConfigConnectionString(string connectionString, string providerName, string name)
|
||||
public ConfigConnectionString(string name, string connectionString, string providerName = null)
|
||||
{
|
||||
Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
ConnectionString = connectionString;
|
||||
ProviderName = providerName;
|
||||
Name = name;
|
||||
|
||||
ProviderName = string.IsNullOrEmpty(providerName) ? ParseProvider(connectionString) : providerName;
|
||||
}
|
||||
|
||||
public string ConnectionString { get; }
|
||||
public string ProviderName { get; }
|
||||
public string Name { get; }
|
||||
|
||||
private string ParseProvider(string connectionString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var builder = new DbConnectionStringBuilder
|
||||
{
|
||||
ConnectionString = connectionString
|
||||
};
|
||||
|
||||
if (builder.TryGetValue("Data Source", out var ds) && ds is string dataSource)
|
||||
{
|
||||
if (dataSource.EndsWith(".sdf"))
|
||||
{
|
||||
return Constants.DbProviderNames.SqlCe;
|
||||
}
|
||||
}
|
||||
|
||||
if (builder.TryGetValue("Server", out var s) && s is string server && !string.IsNullOrEmpty(server))
|
||||
{
|
||||
if (builder.TryGetValue("Database", out var db) && db is string database && !string.IsNullOrEmpty(database))
|
||||
{
|
||||
return Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
|
||||
if (builder.TryGetValue("AttachDbFileName", out var a) && a is string attachDbFileName && !string.IsNullOrEmpty(attachDbFileName))
|
||||
{
|
||||
return Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
|
||||
if (builder.TryGetValue("Initial Catalog", out var i) && i is string initialCatalog && !string.IsNullOrEmpty(initialCatalog))
|
||||
{
|
||||
return Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
}
|
||||
|
||||
throw new ArgumentException("Cannot determine provider name from connection string", nameof(connectionString));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,93 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Umbraco.Core.Configuration.Models
|
||||
{
|
||||
public class ConnectionStrings
|
||||
{
|
||||
|
||||
|
||||
// Backing field for UmbracoConnectionString to load from configuration value with key umbracoDbDSN.
|
||||
// Attributes cannot be applied to map from keys that don't match, and have chosen to retain the key name
|
||||
// used in configuration for older Umbraco versions.
|
||||
// See: https://stackoverflow.com/a/54607296/489433
|
||||
private string umbracoDbDSN
|
||||
{
|
||||
get => string.Empty;
|
||||
set
|
||||
{
|
||||
UmbracoConnectionString = value;
|
||||
|
||||
ConnectionStringDictionary[Constants.System.UmbracoConnectionName] = value;
|
||||
}
|
||||
get => UmbracoConnectionString?.ConnectionString;
|
||||
set => UmbracoConnectionString = new ConfigConnectionString(Constants.System.UmbracoConnectionName, value);
|
||||
}
|
||||
|
||||
public string UmbracoConnectionString
|
||||
{
|
||||
get
|
||||
{
|
||||
ConnectionStringDictionary.TryGetValue(Constants.System.UmbracoConnectionName, out var value);
|
||||
return value;
|
||||
}
|
||||
set => ConnectionStringDictionary[Constants.System.UmbracoConnectionName] = value;
|
||||
}
|
||||
|
||||
private Dictionary<string, string> ConnectionStringDictionary { get; } = new Dictionary<string, string>();
|
||||
|
||||
public ConfigConnectionString this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!ConnectionStringDictionary.TryGetValue(key, out var connectionString))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var provider = ParseProvider(connectionString);
|
||||
return new ConfigConnectionString(connectionString, provider, key);
|
||||
}
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private string ParseProvider(string connectionString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var builder = new DbConnectionStringBuilder();
|
||||
|
||||
builder.ConnectionString = connectionString;
|
||||
|
||||
if (builder.TryGetValue("Data Source", out var ds) && ds is string dataSource)
|
||||
{
|
||||
if (dataSource.EndsWith(".sdf"))
|
||||
{
|
||||
return Constants.DbProviderNames.SqlCe;
|
||||
}
|
||||
}
|
||||
|
||||
if (builder.TryGetValue("Server", out var s) && s is string server && !string.IsNullOrEmpty(server))
|
||||
{
|
||||
if (builder.TryGetValue("Database", out var db) && db is string database && !string.IsNullOrEmpty(database))
|
||||
{
|
||||
return Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
|
||||
if (builder.TryGetValue("AttachDbFileName", out var a) && a is string attachDbFileName && !string.IsNullOrEmpty(attachDbFileName))
|
||||
{
|
||||
return Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
|
||||
if (builder.TryGetValue("Initial Catalog", out var i) && i is string initialCatalog && !string.IsNullOrEmpty(initialCatalog))
|
||||
{
|
||||
return Constants.DbProviderNames.SqlServer;
|
||||
}
|
||||
}
|
||||
|
||||
throw new ArgumentException("Cannot determine provider name from connection string", nameof(connectionString));
|
||||
}
|
||||
public ConfigConnectionString UmbracoConnectionString { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ namespace Umbraco.Web.Install
|
||||
{
|
||||
get
|
||||
{
|
||||
var databaseSettings = _connectionStrings[Constants.System.UmbracoConnectionName];
|
||||
var databaseSettings = _connectionStrings.UmbracoConnectionString;
|
||||
if (databaseSettings.IsConnectionStringConfigured() == false)
|
||||
{
|
||||
//no version or conn string configured, must be a brand new install
|
||||
|
||||
@@ -108,7 +108,7 @@ namespace Umbraco.Web.Install.InstallSteps
|
||||
private bool ShouldDisplayView()
|
||||
{
|
||||
//If the connection string is already present in web.config we don't need to show the settings page and we jump to installing/upgrading.
|
||||
var databaseSettings = _connectionStrings[Constants.System.UmbracoConnectionName];
|
||||
var databaseSettings = _connectionStrings.UmbracoConnectionString;
|
||||
|
||||
if (databaseSettings.IsConnectionStringConfigured())
|
||||
{
|
||||
|
||||
@@ -75,7 +75,7 @@ namespace Umbraco.Web.Install.InstallSteps
|
||||
return false;
|
||||
}
|
||||
|
||||
var databaseSettings = _connectionStrings[Constants.System.UmbracoConnectionName];
|
||||
var databaseSettings = _connectionStrings.UmbracoConnectionString;
|
||||
|
||||
if (databaseSettings.IsConnectionStringConfigured())
|
||||
{
|
||||
|
||||
@@ -132,7 +132,7 @@ namespace Umbraco.Web.Install.InstallSteps
|
||||
public override bool RequiresExecution(UserModel model)
|
||||
{
|
||||
//now we have to check if this is really a new install, the db might be configured and might contain data
|
||||
var databaseSettings = _connectionStrings[Constants.System.UmbracoConnectionName];
|
||||
var databaseSettings = _connectionStrings.UmbracoConnectionString;
|
||||
if (databaseSettings.IsConnectionStringConfigured() && _databaseBuilder.IsDatabaseConfigured)
|
||||
return _databaseBuilder.HasSomeNonDefaultUser() == false;
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace Umbraco.Core.Persistence
|
||||
/// </summary>
|
||||
/// <remarks>Used by core runtime.</remarks>
|
||||
public UmbracoDatabaseFactory(ILogger logger, IOptions<GlobalSettings> globalSettings, IOptions<ConnectionStrings> connectionStrings, Lazy<IMapperCollection> mappers,IDbProviderFactoryCreator dbProviderFactoryCreator)
|
||||
: this(logger, globalSettings.Value, connectionStrings.Value, Constants.System.UmbracoConnectionName, mappers, dbProviderFactoryCreator)
|
||||
: this(logger, globalSettings.Value, connectionStrings.Value, mappers, dbProviderFactoryCreator)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -79,17 +79,15 @@ namespace Umbraco.Core.Persistence
|
||||
/// Initializes a new instance of the <see cref="UmbracoDatabaseFactory"/>.
|
||||
/// </summary>
|
||||
/// <remarks>Used by the other ctor and in tests.</remarks>
|
||||
public UmbracoDatabaseFactory(ILogger logger, GlobalSettings globalSettings, ConnectionStrings connectionStrings, string connectionStringName, Lazy<IMapperCollection> mappers, IDbProviderFactoryCreator dbProviderFactoryCreator)
|
||||
public UmbracoDatabaseFactory(ILogger logger, GlobalSettings globalSettings, ConnectionStrings connectionStrings, Lazy<IMapperCollection> mappers, IDbProviderFactoryCreator dbProviderFactoryCreator)
|
||||
{
|
||||
if (connectionStringName == null) throw new ArgumentNullException(nameof(connectionStringName));
|
||||
if (string.IsNullOrWhiteSpace(connectionStringName)) throw new ArgumentException("Value can't be empty or consist only of white-space characters.", nameof(connectionStringName));
|
||||
|
||||
_globalSettings = globalSettings;
|
||||
_mappers = mappers ?? throw new ArgumentNullException(nameof(mappers));
|
||||
_dbProviderFactoryCreator = dbProviderFactoryCreator ?? throw new ArgumentNullException(nameof(dbProviderFactoryCreator));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
|
||||
var settings = connectionStrings[connectionStringName];
|
||||
var settings = connectionStrings.UmbracoConnectionString;
|
||||
|
||||
if (settings == null)
|
||||
{
|
||||
|
||||
@@ -41,7 +41,6 @@ namespace Umbraco.Core.Runtime
|
||||
_dbFactory = new UmbracoDatabaseFactory(_logger,
|
||||
globalSettings,
|
||||
connectionStrings,
|
||||
Constants.System.UmbracoConnectionName,
|
||||
new Lazy<IMapperCollection>(() => new MapperCollection(Enumerable.Empty<BaseMapper>())),
|
||||
dbProviderFactoryCreator);
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Configuration;
|
||||
using ConnectionStrings = Umbraco.Core.Configuration.Models.ConnectionStrings;
|
||||
|
||||
namespace Umbraco.Tests.Common.Builders
|
||||
@@ -18,7 +20,7 @@ namespace Umbraco.Tests.Common.Builders
|
||||
|
||||
return new ConnectionStrings
|
||||
{
|
||||
UmbracoConnectionString = umbracoConnectionString,
|
||||
UmbracoConnectionString = new ConfigConnectionString(Constants.System.UmbracoConnectionName, umbracoConnectionString),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@
|
||||
<Compile Remove="TEMP\**" />
|
||||
<EmbeddedResource Remove="TEMP\**" />
|
||||
<None Remove="TEMP\**" />
|
||||
<Compile Remove="Views\**" />
|
||||
<EmbeddedResource Remove="Views\**" />
|
||||
<None Remove="Views\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Configuration.Models;
|
||||
|
||||
namespace Umbraco.Tests.UnitTests.Umbraco.Configuration.Models
|
||||
@@ -13,19 +14,17 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Configuration.Models
|
||||
[TestCase(@"Server=(LocalDb)\Umbraco;Database=NetCore;Integrated Security=true", ExpectedResult = Constants.DbProviderNames.SqlServer)]
|
||||
public string ParseProviderName(string connectionString)
|
||||
{
|
||||
var key = Constants.System.UmbracoConnectionName;
|
||||
|
||||
var connectionStrings = new ConnectionStrings
|
||||
{
|
||||
UmbracoConnectionString = connectionString
|
||||
UmbracoConnectionString = new ConfigConnectionString(Constants.System.UmbracoConnectionName, connectionString)
|
||||
};
|
||||
|
||||
var actual = connectionStrings[key];
|
||||
var actual = connectionStrings.UmbracoConnectionString;
|
||||
|
||||
Assert.AreEqual(connectionString, actual.ConnectionString);
|
||||
Assert.AreEqual(key, actual.Name);
|
||||
Assert.AreEqual(Constants.System.UmbracoConnectionName, actual.Name);
|
||||
|
||||
return connectionStrings[key].ProviderName;
|
||||
return connectionStrings.UmbracoConnectionString.ProviderName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +96,6 @@ namespace Umbraco.Tests.TestHelpers
|
||||
databaseFactory = new UmbracoDatabaseFactory(logger,
|
||||
globalSettings,
|
||||
connectionStrings,
|
||||
Constants.System.UmbracoConnectionName,
|
||||
new Lazy<IMapperCollection>(() => mappers),
|
||||
TestHelper.DbProviderFactoryCreator);
|
||||
}
|
||||
|
||||
@@ -471,7 +471,6 @@ namespace Umbraco.Tests.Testing
|
||||
Composition.RegisterUnique<IUmbracoDatabaseFactory>(f => new UmbracoDatabaseFactory(Logger,
|
||||
globalSettings,
|
||||
connectionStrings,
|
||||
Constants.System.UmbracoConnectionName,
|
||||
new Lazy<IMapperCollection>(f.GetInstance<IMapperCollection>),
|
||||
TestHelper.DbProviderFactoryCreator));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user