diff --git a/src/Umbraco.Core/Configuration/ConfigConnectionString.cs b/src/Umbraco.Core/Configuration/ConfigConnectionString.cs index e9ac944b85..3a540aa3e2 100644 --- a/src/Umbraco.Core/Configuration/ConfigConnectionString.cs +++ b/src/Umbraco.Core/Configuration/ConfigConnectionString.cs @@ -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)); + } } } diff --git a/src/Umbraco.Core/Configuration/Models/ConnectionStrings.cs b/src/Umbraco.Core/Configuration/Models/ConnectionStrings.cs index eee8faa492..d21cba4486 100644 --- a/src/Umbraco.Core/Configuration/Models/ConnectionStrings.cs +++ b/src/Umbraco.Core/Configuration/Models/ConnectionStrings.cs @@ -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 ConnectionStringDictionary { get; } = new Dictionary(); - - 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; } } } diff --git a/src/Umbraco.Infrastructure/Install/InstallHelper.cs b/src/Umbraco.Infrastructure/Install/InstallHelper.cs index af00034983..967e8cc087 100644 --- a/src/Umbraco.Infrastructure/Install/InstallHelper.cs +++ b/src/Umbraco.Infrastructure/Install/InstallHelper.cs @@ -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 diff --git a/src/Umbraco.Infrastructure/Install/InstallSteps/DatabaseConfigureStep.cs b/src/Umbraco.Infrastructure/Install/InstallSteps/DatabaseConfigureStep.cs index d995002daa..74b5b68bfd 100644 --- a/src/Umbraco.Infrastructure/Install/InstallSteps/DatabaseConfigureStep.cs +++ b/src/Umbraco.Infrastructure/Install/InstallSteps/DatabaseConfigureStep.cs @@ -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()) { diff --git a/src/Umbraco.Infrastructure/Install/InstallSteps/DatabaseUpgradeStep.cs b/src/Umbraco.Infrastructure/Install/InstallSteps/DatabaseUpgradeStep.cs index 22ad84df74..476feba119 100644 --- a/src/Umbraco.Infrastructure/Install/InstallSteps/DatabaseUpgradeStep.cs +++ b/src/Umbraco.Infrastructure/Install/InstallSteps/DatabaseUpgradeStep.cs @@ -75,7 +75,7 @@ namespace Umbraco.Web.Install.InstallSteps return false; } - var databaseSettings = _connectionStrings[Constants.System.UmbracoConnectionName]; + var databaseSettings = _connectionStrings.UmbracoConnectionString; if (databaseSettings.IsConnectionStringConfigured()) { diff --git a/src/Umbraco.Infrastructure/Install/InstallSteps/NewInstallStep.cs b/src/Umbraco.Infrastructure/Install/InstallSteps/NewInstallStep.cs index 11f8f916ed..f2e9556a48 100644 --- a/src/Umbraco.Infrastructure/Install/InstallSteps/NewInstallStep.cs +++ b/src/Umbraco.Infrastructure/Install/InstallSteps/NewInstallStep.cs @@ -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; diff --git a/src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs b/src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs index eea82026ab..c092cc8b0e 100644 --- a/src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs +++ b/src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs @@ -70,7 +70,7 @@ namespace Umbraco.Core.Persistence /// /// Used by core runtime. public UmbracoDatabaseFactory(ILogger logger, IOptions globalSettings, IOptions connectionStrings, Lazy 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 . /// /// Used by the other ctor and in tests. - public UmbracoDatabaseFactory(ILogger logger, GlobalSettings globalSettings, ConnectionStrings connectionStrings, string connectionStringName, Lazy mappers, IDbProviderFactoryCreator dbProviderFactoryCreator) + public UmbracoDatabaseFactory(ILogger logger, GlobalSettings globalSettings, ConnectionStrings connectionStrings, Lazy 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) { diff --git a/src/Umbraco.Infrastructure/Runtime/SqlMainDomLock.cs b/src/Umbraco.Infrastructure/Runtime/SqlMainDomLock.cs index 723be61283..4cbe8f088a 100644 --- a/src/Umbraco.Infrastructure/Runtime/SqlMainDomLock.cs +++ b/src/Umbraco.Infrastructure/Runtime/SqlMainDomLock.cs @@ -41,7 +41,6 @@ namespace Umbraco.Core.Runtime _dbFactory = new UmbracoDatabaseFactory(_logger, globalSettings, connectionStrings, - Constants.System.UmbracoConnectionName, new Lazy(() => new MapperCollection(Enumerable.Empty())), dbProviderFactoryCreator); diff --git a/src/Umbraco.Tests.Common/Builders/ConnectionStringsBuilder.cs b/src/Umbraco.Tests.Common/Builders/ConnectionStringsBuilder.cs index a252a7996c..4aba98fccd 100644 --- a/src/Umbraco.Tests.Common/Builders/ConnectionStringsBuilder.cs +++ b/src/Umbraco.Tests.Common/Builders/ConnectionStringsBuilder.cs @@ -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), }; } } diff --git a/src/Umbraco.Tests.Integration/Umbraco.Tests.Integration.csproj b/src/Umbraco.Tests.Integration/Umbraco.Tests.Integration.csproj index dc10208d1e..b5d2da1bba 100644 --- a/src/Umbraco.Tests.Integration/Umbraco.Tests.Integration.csproj +++ b/src/Umbraco.Tests.Integration/Umbraco.Tests.Integration.csproj @@ -11,6 +11,9 @@ + + + diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Configuration/Models/ConnectionStringsTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Configuration/Models/ConnectionStringsTests.cs index 8c5f096208..544765ee9c 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Configuration/Models/ConnectionStringsTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Configuration/Models/ConnectionStringsTests.cs @@ -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; } } } diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects.cs b/src/Umbraco.Tests/TestHelpers/TestObjects.cs index b3f576b06d..e101bb33a3 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects.cs @@ -96,7 +96,6 @@ namespace Umbraco.Tests.TestHelpers databaseFactory = new UmbracoDatabaseFactory(logger, globalSettings, connectionStrings, - Constants.System.UmbracoConnectionName, new Lazy(() => mappers), TestHelper.DbProviderFactoryCreator); } diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index 0ebe1ab2fb..0c39ee47f0 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -471,7 +471,6 @@ namespace Umbraco.Tests.Testing Composition.RegisterUnique(f => new UmbracoDatabaseFactory(Logger, globalSettings, connectionStrings, - Constants.System.UmbracoConnectionName, new Lazy(f.GetInstance), TestHelper.DbProviderFactoryCreator));