diff --git a/src/Umbraco.Core/DatabaseContext.cs b/src/Umbraco.Core/DatabaseContext.cs index 4f52f61683..1311593e90 100644 --- a/src/Umbraco.Core/DatabaseContext.cs +++ b/src/Umbraco.Core/DatabaseContext.cs @@ -296,16 +296,13 @@ namespace Umbraco.Core if (databaseSettings != null && string.IsNullOrWhiteSpace(databaseSettings.ConnectionString) == false && string.IsNullOrWhiteSpace(databaseSettings.ProviderName) == false) { var providerName = "System.Data.SqlClient"; + string connString = null; if (!string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[GlobalSettings.UmbracoConnectionName].ProviderName)) { providerName = ConfigurationManager.ConnectionStrings[GlobalSettings.UmbracoConnectionName].ProviderName; - - _connectionString = - ConfigurationManager.ConnectionStrings[GlobalSettings.UmbracoConnectionName].ConnectionString; - + connString = ConfigurationManager.ConnectionStrings[GlobalSettings.UmbracoConnectionName].ConnectionString; } - - Initialize(providerName); + Initialize(providerName, connString); } else if (ConfigurationManager.AppSettings.ContainsKey(GlobalSettings.UmbracoConnectionName) && string.IsNullOrEmpty(ConfigurationManager.AppSettings[GlobalSettings.UmbracoConnectionName]) == false) { @@ -369,6 +366,12 @@ namespace Umbraco.Core } } + internal void Initialize(string providerName, string connectionString) + { + _connectionString = connectionString; + Initialize(providerName); + } + internal DatabaseSchemaResult ValidateDatabaseSchema() { if (_configured == false || (string.IsNullOrEmpty(_connectionString) || string.IsNullOrEmpty(ProviderName))) diff --git a/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs b/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs index 1a723166e6..2014ceb1cf 100644 --- a/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs +++ b/src/Umbraco.Core/Persistence/DefaultDatabaseFactory.cs @@ -14,9 +14,9 @@ namespace Umbraco.Core.Persistence /// internal class DefaultDatabaseFactory : DisposableObject, IDatabaseFactory { - private readonly string _connectionStringName; - private readonly string _connectionString; - private readonly string _providerName; + private readonly string _connectionStringName; + public string ConnectionString { get; private set; } + public string ProviderName { get; private set; } //very important to have ThreadStatic: // see: http://issues.umbraco.org/issue/U4-2172 @@ -52,8 +52,8 @@ namespace Umbraco.Core.Persistence { Mandate.ParameterNotNullOrEmpty(connectionString, "connectionString"); Mandate.ParameterNotNullOrEmpty(providerName, "providerName"); - _connectionString = connectionString; - _providerName = providerName; + ConnectionString = connectionString; + ProviderName = providerName; } public UmbracoDatabase CreateDatabase() @@ -68,8 +68,8 @@ namespace Umbraco.Core.Persistence //double check if (_nonHttpInstance == null) { - _nonHttpInstance = string.IsNullOrEmpty(_connectionString) == false && string.IsNullOrEmpty(_providerName) == false - ? new UmbracoDatabase(_connectionString, _providerName) + _nonHttpInstance = string.IsNullOrEmpty(ConnectionString) == false && string.IsNullOrEmpty(ProviderName) == false + ? new UmbracoDatabase(ConnectionString, ProviderName) : new UmbracoDatabase(_connectionStringName); } } @@ -81,8 +81,8 @@ namespace Umbraco.Core.Persistence if (HttpContext.Current.Items.Contains(typeof(DefaultDatabaseFactory)) == false) { HttpContext.Current.Items.Add(typeof (DefaultDatabaseFactory), - string.IsNullOrEmpty(_connectionString) == false && string.IsNullOrEmpty(_providerName) == false - ? new UmbracoDatabase(_connectionString, _providerName) + string.IsNullOrEmpty(ConnectionString) == false && string.IsNullOrEmpty(ProviderName) == false + ? new UmbracoDatabase(ConnectionString, ProviderName) : new UmbracoDatabase(_connectionStringName)); } return (UmbracoDatabase)HttpContext.Current.Items[typeof(DefaultDatabaseFactory)]; diff --git a/src/Umbraco.Core/Persistence/PetaPocoExtensions.cs b/src/Umbraco.Core/Persistence/PetaPocoExtensions.cs index 51ae2638b0..c2db49980e 100644 --- a/src/Umbraco.Core/Persistence/PetaPocoExtensions.cs +++ b/src/Umbraco.Core/Persistence/PetaPocoExtensions.cs @@ -6,6 +6,7 @@ using Umbraco.Core.Logging; using Umbraco.Core.Models.Rdbms; using Umbraco.Core.Persistence.DatabaseModelDefinitions; using Umbraco.Core.Persistence.Migrations.Initial; +using Umbraco.Core.Persistence.Querying; using Umbraco.Core.Persistence.SqlSyntax; namespace Umbraco.Core.Persistence @@ -48,11 +49,15 @@ namespace Umbraco.Core.Persistence } else { - string sql; - using (var cmd = db.GenerateBulkInsertCommand(collection, db.Connection, out sql)) + string[] sqlStatements; + var cmds = db.GenerateBulkInsertCommand(collection, db.Connection, out sqlStatements); + for (var i = 0; i < sqlStatements.Length; i++) { - cmd.CommandText = sql; - cmd.ExecuteNonQuery(); + using (var cmd = cmds[i]) + { + cmd.CommandText = sqlStatements[i]; + cmd.ExecuteNonQuery(); + } } } @@ -66,42 +71,92 @@ namespace Umbraco.Core.Persistence } } - internal static IDbCommand GenerateBulkInsertCommand(this Database db, IEnumerable collection, IDbConnection connection, out string sql) + /// + /// Creates a bulk insert command + /// + /// + /// + /// + /// + /// + /// Sql commands with populated command parameters required to execute the sql statement + /// + /// The limits for number of parameters are 2100 (in sql server, I think there's many more allowed in mysql). So + /// we need to detect that many params and split somehow. + /// For some reason the 2100 limit is not actually allowed even though the exception from sql server mentions 2100 as a max, perhaps it is 2099 + /// that is max. I've reduced it to 2000 anyways. + /// + internal static IDbCommand[] GenerateBulkInsertCommand( + this Database db, + IEnumerable collection, + IDbConnection connection, + out string[] sql) { + //A filter used below a few times to get all columns except result cols and not the primary key if it is auto-incremental + Func, bool> includeColumn = (data, column) => + { + if (column.Value.ResultColumn) return false; + if (data.TableInfo.AutoIncrement && column.Key == data.TableInfo.PrimaryKey) return false; + return true; + }; + var pd = Database.PocoData.ForType(typeof(T)); var tableName = db.EscapeTableName(pd.TableInfo.TableName); - //get all columns but not the primary key if it is auto-incremental - var cols = string.Join(", ", ( - from c in pd.Columns - where - //don't return ResultColumns - !c.Value.ResultColumn - //if the table is auto-incremental, don't return the primary key - && (pd.TableInfo.AutoIncrement && c.Key != pd.TableInfo.PrimaryKey) - select tableName + "." + db.EscapeSqlIdentifier(c.Key)) - .ToArray()); + //get all columns to include and format for sql + var cols = string.Join(", ", + pd.Columns + .Where(c => includeColumn(pd, c)) + .Select(c => tableName + "." + db.EscapeSqlIdentifier(c.Key)).ToArray()); - var cmd = db.CreateCommand(connection, ""); + var itemArray = collection.ToArray(); - var pocoValues = new List(); - var index = 0; - foreach (var poco in collection) + //calculate number of parameters per item + var paramsPerItem = pd.Columns.Count(i => includeColumn(pd, i)); + + //Example calc: + // Given: we have 4168 items in the itemArray, each item contains 8 command parameters (values to be inserterted) + // 2100 / 8 = 262.5 + // Math.Floor(2100 / 8) = 262 items per trans + // 4168 / 262 = 15.908... = there will be 16 trans in total + + //all items will be included if we have disabled db parameters + var itemsPerTrans = Math.Floor(2000.00 / paramsPerItem); + //there will only be one transaction if we have disabled db parameters + var numTrans = Math.Ceiling(itemArray.Length / itemsPerTrans); + + var sqlQueries = new List(); + var commands = new List(); + + for (var tIndex = 0; tIndex < numTrans; tIndex++) { - var values = new List(); - foreach (var i in pd.Columns) + var itemsForTrans = itemArray + .Skip(tIndex * (int)itemsPerTrans) + .Take((int)itemsPerTrans); + + var cmd = db.CreateCommand(connection, ""); + var pocoValues = new List(); + var index = 0; + foreach (var poco in itemsForTrans) { - if (pd.TableInfo.AutoIncrement && i.Key == pd.TableInfo.PrimaryKey) + var values = new List(); + //get all columns except result cols and not the primary key if it is auto-incremental + foreach (var i in pd.Columns.Where(x => includeColumn(pd, x))) { - continue; + db.AddParam(cmd, i.Value.GetValue(poco), "@"); + values.Add(string.Format("{0}{1}", "@", index++)); } - values.Add(string.Format("{0}{1}", "@", index++)); - db.AddParam(cmd, i.Value.GetValue(poco), "@"); + pocoValues.Add("(" + string.Join(",", values.ToArray()) + ")"); } - pocoValues.Add("(" + string.Join(",", values.ToArray()) + ")"); + + var sqlResult = string.Format("INSERT INTO {0} ({1}) VALUES {2}", tableName, cols, string.Join(", ", pocoValues)); + sqlQueries.Add(sqlResult); + commands.Add(cmd); } - sql = string.Format("INSERT INTO {0} ({1}) VALUES {2}", tableName, cols, string.Join(", ", pocoValues)); - return cmd; + + sql = sqlQueries.ToArray(); + + return commands.ToArray(); } public static void CreateTable(this Database db, bool overwrite, Type modelType) diff --git a/src/Umbraco.Core/Persistence/Querying/ModelToSqlExpressionHelper.cs b/src/Umbraco.Core/Persistence/Querying/ModelToSqlExpressionHelper.cs index 470a098fd8..559b978721 100644 --- a/src/Umbraco.Core/Persistence/Querying/ModelToSqlExpressionHelper.cs +++ b/src/Umbraco.Core/Persistence/Querying/ModelToSqlExpressionHelper.cs @@ -435,38 +435,7 @@ namespace Umbraco.Core.Persistence.Querying public virtual string GetQuotedValue(object value, Type fieldType) { - if (value == null) return "NULL"; - - if (!fieldType.UnderlyingSystemType.IsValueType && fieldType != typeof(string)) - { - throw new NotSupportedException( - string.Format("Property of type: {0} is not supported", fieldType.FullName)); - } - - if (fieldType == typeof(int)) - return ((int)value).ToString(CultureInfo.InvariantCulture); - - if (fieldType == typeof(float)) - return ((float)value).ToString(CultureInfo.InvariantCulture); - - if (fieldType == typeof(double)) - return ((double)value).ToString(CultureInfo.InvariantCulture); - - if (fieldType == typeof(decimal)) - return ((decimal)value).ToString(CultureInfo.InvariantCulture); - - if (fieldType == typeof (DateTime)) - { - return "'" + EscapeParam(((DateTime)value).ToIsoString()) + "'"; - } - - - if (fieldType == typeof(bool)) - return ((bool)value) ? Convert.ToString(1, CultureInfo.InvariantCulture) : Convert.ToString(0, CultureInfo.InvariantCulture); - - return ShouldQuoteValue(fieldType) - ? "'" + EscapeParam(value) + "'" - : value.ToString(); + return QueryHelper.GetQuotedValue(value, fieldType, EscapeParam, ShouldQuoteValue); } public virtual string EscapeParam(object paramValue) diff --git a/src/Umbraco.Core/Persistence/Querying/PocoToSqlExpressionHelper.cs b/src/Umbraco.Core/Persistence/Querying/PocoToSqlExpressionHelper.cs index d065849102..f84933b3e7 100644 --- a/src/Umbraco.Core/Persistence/Querying/PocoToSqlExpressionHelper.cs +++ b/src/Umbraco.Core/Persistence/Querying/PocoToSqlExpressionHelper.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Globalization; using System.Linq; using System.Linq.Expressions; using System.Text; @@ -440,43 +439,7 @@ namespace Umbraco.Core.Persistence.Querying public virtual string GetQuotedValue(object value, Type fieldType) { - if (value == null) return "NULL"; - - if (!fieldType.UnderlyingSystemType.IsValueType && fieldType != typeof(string)) - { - //if (TypeSerializer.CanCreateFromString(fieldType)) - //{ - // return "'" + EscapeParam(TypeSerializer.SerializeToString(value)) + "'"; - //} - - throw new NotSupportedException( - string.Format("Property of type: {0} is not supported", fieldType.FullName)); - } - - if (fieldType == typeof(int)) - return ((int)value).ToString(CultureInfo.InvariantCulture); - - if (fieldType == typeof(float)) - return ((float)value).ToString(CultureInfo.InvariantCulture); - - if (fieldType == typeof(double)) - return ((double)value).ToString(CultureInfo.InvariantCulture); - - if (fieldType == typeof(decimal)) - return ((decimal)value).ToString(CultureInfo.InvariantCulture); - - if (fieldType == typeof (DateTime)) - { - return "'" + EscapeParam(((DateTime)value).ToIsoString()) + "'"; - } - - - if (fieldType == typeof(bool)) - return ((bool)value) ? Convert.ToString(1, CultureInfo.InvariantCulture) : Convert.ToString(0, CultureInfo.InvariantCulture); - - return ShouldQuoteValue(fieldType) - ? "'" + EscapeParam(value) + "'" - : value.ToString(); + return QueryHelper.GetQuotedValue(value, fieldType, EscapeParam, ShouldQuoteValue); } public virtual string EscapeParam(object paramValue) diff --git a/src/Umbraco.Core/Persistence/Querying/QueryHelper.cs b/src/Umbraco.Core/Persistence/Querying/QueryHelper.cs new file mode 100644 index 0000000000..f69e106f57 --- /dev/null +++ b/src/Umbraco.Core/Persistence/Querying/QueryHelper.cs @@ -0,0 +1,70 @@ +using System; +using System.Globalization; + +namespace Umbraco.Core.Persistence.Querying +{ + /// + /// Logic that is shared with the expression helpers + /// + internal class QueryHelper + { + public static string GetQuotedValue(object value, Type fieldType, Func escapeCallback = null, Func shouldQuoteCallback = null) + { + if (value == null) return "NULL"; + + if (escapeCallback == null) + { + escapeCallback = EscapeParam; + } + if (shouldQuoteCallback == null) + { + shouldQuoteCallback = ShouldQuoteValue; + } + + if (!fieldType.UnderlyingSystemType.IsValueType && fieldType != typeof(string)) + { + //if (TypeSerializer.CanCreateFromString(fieldType)) + //{ + // return "'" + EscapeParam(TypeSerializer.SerializeToString(value)) + "'"; + //} + + throw new NotSupportedException( + string.Format("Property of type: {0} is not supported", fieldType.FullName)); + } + + if (fieldType == typeof(int)) + return ((int)value).ToString(CultureInfo.InvariantCulture); + + if (fieldType == typeof(float)) + return ((float)value).ToString(CultureInfo.InvariantCulture); + + if (fieldType == typeof(double)) + return ((double)value).ToString(CultureInfo.InvariantCulture); + + if (fieldType == typeof(decimal)) + return ((decimal)value).ToString(CultureInfo.InvariantCulture); + + if (fieldType == typeof(DateTime)) + { + return "'" + EscapeParam(((DateTime)value).ToIsoString()) + "'"; + } + + if (fieldType == typeof(bool)) + return ((bool)value) ? Convert.ToString(1, CultureInfo.InvariantCulture) : Convert.ToString(0, CultureInfo.InvariantCulture); + + return ShouldQuoteValue(fieldType) + ? "'" + EscapeParam(value) + "'" + : value.ToString(); + } + + public static string EscapeParam(object paramValue) + { + return paramValue.ToString().Replace("'", "''"); + } + + public static bool ShouldQuoteValue(Type fieldType) + { + return true; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index 94aa0f9140..3c4eedcc0d 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -1374,6 +1374,8 @@ namespace Umbraco.Core.Services { foreach (var id in contentTypeIds) { + + //first we'll clear out the data from the cmsContentXml table for this type uow.Database.Execute(@"delete from cmsContentXml where nodeId in (select cmsDocument.nodeId from cmsDocument diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index d2a5e33a28..b1b83ace68 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -462,6 +462,7 @@ + diff --git a/src/Umbraco.Tests/Persistence/PetaPocoExtensionsTest.cs b/src/Umbraco.Tests/Persistence/PetaPocoExtensionsTest.cs index 1cf6aff7b0..3a76e29139 100644 --- a/src/Umbraco.Tests/Persistence/PetaPocoExtensionsTest.cs +++ b/src/Umbraco.Tests/Persistence/PetaPocoExtensionsTest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text.RegularExpressions; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Models.Rdbms; @@ -73,13 +74,47 @@ namespace Umbraco.Tests.Persistence db.OpenSharedConnection(); // Act - string sql; + string[] sql; db.GenerateBulkInsertCommand(servers, db.Connection, out sql); db.CloseSharedConnection(); // Assert - Assert.That(sql, + Assert.That(sql[0], Is.EqualTo("INSERT INTO [umbracoServer] ([umbracoServer].[address], [umbracoServer].[computerName], [umbracoServer].[registeredDate], [umbracoServer].[lastNotifiedDate], [umbracoServer].[isActive]) VALUES (@0,@1,@2,@3,@4), (@5,@6,@7,@8,@9)")); } + + + [Test] + public void Generate_Bulk_Import_Sql_Exceeding_Max_Params() + { + // Arrange + var db = DatabaseContext.Database; + + var servers = new List(); + for (var i = 0; i < 1500; i++) + { + servers.Add(new ServerRegistrationDto + { + Address = "address" + i, + ComputerName = "computer" + i, + DateRegistered = DateTime.Now, + IsActive = true, + LastNotified = DateTime.Now + }); + } + db.OpenSharedConnection(); + + // Act + string[] sql; + db.GenerateBulkInsertCommand(servers, db.Connection, out sql); + db.CloseSharedConnection(); + + // Assert + Assert.That(sql.Length, Is.EqualTo(4)); + foreach (var s in sql) + { + Assert.LessOrEqual(Regex.Matches(s, "@\\d+").Count, 2000); + } + } } } \ No newline at end of file diff --git a/src/Umbraco.Tests/Services/PerformanceTests.cs b/src/Umbraco.Tests/Services/PerformanceTests.cs new file mode 100644 index 0000000000..f764a4cd04 --- /dev/null +++ b/src/Umbraco.Tests/Services/PerformanceTests.cs @@ -0,0 +1,211 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data.SqlServerCe; +using System.Globalization; +using System.Linq; +using System.Threading; +using System.Xml.Linq; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Core.Models.Rdbms; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.Repositories; +using Umbraco.Core.Persistence.SqlSyntax; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Core.Services; +using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Entities; + +namespace Umbraco.Tests.Services +{ + /// + /// Tests covering all methods in the ContentService class. + /// This is more of an integration test as it involves multiple layers + /// as well as configuration. + /// + [TestFixture, RequiresSTA] + [NUnit.Framework.Ignore("These should not be run by the server, only directly as they are only benchmark tests")] + public class PerformanceTests : BaseDatabaseFactoryTest + { + [SetUp] + public override void Initialize() + { + base.Initialize(); + } + + protected override string GetDbConnectionString() + { + return @"server=.\SQLEXPRESS;database=UmbTest;user id=sa;password=test"; + } + + protected override string GetDbProviderName() + { + return "System.Data.SqlClient"; + } + + /// + /// new schema per test + /// + protected override DatabaseBehavior DatabaseTestBehavior + { + get { return DatabaseBehavior.NewSchemaPerTest; } + } + + /// + /// don't create anything, we're testing against our own server + /// + protected override void CreateSqlCeDatabase() + { + } + + [TearDown] + public override void TearDown() + { + base.TearDown(); + } + + [Test] + public void Truncate_Insert_Vs_Update_Insert() + { + var customObjectType = Guid.NewGuid(); + //chuck lots of data in the db + var nodes = PrimeDbWithLotsOfContentXmlRecords(customObjectType); + + //now we need to test the difference between truncating all records and re-inserting them as we do now, + //vs updating them (which might result in checking if they exist for or listening on an exception). + using (DisposableTimer.DebugDuration("Starting truncate + normal insert test")) + { + //do this 10x! + for (var i = 0; i < 10; i++) + { + //clear all the xml entries + DatabaseContext.Database.Execute(@"DELETE FROM cmsContentXml WHERE nodeId IN + (SELECT DISTINCT cmsContentXml.nodeId FROM cmsContentXml + INNER JOIN cmsContent ON cmsContentXml.nodeId = cmsContent.nodeId)"); + + //now we insert each record for the ones we've deleted like we do in the content service. + var xmlItems = nodes.Select(node => new ContentXmlDto { NodeId = node.NodeId, Xml = UpdatedXmlStructure }).ToList(); + foreach (var xml in xmlItems) + { + var result = DatabaseContext.Database.Insert(xml); + } + } + } + + //now, isntead of truncating, we'll attempt to update and if it doesn't work then we insert + using (DisposableTimer.DebugDuration("Starting update test")) + { + //do this 10x! + for (var i = 0; i < 10; i++) + { + //now we insert each record for the ones we've deleted like we do in the content service. + var xmlItems = nodes.Select(node => new ContentXmlDto { NodeId = node.NodeId, Xml = UpdatedXmlStructure }).ToList(); + foreach (var xml in xmlItems) + { + var result = DatabaseContext.Database.Update(xml); + } + } + } + + //now, isntead of truncating, we'll attempt to update and if it doesn't work then we insert + using (DisposableTimer.DebugDuration("Starting truncate + bulk insert test")) + { + //do this 10x! + for (var i = 0; i < 10; i++) + { + //clear all the xml entries + DatabaseContext.Database.Execute(@"DELETE FROM cmsContentXml WHERE nodeId IN + (SELECT DISTINCT cmsContentXml.nodeId FROM cmsContentXml + INNER JOIN cmsContent ON cmsContentXml.nodeId = cmsContent.nodeId)"); + + //now we insert each record for the ones we've deleted like we do in the content service. + var xmlItems = nodes.Select(node => new ContentXmlDto { NodeId = node.NodeId, Xml = UpdatedXmlStructure }).ToList(); + DatabaseContext.Database.BulkInsertRecords(xmlItems); + } + } + + + + } + + private IEnumerable PrimeDbWithLotsOfContentXmlRecords(Guid customObjectType) + { + var nodes = new List(); + for (var i = 1; i < 10000; i++) + { + nodes.Add(new NodeDto + { + Level = 1, + ParentId = -1, + NodeObjectType = customObjectType, + Text = i.ToString(CultureInfo.InvariantCulture), + UserId = 0, + CreateDate = DateTime.Now, + Trashed = false, + SortOrder = 0, + Path = "" + }); + } + DatabaseContext.Database.BulkInsertRecords(nodes); + + //re-get the nodes with ids + var sql = new Sql(); + sql.Select("*").From().Where(x => x.NodeObjectType == customObjectType); + nodes = DatabaseContext.Database.Fetch(sql); + + //create the cmsContent data, each with a new content type id (so we can query on it later if needed) + var contentTypeId = 0; + var cmsContentItems = nodes.Select(node => new ContentDto { NodeId = node.NodeId, ContentTypeId = contentTypeId++ }).ToList(); + DatabaseContext.Database.BulkInsertRecords(cmsContentItems); + + //create the xml data + var xmlItems = nodes.Select(node => new ContentXmlDto { NodeId = node.NodeId, Xml = TestXmlStructure }).ToList(); + DatabaseContext.Database.BulkInsertRecords(xmlItems); + + return nodes; + } + + private const string TestXmlStructure = @" + 0 + Standard Site for Umbraco by Koiak + + + + Built by Creative Founds +

Web ApplicationsCreative Founds design and build first class software solutions that deliver big results. We provide ASP.NET web and mobile applications, Umbraco development service & technical consultancy.

+

www.creativefounds.co.uk

]]>
+ Umbraco Development +

