diff --git a/src/Umbraco.Core/DatabaseContext.cs b/src/Umbraco.Core/DatabaseContext.cs index fad96b849d..253c5cc04e 100644 --- a/src/Umbraco.Core/DatabaseContext.cs +++ b/src/Umbraco.Core/DatabaseContext.cs @@ -24,7 +24,10 @@ namespace Umbraco.Core /// public class DatabaseContext { + + private readonly IDatabaseFactory _factory; + private readonly ILogger _logger; private bool _configured; private bool _canConnect; private volatile bool _connectCheck = false; @@ -33,11 +36,21 @@ namespace Umbraco.Core private string _providerName; private DatabaseSchemaResult _result; + [Obsolete("Use the constructor specifying all dependencies instead")] public DatabaseContext(IDatabaseFactory factory) + : this(factory, LoggerResolver.Current.Logger, SqlSyntaxContext.SqlSyntaxProvider) { - _factory = factory; } + public DatabaseContext(IDatabaseFactory factory, ILogger logger, ISqlSyntaxProvider sqlSyntax) + { + SqlSyntax = sqlSyntax; + _factory = factory; + _logger = logger; + } + + public ISqlSyntaxProvider SqlSyntax { get; private set; } + /// /// Gets the object for doing CRUD operations /// against custom tables that resides in the Umbraco database. @@ -324,7 +337,7 @@ namespace Umbraco.Core xml.Save(fileName, SaveOptions.DisableFormatting); - LogHelper.Info("Configured a new ConnectionString using the '" + providerName + "' provider."); + _logger.Info("Configured a new ConnectionString using the '" + providerName + "' provider."); } /// @@ -402,8 +415,8 @@ namespace Umbraco.Core try { - SqlSyntaxContext.SqlSyntaxProvider = - SqlSyntaxProvidersResolver.Current.GetByProviderNameOrDefault(providerName); + SqlSyntax = SqlSyntaxProvidersResolver.Current.GetByProviderNameOrDefault(providerName); + SqlSyntaxContext.SqlSyntaxProvider = SqlSyntax; _configured = true; } @@ -411,8 +424,8 @@ namespace Umbraco.Core { _configured = false; - LogHelper.Info("Initialization of the DatabaseContext failed with following error: " + e.Message); - LogHelper.Info(e.StackTrace); + _logger.Info("Initialization of the DatabaseContext failed with following error: " + e.Message); + _logger.Info(e.StackTrace); } } @@ -427,8 +440,8 @@ namespace Umbraco.Core /// private void DetermineSqlServerVersion() { - - var sqlServerSyntax = SqlSyntaxContext.SqlSyntaxProvider as SqlServerSyntaxProvider; + + var sqlServerSyntax = SqlSyntax as SqlServerSyntaxProvider; if (sqlServerSyntax != null) { //this will not execute now, it is lazy so will only execute when we need to actually know @@ -480,7 +493,7 @@ namespace Umbraco.Core } var database = new UmbracoDatabase(_connectionString, ProviderName); - var dbSchema = new DatabaseSchemaCreation(database); + var dbSchema = new DatabaseSchemaCreation(database, _logger, SqlSyntax); _result = dbSchema.ValidateSchema(); } return _result; @@ -496,7 +509,7 @@ namespace Umbraco.Core return readyForInstall.Result; } - LogHelper.Info("Database configuration status: Started"); + _logger.Info("Database configuration status: Started"); string message; @@ -506,7 +519,7 @@ namespace Umbraco.Core // for case insensitive queries. In an ideal situation (which is what we're striving for), all calls would be case sensitive. /* - var supportsCaseInsensitiveQueries = SqlSyntaxContext.SqlSyntaxProvider.SupportsCaseInsensitiveQueries(database); + var supportsCaseInsensitiveQueries = SqlSyntax.SupportsCaseInsensitiveQueries(database); if (supportsCaseInsensitiveQueries == false) { message = "

 

The database you're trying to use does not support case insensitive queries.
We currently do not support these types of databases.

" + @@ -532,12 +545,12 @@ namespace Umbraco.Core message = message + "

Installation completed!

"; //now that everything is done, we need to determine the version of SQL server that is executing - LogHelper.Info("Database configuration status: " + message); + _logger.Info("Database configuration status: " + message); return new Result { Message = message, Success = true, Percentage = "100" }; } //we need to do an upgrade so return a new status message and it will need to be done during the next step - LogHelper.Info("Database requires upgrade"); + _logger.Info("Database requires upgrade"); message = "

Upgrading database, this may take some time...

