Changed ConfigConnectionString to accept more valid connectionstrings (#10766)

* Added test cases for valid connectionstring configuration and updated code to pass tests

* Removed unneeded reference added by reverted changes

Co-authored-by: Dave de Moel <d.demoel@wearetriple.com>
Co-authored-by: Elitsa Marinovska <elm@umbraco.dk>
This commit is contained in:
deMD
2021-08-03 09:04:49 +02:00
committed by GitHub
parent 7cbe205472
commit c1dcdfa634
2 changed files with 41 additions and 30 deletions

View File

@@ -17,48 +17,57 @@ namespace Umbraco.Cms.Core.Configuration
public string ProviderName { get; }
public string Name { get; }
private string ParseProvider(string connectionString)
private static bool IsSqlCe(DbConnectionStringBuilder builder) => (builder.TryGetValue("Data Source", out var ds)
|| builder.TryGetValue("DataSource", out ds)) &&
ds is string dataSource &&
dataSource.EndsWith(".sdf");
private static bool IsSqlServer(DbConnectionStringBuilder builder) =>
!string.IsNullOrEmpty(GetServer(builder)) &&
((builder.TryGetValue("Database", out var db) && db is string database &&
!string.IsNullOrEmpty(database)) ||
(builder.TryGetValue("AttachDbFileName", out var a) && a is string attachDbFileName &&
!string.IsNullOrEmpty(attachDbFileName)) ||
(builder.TryGetValue("Initial Catalog", out var i) && i is string initialCatalog &&
!string.IsNullOrEmpty(initialCatalog)));
private static string GetServer(DbConnectionStringBuilder builder)
{
if(builder.TryGetValue("Server", out var s) && s is string server)
{
return server;
}
if ((builder.TryGetValue("Data Source", out var ds)
|| builder.TryGetValue("DataSource", out ds)) && ds is string dataSource)
{
return dataSource;
}
return "";
}
private static string ParseProvider(string connectionString)
{
if (string.IsNullOrEmpty(connectionString))
{
return null;
}
var builder = new DbConnectionStringBuilder
var builder = new DbConnectionStringBuilder {ConnectionString = connectionString};
if (IsSqlCe(builder))
{
ConnectionString = connectionString
};
if (
(builder.TryGetValue("Data Source", out var ds)
|| builder.TryGetValue("DataSource", out ds)) && ds is string dataSource)
{
if (dataSource.EndsWith(".sdf"))
{
return Umbraco.Cms.Core.Constants.DbProviderNames.SqlCe;
}
return Constants.DbProviderNames.SqlCe;
}
if (builder.TryGetValue("Server", out var s) && s is string server && !string.IsNullOrEmpty(server))
if (IsSqlServer(builder))
{
if (builder.TryGetValue("Database", out var db) && db is string database && !string.IsNullOrEmpty(database))
{
return Umbraco.Cms.Core.Constants.DbProviderNames.SqlServer;
}
if (builder.TryGetValue("AttachDbFileName", out var a) && a is string attachDbFileName && !string.IsNullOrEmpty(attachDbFileName))
{
return Umbraco.Cms.Core.Constants.DbProviderNames.SqlServer;
}
if (builder.TryGetValue("Initial Catalog", out var i) && i is string initialCatalog && !string.IsNullOrEmpty(initialCatalog))
{
return Umbraco.Cms.Core.Constants.DbProviderNames.SqlServer;
}
return Constants.DbProviderNames.SqlServer;
}
throw new ArgumentException("Cannot determine provider name from connection string", nameof(connectionString));
throw new ArgumentException("Cannot determine provider name from connection string",
nameof(connectionString));
}
}
}

View File

@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.
// Copyright (c) Umbraco.
// See LICENSE for more details.
using NUnit.Framework;
@@ -15,6 +15,8 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Configuration.Models
[TestCase(null, ExpectedResult = null)]
[TestCase(@"Data Source=|DataDirectory|\Umbraco.sdf;Flush Interval=1;", ExpectedResult = Constants.DbProviderNames.SqlCe)]
[TestCase(@"Server=(LocalDb)\Umbraco;Database=NetCore;Integrated Security=true", ExpectedResult = Constants.DbProviderNames.SqlServer)]
[TestCase(@"Data Source=(LocalDb)\Umbraco;Initial Catalog=NetCore;Integrated Security=true;", ExpectedResult = Constants.DbProviderNames.SqlServer)]
[TestCase(@"Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=MyDataFile.mdf;", ExpectedResult = Constants.DbProviderNames.SqlServer)]
public string ParseProviderName(string connectionString)
{
var connectionStrings = new ConnectionStrings