UmbracoUmbraco the the leading ASP.NET open source CMS, under pinning over 150,000 websites. Our Certified Developers are experts in developing high performance and feature rich websites.

]]>
+ Contact Us +

Contact Us on TwitterWe'd love to hear how this package has helped you and how it can be improved. Get in touch on the project website or via twitter

]]>
+ +
Standard Website MVC, Company Address, Glasgow, Postcode
+ Copyright &copy; 2012 Your Company + http://www.umbraco.org + /media/1477/umbraco_logo.png + + + + + + + 2013-07-01T00:00:00 +
"; + + private const string UpdatedXmlStructure = @" + 0 + + + + Clients +

This is a standard content page.

+

Vestibulum malesuada aliquet ante, vitae ullamcorper felis faucibus vel. Vestibulum condimentum faucibus tellus porta ultrices. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

+

Cras at auctor orci. Praesent facilisis erat nec odio consequat at posuere ligula pretium. Nulla eget felis id nisl volutpat pellentesque. Ut id augue id ligula placerat rutrum a nec purus. Maecenas sed lectus ac mi pellentesque luctus quis sit amet turpis. Vestibulum adipiscing convallis vestibulum.

+

Duis condimentum lectus at orci placerat vitae imperdiet lorem cursus. Duis hendrerit porta lorem, non suscipit quam consectetur vitae. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean elit augue, tincidunt nec tincidunt id, elementum vel est.

]]>
+ +
"; + + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs index d6ef094169..5a98e303a4 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs @@ -56,20 +56,23 @@ namespace Umbraco.Tests.TestHelpers var path = TestHelper.CurrentAssemblyDirectory; AppDomain.CurrentDomain.SetData("DataDirectory", path); + var dbFactory = new DefaultDatabaseFactory( + GetDbConnectionString(), + GetDbProviderName()); ApplicationContext.Current = new ApplicationContext( //assign the db context - new DatabaseContext(new DefaultDatabaseFactory()), + new DatabaseContext(dbFactory), //assign the service context - new ServiceContext(new PetaPocoUnitOfWorkProvider(), new FileUnitOfWorkProvider(), new PublishingStrategy()), + new ServiceContext(new PetaPocoUnitOfWorkProvider(dbFactory), new FileUnitOfWorkProvider(), new PublishingStrategy()), //disable cache false) { IsReady = true }; - DatabaseContext.Initialize(); + DatabaseContext.Initialize(dbFactory.ProviderName, dbFactory.ConnectionString); - CreateDatabase(); + CreateSqlCeDatabase(); InitializeDatabase(); @@ -85,10 +88,23 @@ namespace Umbraco.Tests.TestHelpers get { return DatabaseBehavior.NewSchemaPerTest; } } + protected virtual string GetDbProviderName() + { + return "System.Data.SqlServerCe.4.0"; + } + + /// + /// Get the db conn string + /// + protected virtual string GetDbConnectionString() + { + return @"Datasource=|DataDirectory|UmbracoPetaPocoTests.sdf"; + } + /// /// Creates the SqlCe database if required /// - protected virtual void CreateDatabase() + protected virtual void CreateSqlCeDatabase() { if (DatabaseTestBehavior == DatabaseBehavior.NoDatabasePerFixture) return; @@ -97,7 +113,9 @@ namespace Umbraco.Tests.TestHelpers //Get the connectionstring settings from config var settings = ConfigurationManager.ConnectionStrings[Core.Configuration.GlobalSettings.UmbracoConnectionName]; - ConfigurationManager.AppSettings.Set(Core.Configuration.GlobalSettings.UmbracoConnectionName, @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\UmbracoPetaPocoTests.sdf"); + ConfigurationManager.AppSettings.Set( + Core.Configuration.GlobalSettings.UmbracoConnectionName, + GetDbConnectionString()); string dbFilePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf"); diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index d9b9b857b1..66764feae5 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -188,6 +188,7 @@ +