diff --git a/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs b/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs index 0de5ecebf4..321e61e2ac 100644 --- a/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs +++ b/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs @@ -54,26 +54,26 @@ namespace Umbraco.Core.Persistence /// /// The collection of available sql syntax providers. /// A logger. + /// Used by LightInject. public DefaultDatabaseFactory(IEnumerable sqlSyntaxProviders, ILogger logger) - { - if (sqlSyntaxProviders == null) throw new ArgumentNullException(nameof(sqlSyntaxProviders)); - if (logger == null) throw new ArgumentNullException(nameof(logger)); + : this(GlobalSettings.UmbracoConnectionName, sqlSyntaxProviders, logger) + { } - _sqlSyntaxProviders = sqlSyntaxProviders.ToArray(); - _logger = logger; - } - - // fixme - used once by the other ctor, and 5 times in various tests /// /// Initializes a new instance of the with a connection string name and a logger. /// /// The name of the connection string in web.config. /// The collection of available sql syntax providers. /// A logger + /// Used by the other ctor and in tests. public DefaultDatabaseFactory(string connectionStringName, IEnumerable sqlSyntaxProviders, ILogger logger) - : this(sqlSyntaxProviders, logger) { - Mandate.ParameterNotNullOrEmpty(connectionStringName, "connectionStringName"); + if (sqlSyntaxProviders == null) throw new ArgumentNullException(nameof(sqlSyntaxProviders)); + if (logger == null) throw new ArgumentNullException(nameof(logger)); + if (string.IsNullOrWhiteSpace(connectionStringName)) throw new ArgumentException("Value cannot be null nor empty.", nameof(connectionStringName)); + + _sqlSyntaxProviders = sqlSyntaxProviders.ToArray(); + _logger = logger; var settings = ConfigurationManager.ConnectionStrings[connectionStringName]; if (settings == null) @@ -82,7 +82,6 @@ namespace Umbraco.Core.Persistence Configure(settings.ConnectionString, settings.ProviderName); } - // fixme - used only once in tests /// /// Initializes a new instance of the with a connection string, a provider name and a logger. /// @@ -90,9 +89,15 @@ namespace Umbraco.Core.Persistence /// The name of the database provider. /// The collection of available sql syntax providers. /// A logger. + /// Used in tests. public DefaultDatabaseFactory(string connectionString, string providerName, IEnumerable sqlSyntaxProviders, ILogger logger) - : this(sqlSyntaxProviders, logger) { + if (sqlSyntaxProviders == null) throw new ArgumentNullException(nameof(sqlSyntaxProviders)); + if (logger == null) throw new ArgumentNullException(nameof(logger)); + + _sqlSyntaxProviders = sqlSyntaxProviders.ToArray(); + _logger = logger; + if (string.IsNullOrWhiteSpace(connectionString) || string.IsNullOrWhiteSpace(providerName)) return; // not configured diff --git a/src/Umbraco.Tests/Migrations/Upgrades/SqlCeDataUpgradeTest.cs b/src/Umbraco.Tests/Migrations/Upgrades/SqlCeDataUpgradeTest.cs index 12ee192a7d..01b69cbbdc 100644 --- a/src/Umbraco.Tests/Migrations/Upgrades/SqlCeDataUpgradeTest.cs +++ b/src/Umbraco.Tests/Migrations/Upgrades/SqlCeDataUpgradeTest.cs @@ -27,7 +27,7 @@ namespace Umbraco.Tests.Migrations.Upgrades var targetVersion = new SemVersion(6, 0, 0); var db = GetConfiguredDatabase(); - var fix = new PublishAfterUpgradeToVersionSixth(db.SqlSyntax); + var fix = new PublishAfterUpgradeToVersionSixth(); //Setup the MigrationRunner var migrationRunner = new MigrationRunner( diff --git a/src/Umbraco.Web/Strategies/Migrations/ClearMediaXmlCacheForDeletedItemsAfterUpgrade.cs b/src/Umbraco.Web/Strategies/Migrations/ClearMediaXmlCacheForDeletedItemsAfterUpgrade.cs index 6e0580a2c4..b3e1306429 100644 --- a/src/Umbraco.Web/Strategies/Migrations/ClearMediaXmlCacheForDeletedItemsAfterUpgrade.cs +++ b/src/Umbraco.Web/Strategies/Migrations/ClearMediaXmlCacheForDeletedItemsAfterUpgrade.cs @@ -18,13 +18,6 @@ namespace Umbraco.Web.Strategies.Migrations /// public class ClearMediaXmlCacheForDeletedItemsAfterUpgrade : MigrationStartupHander { - private readonly ISqlSyntaxProvider _sqlSyntax; - - public ClearMediaXmlCacheForDeletedItemsAfterUpgrade(ISqlSyntaxProvider sqlSyntax) - { - _sqlSyntax = sqlSyntax; - } - protected override void AfterMigration(MigrationRunner sender, MigrationEventArgs e) { if (e.ProductName != GlobalSettings.UmbracoMigrationName) return; @@ -36,10 +29,12 @@ namespace Umbraco.Web.Strategies.Migrations //This query is structured to work with MySql, SQLCE and SqlServer: // http://issues.umbraco.org/issue/U4-3876 + var syntax = e.MigrationContext.Database.SqlSyntax; + var sql = @"DELETE FROM cmsContentXml WHERE nodeId IN (SELECT nodeId FROM (SELECT DISTINCT cmsContentXml.nodeId FROM cmsContentXml INNER JOIN umbracoNode ON cmsContentXml.nodeId = umbracoNode.id - WHERE nodeObjectType = '" + Constants.ObjectTypes.Media + "' AND " + _sqlSyntax.GetQuotedColumnName("path") + " LIKE '%-21%') x)"; + WHERE nodeObjectType = '" + Constants.ObjectTypes.Media + "' AND " + syntax.GetQuotedColumnName("path") + " LIKE '%-21%') x)"; var count = e.MigrationContext.Database.Execute(sql); diff --git a/src/Umbraco.Web/Strategies/Migrations/EnsureListViewDataTypeIsCreated.cs b/src/Umbraco.Web/Strategies/Migrations/EnsureListViewDataTypeIsCreated.cs index 92325ecaa7..efffc8bfd0 100644 --- a/src/Umbraco.Web/Strategies/Migrations/EnsureListViewDataTypeIsCreated.cs +++ b/src/Umbraco.Web/Strategies/Migrations/EnsureListViewDataTypeIsCreated.cs @@ -19,13 +19,6 @@ namespace Umbraco.Web.Strategies.Migrations /// public class EnsureDefaultListViewDataTypesCreated : MigrationStartupHander { - private readonly ISqlSyntaxProvider _sqlSyntax; - - public EnsureDefaultListViewDataTypesCreated(ISqlSyntaxProvider sqlSyntax) - { - _sqlSyntax = sqlSyntax; - } - protected override void AfterMigration(MigrationRunner sender, MigrationEventArgs e) { if (e.ProductName != GlobalSettings.UmbracoMigrationName) return; @@ -41,13 +34,15 @@ namespace Umbraco.Web.Strategies.Migrations private void EnsureListViewDataTypeCreated(MigrationEventArgs e) { + var syntax = e.MigrationContext.Database.SqlSyntax; + using (var transaction = e.MigrationContext.Database.GetTransaction()) { try { //Turn on identity insert if db provider is not mysql - if (_sqlSyntax.SupportsIdentityInsert()) - e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", _sqlSyntax.GetQuotedTableName("umbracoNode")))); + if (syntax.SupportsIdentityInsert()) + e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", syntax.GetQuotedTableName("umbracoNode")))); if (e.MigrationContext.Database.Exists(Constants.System.DefaultContentListViewDataTypeId)) { @@ -62,16 +57,16 @@ namespace Umbraco.Web.Strategies.Migrations finally { //Turn off identity insert if db provider is not mysql - if (_sqlSyntax.SupportsIdentityInsert()) - e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF;", _sqlSyntax.GetQuotedTableName("umbracoNode")))); + if (syntax.SupportsIdentityInsert()) + e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF;", syntax.GetQuotedTableName("umbracoNode")))); } try { //Turn on identity insert if db provider is not mysql - if (_sqlSyntax.SupportsIdentityInsert()) - e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", _sqlSyntax.GetQuotedTableName("cmsDataType")))); + if (syntax.SupportsIdentityInsert()) + e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", syntax.GetQuotedTableName("cmsDataType")))); e.MigrationContext.Database.Insert("cmsDataType", "pk", false, new DataTypeDto { PrimaryKey = -26, DataTypeId = Constants.System.DefaultContentListViewDataTypeId, PropertyEditorAlias = Constants.PropertyEditors.ListViewAlias, DbType = "Nvarchar" }); e.MigrationContext.Database.Insert("cmsDataType", "pk", false, new DataTypeDto { PrimaryKey = -27, DataTypeId = Constants.System.DefaultMediaListViewDataTypeId, PropertyEditorAlias = Constants.PropertyEditors.ListViewAlias, DbType = "Nvarchar" }); @@ -80,8 +75,8 @@ namespace Umbraco.Web.Strategies.Migrations finally { //Turn off identity insert if db provider is not mysql - if (_sqlSyntax.SupportsIdentityInsert()) - e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF;", _sqlSyntax.GetQuotedTableName("cmsDataType")))); + if (syntax.SupportsIdentityInsert()) + e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF;", syntax.GetQuotedTableName("cmsDataType")))); } @@ -89,8 +84,8 @@ namespace Umbraco.Web.Strategies.Migrations try { //Turn on identity insert if db provider is not mysql - if (_sqlSyntax.SupportsIdentityInsert()) - e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", _sqlSyntax.GetQuotedTableName("cmsDataTypePreValues")))); + if (syntax.SupportsIdentityInsert()) + e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", syntax.GetQuotedTableName("cmsDataTypePreValues")))); //defaults for the member list e.MigrationContext.Database.Insert("cmsDataTypePreValues", "id", false, new DataTypePreValueDto { Id = -1, Alias = "pageSize", SortOrder = 1, DataTypeNodeId = Constants.System.DefaultMembersListViewDataTypeId, Value = "10" }); @@ -101,8 +96,8 @@ namespace Umbraco.Web.Strategies.Migrations finally { //Turn off identity insert if db provider is not mysql - if (_sqlSyntax.SupportsIdentityInsert()) - e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF;", _sqlSyntax.GetQuotedTableName("cmsDataTypePreValues")))); + if (syntax.SupportsIdentityInsert()) + e.MigrationContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF;", syntax.GetQuotedTableName("cmsDataTypePreValues")))); } diff --git a/src/Umbraco.Web/Strategies/Migrations/PublishAfterUpgradeToVersionSixth.cs b/src/Umbraco.Web/Strategies/Migrations/PublishAfterUpgradeToVersionSixth.cs index 70de7c0227..72f28c4628 100644 --- a/src/Umbraco.Web/Strategies/Migrations/PublishAfterUpgradeToVersionSixth.cs +++ b/src/Umbraco.Web/Strategies/Migrations/PublishAfterUpgradeToVersionSixth.cs @@ -19,13 +19,6 @@ namespace Umbraco.Web.Strategies.Migrations /// public class PublishAfterUpgradeToVersionSixth : MigrationStartupHander { - private readonly ISqlSyntaxProvider _sqlSyntax; - - public PublishAfterUpgradeToVersionSixth(ISqlSyntaxProvider sqlSyntax) - { - _sqlSyntax = sqlSyntax; - } - protected override void AfterMigration(MigrationRunner sender, MigrationEventArgs e) { if (e.ProductName != GlobalSettings.UmbracoMigrationName) return;