"; return new Result { @@ -568,10 +581,10 @@ namespace Umbraco.Core return readyForInstall.Result; } - LogHelper.Info("Database upgrade started"); + _logger.Info("Database upgrade started"); var database = new UmbracoDatabase(_connectionString, ProviderName); - //var supportsCaseInsensitiveQueries = SqlSyntaxContext.SqlSyntaxProvider.SupportsCaseInsensitiveQueries(database); + //var supportsCaseInsensitiveQueries = SqlSyntax.SupportsCaseInsensitiveQueries(database); var message = GetResultMessageForMySql(); @@ -590,7 +603,7 @@ namespace Umbraco.Core //now that everything is done, we need to determine the version of SQL server that is executing - LogHelper.Info("Database configuration status: " + message); + _logger.Info("Database configuration status: " + message); return new Result { Message = message, Success = true, Percentage = "100" }; } @@ -602,7 +615,7 @@ namespace Umbraco.Core private string GetResultMessageForMySql() { - if (SqlSyntaxContext.SqlSyntaxProvider.GetType() == typeof(MySqlSyntaxProvider)) + if (SqlSyntax.GetType() == typeof(MySqlSyntaxProvider)) { return "

 

Congratulations, the database step ran successfully!

" + "

Note: You're using MySQL and the database instance you're connecting to seems to support case insensitive queries.

" + @@ -628,7 +641,7 @@ namespace Umbraco.Core "

For more technical information on case sensitivity in MySQL, have a look at " + "the documentation on the subject

"; } - if (SqlSyntaxContext.SqlSyntaxProvider.GetType() == typeof(MySqlSyntaxProvider)) + if (SqlSyntax.GetType() == typeof(MySqlSyntaxProvider)) { return "

 

Congratulations, the database step ran successfully!

" + "

Note: You're using MySQL and the database instance you're connecting to seems to support case insensitive queries.

" + @@ -665,11 +678,11 @@ namespace Umbraco.Core private Result HandleInstallException(Exception ex) { - LogHelper.Error("Database configuration failed", ex); + _logger.Error("Database configuration failed", ex); if (_result != null) { - LogHelper.Info("The database schema validation produced the following summary: \n" + _result.GetSummary()); + _logger.Info("The database schema validation produced the following summary: \n" + _result.GetSummary()); } return new Result diff --git a/src/Umbraco.Core/Persistence/DatabaseSchemaHelper.cs b/src/Umbraco.Core/Persistence/DatabaseSchemaHelper.cs new file mode 100644 index 0000000000..bb1f7f3071 --- /dev/null +++ b/src/Umbraco.Core/Persistence/DatabaseSchemaHelper.cs @@ -0,0 +1,213 @@ +using System; +using System.Linq; +using Umbraco.Core.Logging; +using Umbraco.Core.Models.Rdbms; +using Umbraco.Core.Persistence.DatabaseModelDefinitions; +using Umbraco.Core.Persistence.Migrations.Initial; +using Umbraco.Core.Persistence.SqlSyntax; + +namespace Umbraco.Core.Persistence +{ + public class DatabaseSchemaHelper + { + private readonly Database _db; + private readonly ILogger _logger; + private readonly ISqlSyntaxProvider _syntaxProvider; + + internal delegate void CreateTableEventHandler(string tableName, Database db, TableCreationEventArgs e, ILogger logger); + internal static event CreateTableEventHandler NewTable; + + public DatabaseSchemaHelper(Database db, ILogger logger, ISqlSyntaxProvider syntaxProvider) + { + _db = db; + _logger = logger; + _syntaxProvider = syntaxProvider; + } + + private static void OnNewTable(string tablename, Database db, TableCreationEventArgs e, ILogger logger) + { + CreateTableEventHandler handler = NewTable; + if (handler != null) handler(tablename, db, e, logger); + } + + public bool TableExist(string tableName) + { + return _syntaxProvider.DoesTableExist(_db, tableName); + } + + internal void UninstallDatabaseSchema() + { + var creation = new DatabaseSchemaCreation(_db, _logger, _syntaxProvider); + creation.UninstallDatabaseSchema(); + } + + /// + /// Creates the Umbraco db schema in the Database of the current Database. + /// Safe method that is only able to create the schema in non-configured + /// umbraco instances. + /// + public void CreateDatabaseSchema(ApplicationContext applicationContext) + { + if (applicationContext == null) throw new ArgumentNullException("applicationContext"); + CreateDatabaseSchema(true, applicationContext); + } + + /// + /// Creates the Umbraco db schema in the Database of the current Database + /// with the option to guard the db from having the schema created + /// multiple times. + /// + /// + /// + public void CreateDatabaseSchema(bool guardConfiguration, ApplicationContext applicationContext) + { + if (applicationContext == null) throw new ArgumentNullException("applicationContext"); + + if (guardConfiguration && applicationContext.IsConfigured) + throw new Exception("Umbraco is already configured!"); + + CreateDatabaseSchemaDo(); + } + + internal void CreateDatabaseSchemaDo(bool guardConfiguration, ApplicationContext applicationContext) + { + if (guardConfiguration && applicationContext.IsConfigured) + throw new Exception("Umbraco is already configured!"); + + CreateDatabaseSchemaDo(); + } + + internal void CreateDatabaseSchemaDo() + { + NewTable += PetaPocoExtensions_NewTable; + + _logger.Info("Initializing database schema creation"); + + var creation = new DatabaseSchemaCreation(_db, _logger, _syntaxProvider); + creation.InitializeDatabaseSchema(); + + _logger.Info("Finalized database schema creation"); + + NewTable -= PetaPocoExtensions_NewTable; + } + + private static void PetaPocoExtensions_NewTable(string tableName, Database db, TableCreationEventArgs e, ILogger logger) + { + var baseDataCreation = new BaseDataCreation(db, logger); + baseDataCreation.InitializeBaseData(tableName); + } + + public void CreateTable(bool overwrite) + where T : new() + { + var tableType = typeof(T); + CreateTable(overwrite, tableType); + } + + public void CreateTable() + where T : new() + { + var tableType = typeof(T); + CreateTable(false, tableType); + } + + public void CreateTable(bool overwrite, Type modelType) + { + var tableDefinition = DefinitionFactory.GetTableDefinition(modelType); + var tableName = tableDefinition.Name; + + string createSql = _syntaxProvider.Format(tableDefinition); + string createPrimaryKeySql = _syntaxProvider.FormatPrimaryKey(tableDefinition); + var foreignSql = _syntaxProvider.Format(tableDefinition.ForeignKeys); + var indexSql = _syntaxProvider.Format(tableDefinition.Indexes); + + var tableExist = _db.TableExist(tableName); + if (overwrite && tableExist) + { + _db.DropTable(tableName); + tableExist = false; + } + + if (tableExist == false) + { + using (var transaction = _db.GetTransaction()) + { + //Execute the Create Table sql + int created = _db.Execute(new Sql(createSql)); + _logger.Info(string.Format("Create Table sql {0}:\n {1}", created, createSql)); + + //If any statements exists for the primary key execute them here + if (!string.IsNullOrEmpty(createPrimaryKeySql)) + { + int createdPk = _db.Execute(new Sql(createPrimaryKeySql)); + _logger.Info(string.Format("Primary Key sql {0}:\n {1}", createdPk, createPrimaryKeySql)); + } + + //Fires the NewTable event, which is used internally to insert base data before adding constrants to the schema + if (NewTable != null) + { + var e = new TableCreationEventArgs(); + + //Turn on identity insert if db provider is not mysql + if (_syntaxProvider.SupportsIdentityInsert() && tableDefinition.Columns.Any(x => x.IsIdentity)) + _db.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", _syntaxProvider.GetQuotedTableName(tableName)))); + + //Call the NewTable-event to trigger the insert of base/default data + OnNewTable(tableName, _db, e, _logger); + + //Turn off identity insert if db provider is not mysql + if (_syntaxProvider.SupportsIdentityInsert() && tableDefinition.Columns.Any(x => x.IsIdentity)) + _db.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF;", _syntaxProvider.GetQuotedTableName(tableName)))); + + //Special case for MySql + if (_syntaxProvider is MySqlSyntaxProvider && tableName.Equals("umbracoUser")) + { + _db.Update("SET id = @IdAfter WHERE id = @IdBefore AND userLogin = @Login", new { IdAfter = 0, IdBefore = 1, Login = "admin" }); + } + } + + //Loop through foreignkey statements and execute sql + foreach (var sql in foreignSql) + { + int createdFk = _db.Execute(new Sql(sql)); + _logger.Info(string.Format("Create Foreign Key sql {0}:\n {1}", createdFk, sql)); + } + + //Loop through index statements and execute sql + foreach (var sql in indexSql) + { + int createdIndex = _db.Execute(new Sql(sql)); + _logger.Info(string.Format("Create Index sql {0}:\n {1}", createdIndex, sql)); + } + + transaction.Complete(); + } + } + + _logger.Info(string.Format("New table '{0}' was created", tableName)); + } + + public void DropTable() + where T : new() + { + Type type = typeof(T); + var tableNameAttribute = type.FirstAttribute(); + if (tableNameAttribute == null) + throw new Exception( + string.Format( + "The Type '{0}' does not contain a TableNameAttribute, which is used to find the name of the table to drop. The operation could not be completed.", + type.Name)); + + string tableName = tableNameAttribute.Value; + DropTable(tableName); + } + + public void DropTable(string tableName) + { + var sql = new Sql(string.Format( + SqlSyntaxContext.SqlSyntaxProvider.DropTable, + SqlSyntaxContext.SqlSyntaxProvider.GetQuotedTableName(tableName))); + _db.Execute(sql); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaCreation.cs b/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaCreation.cs index 7b32b59564..8f75bbb189 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaCreation.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaCreation.cs @@ -14,8 +14,27 @@ namespace Umbraco.Core.Persistence.Migrations.Initial ///
internal class DatabaseSchemaCreation { + /// + /// Constructor + /// + /// + /// + /// + public DatabaseSchemaCreation(Database database, ILogger logger, ISqlSyntaxProvider sqlSyntaxProvider) + { + _database = database; + _logger = logger; + _sqlSyntaxProvider = sqlSyntaxProvider; + _schemaHelper = new DatabaseSchemaHelper(database, logger, sqlSyntaxProvider); + } + #region Private Members + + private readonly DatabaseSchemaHelper _schemaHelper; private readonly Database _database; + private readonly ILogger _logger; + private readonly ISqlSyntaxProvider _sqlSyntaxProvider; + private static readonly Dictionary OrderedTables = new Dictionary { {0, typeof (NodeDto)}, @@ -68,7 +87,7 @@ namespace Umbraco.Core.Persistence.Migrations.Initial /// internal void UninstallDatabaseSchema() { - LogHelper.Info("Start UninstallDatabaseSchema"); + _logger.Info("Start UninstallDatabaseSchema"); foreach (var item in OrderedTables.OrderByDescending(x => x.Key)) { @@ -76,28 +95,25 @@ namespace Umbraco.Core.Persistence.Migrations.Initial string tableName = tableNameAttribute == null ? item.Value.Name : tableNameAttribute.Value; - LogHelper.Info("Uninstall" + tableName); + _logger.Info("Uninstall" + tableName); try { - if (_database.TableExist(tableName)) + if (_schemaHelper.TableExist(tableName)) { - _database.DropTable(tableName); + _schemaHelper.DropTable(tableName); } } catch (Exception ex) { //swallow this for now, not sure how best to handle this with diff databases... though this is internal // and only used for unit tests. If this fails its because the table doesn't exist... generally! - LogHelper.Error("Could not drop table " + tableName, ex); + _logger.Error("Could not drop table " + tableName, ex); } } } - public DatabaseSchemaCreation(Database database) - { - _database = database; - } + /// /// Initialize the database by creating the umbraco db schema @@ -111,7 +127,7 @@ namespace Umbraco.Core.Persistence.Migrations.Initial { foreach (var item in OrderedTables.OrderBy(x => x.Key)) { - _database.CreateTable(false, item.Value); + _schemaHelper.CreateTable(false, item.Value); } } @@ -126,7 +142,7 @@ namespace Umbraco.Core.Persistence.Migrations.Initial var result = new DatabaseSchemaResult(); //get the db index defs - result.DbIndexDefinitions = SqlSyntaxContext.SqlSyntaxProvider.GetDefinedIndexes(_database) + result.DbIndexDefinitions = _sqlSyntaxProvider.GetDefinedIndexes(_database) .Select(x => new DbIndexDefinition() { TableName = x.Item1, @@ -157,11 +173,11 @@ namespace Umbraco.Core.Persistence.Migrations.Initial //MySql doesn't conform to the "normal" naming of constraints, so there is currently no point in doing these checks. //TODO: At a later point we do other checks for MySql, but ideally it should be necessary to do special checks for different providers. // ALso note that to get the constraints for MySql we have to open a connection which we currently have not. - if (SqlSyntaxContext.SqlSyntaxProvider is MySqlSyntaxProvider) + if (_sqlSyntaxProvider is MySqlSyntaxProvider) return; //Check constraints in configured database against constraints in schema - var constraintsInDatabase = SqlSyntaxContext.SqlSyntaxProvider.GetConstraintsPerColumn(_database).DistinctBy(x => x.Item3).ToList(); + var constraintsInDatabase = _sqlSyntaxProvider.GetConstraintsPerColumn(_database).DistinctBy(x => x.Item3).ToList(); var foreignKeysInDatabase = constraintsInDatabase.Where(x => x.Item3.InvariantStartsWith("FK_")).Select(x => x.Item3).ToList(); var primaryKeysInDatabase = constraintsInDatabase.Where(x => x.Item3.InvariantStartsWith("PK_")).Select(x => x.Item3).ToList(); var indexesInDatabase = constraintsInDatabase.Where(x => x.Item3.InvariantStartsWith("IX_")).Select(x => x.Item3).ToList(); @@ -244,7 +260,7 @@ namespace Umbraco.Core.Persistence.Migrations.Initial private void ValidateDbColumns(DatabaseSchemaResult result) { //Check columns in configured database against columns in schema - var columnsInDatabase = SqlSyntaxContext.SqlSyntaxProvider.GetColumnsInSchema(_database); + var columnsInDatabase = _sqlSyntaxProvider.GetColumnsInSchema(_database); var columnsPerTableInDatabase = columnsInDatabase.Select(x => string.Concat(x.TableName, ",", x.ColumnName)).ToList(); var columnsPerTableInSchema = result.TableDefinitions.SelectMany(x => x.Columns.Select(y => string.Concat(y.TableName, ",", y.Name))).ToList(); //Add valid and invalid column differences to the result object @@ -266,7 +282,7 @@ namespace Umbraco.Core.Persistence.Migrations.Initial private void ValidateDbTables(DatabaseSchemaResult result) { //Check tables in configured database against tables in schema - var tablesInDatabase = SqlSyntaxContext.SqlSyntaxProvider.GetTablesInSchema(_database).ToList(); + var tablesInDatabase = _sqlSyntaxProvider.GetTablesInSchema(_database).ToList(); var tablesInSchema = result.TableDefinitions.Select(x => x.Name).ToList(); //Add valid and invalid table differences to the result object var validTableDifferences = tablesInDatabase.Intersect(tablesInSchema, StringComparer.InvariantCultureIgnoreCase); diff --git a/src/Umbraco.Core/Persistence/Migrations/MigrationBase.cs b/src/Umbraco.Core/Persistence/Migrations/MigrationBase.cs index 642a920319..b1fc19fa67 100644 --- a/src/Umbraco.Core/Persistence/Migrations/MigrationBase.cs +++ b/src/Umbraco.Core/Persistence/Migrations/MigrationBase.cs @@ -1,4 +1,6 @@ -using Umbraco.Core.Persistence.Migrations.Syntax.Alter; +using System; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence.Migrations.Syntax.Alter; using Umbraco.Core.Persistence.Migrations.Syntax.Create; using Umbraco.Core.Persistence.Migrations.Syntax.Delete; using Umbraco.Core.Persistence.Migrations.Syntax.Execute; @@ -6,11 +8,27 @@ using Umbraco.Core.Persistence.Migrations.Syntax.IfDatabase; using Umbraco.Core.Persistence.Migrations.Syntax.Insert; using Umbraco.Core.Persistence.Migrations.Syntax.Rename; using Umbraco.Core.Persistence.Migrations.Syntax.Update; +using Umbraco.Core.Persistence.SqlSyntax; namespace Umbraco.Core.Persistence.Migrations { public abstract class MigrationBase : IMigration { + public ISqlSyntaxProvider SqlSyntax { get; private set; } + public ILogger Logger { get; private set; } + + [Obsolete("Use the other constructor specifying all dependencies instead")] + protected MigrationBase() + : this(SqlSyntaxContext.SqlSyntaxProvider, LoggerResolver.Current.Logger) + { + } + + protected MigrationBase(ISqlSyntaxProvider sqlSyntax, ILogger logger) + { + SqlSyntax = sqlSyntax; + Logger = logger; + } + internal IMigrationContext Context; public abstract void Up(); diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSixOneZero/CreateServerRegistryTable.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSixOneZero/CreateServerRegistryTable.cs index 5c39f92b21..b9ab37e4b7 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSixOneZero/CreateServerRegistryTable.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSixOneZero/CreateServerRegistryTable.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using Umbraco.Core.Configuration; using Umbraco.Core.Models.Rdbms; +using Umbraco.Core.Persistence.SqlSyntax; namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSixOneZero { @@ -13,8 +14,10 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSixOneZero { public override void Up() { + var schemaHelper = new DatabaseSchemaHelper(Context.Database, Logger, SqlSyntax); + //NOTE: This isn't the correct way to do this but to manually create this table with the Create syntax is a pain in the arse - Context.Database.CreateTable(); + schemaHelper.CreateTable(); } public override void Down() diff --git a/src/Umbraco.Core/Persistence/PetaPocoExtensions.cs b/src/Umbraco.Core/Persistence/PetaPocoExtensions.cs index 05ddc60b0a..72aa166a00 100644 --- a/src/Umbraco.Core/Persistence/PetaPocoExtensions.cs +++ b/src/Umbraco.Core/Persistence/PetaPocoExtensions.cs @@ -4,9 +4,6 @@ using System.Data; using System.Linq; using System.Text.RegularExpressions; 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; @@ -14,9 +11,7 @@ namespace Umbraco.Core.Persistence { public static class PetaPocoExtensions { - internal delegate void CreateTableEventHandler(string tableName, Database db, TableCreationEventArgs e); - - internal static event CreateTableEventHandler NewTable; + /// /// This will escape single @ symbols for peta poco values so it doesn't think it's a parameter @@ -35,18 +30,20 @@ namespace Umbraco.Core.Persistence } + [Obsolete("Use the DatabaseSchemaHelper instead")] public static void CreateTable(this Database db) - where T : new() + where T : new() { - var tableType = typeof(T); - CreateTable(db, false, tableType); + var creator = new DatabaseSchemaHelper(db, LoggerResolver.Current.Logger, SqlSyntaxContext.SqlSyntaxProvider); + creator.CreateTable(); } + [Obsolete("Use the DatabaseSchemaHelper instead")] public static void CreateTable(this Database db, bool overwrite) where T : new() { - var tableType = typeof(T); - CreateTable(db, overwrite, tableType); + var creator = new DatabaseSchemaHelper(db, LoggerResolver.Current.Logger, SqlSyntaxContext.SqlSyntaxProvider); + creator.CreateTable(overwrite); } public static void BulkInsertRecords(this Database db, IEnumerable collection) @@ -207,103 +204,26 @@ namespace Umbraco.Core.Persistence return commands.ToArray(); } + [Obsolete("Use the DatabaseSchemaHelper instead")] public static void CreateTable(this Database db, bool overwrite, Type modelType) { - var tableDefinition = DefinitionFactory.GetTableDefinition(modelType); - var tableName = tableDefinition.Name; - - string createSql = SqlSyntaxContext.SqlSyntaxProvider.Format(tableDefinition); - string createPrimaryKeySql = SqlSyntaxContext.SqlSyntaxProvider.FormatPrimaryKey(tableDefinition); - var foreignSql = SqlSyntaxContext.SqlSyntaxProvider.Format(tableDefinition.ForeignKeys); - var indexSql = SqlSyntaxContext.SqlSyntaxProvider.Format(tableDefinition.Indexes); - - var tableExist = db.TableExist(tableName); - if (overwrite && tableExist) - { - db.DropTable(tableName); - tableExist = false; - } - - if (tableExist == false) - { - using (var transaction = db.GetTransaction()) - { - //Execute the Create Table sql - int created = db.Execute(new Sql(createSql)); - LogHelper.Info(string.Format("Create Table sql {0}:\n {1}", created, createSql)); - - //If any statements exists for the primary key execute them here - if (!string.IsNullOrEmpty(createPrimaryKeySql)) - { - int createdPk = db.Execute(new Sql(createPrimaryKeySql)); - LogHelper.Info(string.Format("Primary Key sql {0}:\n {1}", createdPk, createPrimaryKeySql)); - } - - //Fires the NewTable event, which is used internally to insert base data before adding constrants to the schema - if (NewTable != null) - { - var e = new TableCreationEventArgs(); - - //Turn on identity insert if db provider is not mysql - if (SqlSyntaxContext.SqlSyntaxProvider.SupportsIdentityInsert() && tableDefinition.Columns.Any(x => x.IsIdentity)) - db.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", SqlSyntaxContext.SqlSyntaxProvider.GetQuotedTableName(tableName)))); - - //Call the NewTable-event to trigger the insert of base/default data - NewTable(tableName, db, e); - - //Turn off identity insert if db provider is not mysql - if (SqlSyntaxContext.SqlSyntaxProvider.SupportsIdentityInsert() && tableDefinition.Columns.Any(x => x.IsIdentity)) - db.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF;", SqlSyntaxContext.SqlSyntaxProvider.GetQuotedTableName(tableName)))); - - //Special case for MySql - if (SqlSyntaxContext.SqlSyntaxProvider is MySqlSyntaxProvider && tableName.Equals("umbracoUser")) - { - db.Update("SET id = @IdAfter WHERE id = @IdBefore AND userLogin = @Login", new { IdAfter = 0, IdBefore = 1, Login = "admin" }); - } - } - - //Loop through foreignkey statements and execute sql - foreach (var sql in foreignSql) - { - int createdFk = db.Execute(new Sql(sql)); - LogHelper.Info(string.Format("Create Foreign Key sql {0}:\n {1}", createdFk, sql)); - } - - //Loop through index statements and execute sql - foreach (var sql in indexSql) - { - int createdIndex = db.Execute(new Sql(sql)); - LogHelper.Info(string.Format("Create Index sql {0}:\n {1}", createdIndex, sql)); - } - - transaction.Complete(); - } - } - - LogHelper.Info(string.Format("New table '{0}' was created", tableName)); + var creator = new DatabaseSchemaHelper(db, LoggerResolver.Current.Logger, SqlSyntaxContext.SqlSyntaxProvider); + creator.CreateTable(overwrite, modelType); } + [Obsolete("Use the DatabaseSchemaHelper instead")] public static void DropTable(this Database db) where T : new() { - Type type = typeof(T); - var tableNameAttribute = type.FirstAttribute(); - if (tableNameAttribute == null) - throw new Exception( - string.Format( - "The Type '{0}' does not contain a TableNameAttribute, which is used to find the name of the table to drop. The operation could not be completed.", - type.Name)); - - string tableName = tableNameAttribute.Value; - DropTable(db, tableName); + var helper = new DatabaseSchemaHelper(db, LoggerResolver.Current.Logger, SqlSyntaxContext.SqlSyntaxProvider); + helper.DropTable(); } + [Obsolete("Use the DatabaseSchemaHelper instead")] public static void DropTable(this Database db, string tableName) { - var sql = new Sql(string.Format( - SqlSyntaxContext.SqlSyntaxProvider.DropTable, - SqlSyntaxContext.SqlSyntaxProvider.GetQuotedTableName(tableName))); - db.Execute(sql); + var helper = new DatabaseSchemaHelper(db, LoggerResolver.Current.Logger, SqlSyntaxContext.SqlSyntaxProvider); + helper.DropTable(tableName); } public static void TruncateTable(this Database db, string tableName) @@ -314,11 +234,13 @@ namespace Umbraco.Core.Persistence db.Execute(sql); } + [Obsolete("Use the DatabaseSchemaHelper instead")] public static bool TableExist(this Database db, string tableName) { return SqlSyntaxContext.SqlSyntaxProvider.DoesTableExist(db, tableName); } + [Obsolete("Use the DatabaseSchemaHelper instead")] public static bool TableExist(this UmbracoDatabase db, string tableName) { return SqlSyntaxContext.SqlSyntaxProvider.DoesTableExist(db, tableName); @@ -330,6 +252,7 @@ namespace Umbraco.Core.Persistence /// umbraco instances. /// /// Current PetaPoco object + [Obsolete("Use the DatabaseSchemaHelper instead")] public static void CreateDatabaseSchema(this Database db) { CreateDatabaseSchema(db, true); @@ -342,55 +265,20 @@ namespace Umbraco.Core.Persistence /// /// /// + [Obsolete("Use the DatabaseSchemaHelper instead")] public static void CreateDatabaseSchema(this Database db, bool guardConfiguration) { - if (guardConfiguration && ApplicationContext.Current.IsConfigured) - throw new Exception("Umbraco is already configured!"); - - CreateDatabaseSchemaDo(db); - } - - internal static void UninstallDatabaseSchema(this Database db) - { - var creation = new DatabaseSchemaCreation(db); - creation.UninstallDatabaseSchema(); - } - - internal static void CreateDatabaseSchemaDo(this Database db, bool guardConfiguration) - { - if (guardConfiguration && ApplicationContext.Current.IsConfigured) - throw new Exception("Umbraco is already configured!"); - - CreateDatabaseSchemaDo(db); - } - - internal static void CreateDatabaseSchemaDo(this Database db) - { - NewTable += PetaPocoExtensions_NewTable; - - LogHelper.Info("Initializing database schema creation"); - - var creation = new DatabaseSchemaCreation(db); - creation.InitializeDatabaseSchema(); - - LogHelper.Info("Finalized database schema creation"); - - NewTable -= PetaPocoExtensions_NewTable; + var helper = new DatabaseSchemaHelper(db, LoggerResolver.Current.Logger, SqlSyntaxContext.SqlSyntaxProvider); + helper.CreateDatabaseSchema(guardConfiguration, ApplicationContext.Current); } + //TODO: What the heck? This makes no sense at all public static DatabaseProviders GetDatabaseProvider(this Database db) { return ApplicationContext.Current.DatabaseContext.DatabaseProvider; } - private static void PetaPocoExtensions_NewTable(string tableName, Database db, TableCreationEventArgs e) - { - //argh! this is a hack because all these are extension methods and don't have an ILogger, we don't want to - // always need to think about the LoggerResolver being set (i.e. unit tests) so we'll check if it is otherwise - // create a temp logger - var baseDataCreation = new BaseDataCreation(db, LoggerResolver.HasCurrent ? LoggerResolver.Current.Logger: new DebugDiagnosticsLogger() ); - baseDataCreation.InitializeBaseData(tableName); - } + } internal class TableCreationEventArgs : System.ComponentModel.CancelEventArgs { } diff --git a/src/Umbraco.Core/Persistence/SqlSyntax/SqlSyntaxContext.cs b/src/Umbraco.Core/Persistence/SqlSyntax/SqlSyntaxContext.cs index 9fe69a4ca9..b50acc5026 100644 --- a/src/Umbraco.Core/Persistence/SqlSyntax/SqlSyntaxContext.cs +++ b/src/Umbraco.Core/Persistence/SqlSyntax/SqlSyntaxContext.cs @@ -5,6 +5,7 @@ namespace Umbraco.Core.Persistence.SqlSyntax /// /// Singleton to handle the configuration of a SqlSyntaxProvider /// + [Obsolete("This should not be used, the ISqlSyntaxProvider is part of the DatabaseContext or should be injected into your services as a constructor parameter")] public static class SqlSyntaxContext { private static ISqlSyntaxProvider _sqlSyntaxProvider; diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 489f537b5e..91c4b8e90f 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -319,6 +319,7 @@ + diff --git a/src/Umbraco.Tests/Migrations/Upgrades/ValidateOlderSchemaTest.cs b/src/Umbraco.Tests/Migrations/Upgrades/ValidateOlderSchemaTest.cs index 90fd108030..17794f7d29 100644 --- a/src/Umbraco.Tests/Migrations/Upgrades/ValidateOlderSchemaTest.cs +++ b/src/Umbraco.Tests/Migrations/Upgrades/ValidateOlderSchemaTest.cs @@ -7,6 +7,7 @@ using Moq; using NUnit.Framework; using SQLCE4Umbraco; using Umbraco.Core.Configuration; +using Umbraco.Core.Logging; using Umbraco.Core.ObjectResolution; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Migrations.Initial; @@ -26,7 +27,7 @@ namespace Umbraco.Tests.Migrations.Upgrades { // Arrange var db = GetConfiguredDatabase(); - var schema = new DatabaseSchemaCreation(db); + var schema = new DatabaseSchemaCreation(db, Mock.Of(), new SqlCeSyntaxProvider()); //Create db schema and data from old Total.sql file for Sql Ce string statements = GetDatabaseSpecificSqlScript(); diff --git a/src/Umbraco.Tests/Persistence/BaseTableByTableTest.cs b/src/Umbraco.Tests/Persistence/BaseTableByTableTest.cs index 702a49b92c..1934e85847 100644 --- a/src/Umbraco.Tests/Persistence/BaseTableByTableTest.cs +++ b/src/Umbraco.Tests/Persistence/BaseTableByTableTest.cs @@ -21,34 +21,41 @@ namespace Umbraco.Tests.Persistence [TestFixture] public abstract class BaseTableByTableTest { + private ILogger _logger; + private DatabaseSchemaHelper _schemaHelper; + [SetUp] public virtual void Initialize() { + _logger = new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config"))); + TestHelper.InitializeContentDirectories(); string path = TestHelper.CurrentAssemblyDirectory; AppDomain.CurrentDomain.SetData("DataDirectory", path); - - var logger = new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config"))); - + RepositoryResolver.Current = new RepositoryResolver(new RepositoryFactory(true, - logger, + _logger, Mock.Of())); //disable cache var cacheHelper = CacheHelper.CreateDisabledCacheHelper(); + var dbContext = new DatabaseContext(new DefaultDatabaseFactory()); + ApplicationContext.Current = new ApplicationContext( //assign the db context - new DatabaseContext(new DefaultDatabaseFactory()), + dbContext, //assign the service context - new ServiceContext(new PetaPocoUnitOfWorkProvider(), new FileUnitOfWorkProvider(), new PublishingStrategy(), cacheHelper, logger), + new ServiceContext(new PetaPocoUnitOfWorkProvider(), new FileUnitOfWorkProvider(), new PublishingStrategy(), cacheHelper, _logger), cacheHelper, new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config")))) { IsReady = true }; + _schemaHelper = new DatabaseSchemaHelper(dbContext.Database, _logger, new SqlCeSyntaxProvider()); + Resolution.Freeze(); } @@ -71,7 +78,7 @@ namespace Umbraco.Tests.Persistence { using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -82,7 +89,7 @@ namespace Umbraco.Tests.Persistence { using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -94,7 +101,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -106,8 +113,8 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -119,9 +126,9 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -133,8 +140,8 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -146,10 +153,10 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -161,10 +168,10 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -176,8 +183,8 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -189,9 +196,9 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -203,7 +210,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -215,8 +222,8 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -228,8 +235,8 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -241,11 +248,11 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -257,10 +264,10 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -272,8 +279,8 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -285,7 +292,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -297,7 +304,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -309,7 +316,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -321,10 +328,10 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -336,11 +343,11 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -352,9 +359,9 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -366,11 +373,11 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -382,12 +389,12 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -399,11 +406,11 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -415,9 +422,9 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -429,9 +436,9 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -443,7 +450,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -455,8 +462,8 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -468,8 +475,8 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -481,7 +488,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -493,15 +500,15 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -513,11 +520,11 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -529,7 +536,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -541,7 +548,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -553,8 +560,8 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -566,7 +573,7 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -578,9 +585,9 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -592,10 +599,10 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } @@ -607,10 +614,10 @@ namespace Umbraco.Tests.Persistence using (Transaction transaction = Database.GetTransaction()) { - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); - Database.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); + _schemaHelper.CreateTable(); //transaction.Complete(); } diff --git a/src/Umbraco.Tests/Persistence/SchemaValidationTest.cs b/src/Umbraco.Tests/Persistence/SchemaValidationTest.cs index 14581378f9..26b5b49efb 100644 --- a/src/Umbraco.Tests/Persistence/SchemaValidationTest.cs +++ b/src/Umbraco.Tests/Persistence/SchemaValidationTest.cs @@ -1,6 +1,9 @@ -using NUnit.Framework; +using Moq; +using NUnit.Framework; using Umbraco.Core.Configuration; +using Umbraco.Core.Logging; using Umbraco.Core.Persistence.Migrations.Initial; +using Umbraco.Core.Persistence.SqlSyntax; using Umbraco.Tests.TestHelpers; namespace Umbraco.Tests.Persistence @@ -26,7 +29,7 @@ namespace Umbraco.Tests.Persistence { // Arrange var db = DatabaseContext.Database; - var schema = new DatabaseSchemaCreation(db); + var schema = new DatabaseSchemaCreation(db, Logger, new SqlCeSyntaxProvider()); // Act var result = schema.ValidateSchema(); diff --git a/src/Umbraco.Tests/TestHelpers/BaseSeleniumTest.cs b/src/Umbraco.Tests/TestHelpers/BaseSeleniumTest.cs index 832dde1e70..b820ea3c0f 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseSeleniumTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseSeleniumTest.cs @@ -7,10 +7,12 @@ using System.Linq.Dynamic; using System.Runtime.InteropServices; using System.Text; using System.Web.Management; +using Moq; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using Umbraco.Core; +using Umbraco.Core.Logging; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Migrations.Initial; using Umbraco.Core.Persistence.SqlSyntax; @@ -53,12 +55,13 @@ namespace Umbraco.Tests.TestHelpers if (File.Exists(databaseDataPath) == false) engine.CreateDatabase(); - SqlSyntaxContext.SqlSyntaxProvider = new SqlCeSyntaxProvider(); + var syntaxProvider = new SqlCeSyntaxProvider(); + SqlSyntaxContext.SqlSyntaxProvider = syntaxProvider; _database = new UmbracoDatabase(connectionString, "System.Data.SqlServerCe.4.0"); // First remove anything in the database - var creation = new DatabaseSchemaCreation(_database); + var creation = new DatabaseSchemaCreation(_database, Mock.Of(), syntaxProvider); creation.UninstallDatabaseSchema(); // Then populate it with fresh data