Refactoring DatabaseSchemaCreation to allow for schema validation.
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Events;
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Migrations.Initial
|
||||
{
|
||||
@@ -7,13 +11,110 @@ namespace Umbraco.Core.Persistence.Migrations.Initial
|
||||
/// </summary>
|
||||
internal class DatabaseSchemaCreation
|
||||
{
|
||||
#region Private Members
|
||||
private readonly Database _database;
|
||||
private static readonly Dictionary<int, Type> OrderedTables = new Dictionary<int, Type>
|
||||
{
|
||||
{0, typeof (NodeDto)},
|
||||
{1, typeof (TemplateDto)},
|
||||
{2, typeof (ContentDto)},
|
||||
{3, typeof (ContentVersionDto)},
|
||||
{4, typeof (DocumentDto)},
|
||||
{5, typeof (ContentTypeDto)},
|
||||
{6, typeof (DocumentTypeDto)},
|
||||
{7, typeof (DataTypeDto)},
|
||||
{8, typeof (DataTypePreValueDto)},
|
||||
{9, typeof (DictionaryDto)},
|
||||
{10, typeof (LanguageTextDto)},
|
||||
{11, typeof (LanguageDto)},
|
||||
{12, typeof (DomainDto)},
|
||||
{13, typeof (LogDto)},
|
||||
{14, typeof (MacroDto)},
|
||||
{15, typeof (MacroPropertyTypeDto)},
|
||||
{16, typeof (MacroPropertyDto)},
|
||||
{17, typeof (MemberTypeDto)},
|
||||
{18, typeof (MemberDto)},
|
||||
{19, typeof (Member2MemberGroupDto)},
|
||||
{20, typeof (ContentXmlDto)},
|
||||
{21, typeof (PreviewXmlDto)},
|
||||
{22, typeof (PropertyTypeGroupDto)},
|
||||
{23, typeof (PropertyTypeDto)},
|
||||
{24, typeof (PropertyDataDto)},
|
||||
{25, typeof (RelationTypeDto)},
|
||||
{26, typeof (RelationDto)},
|
||||
{27, typeof (StylesheetDto)},
|
||||
{28, typeof (StylesheetPropertyDto)},
|
||||
{29, typeof (TagDto)},
|
||||
{30, typeof (TagRelationshipDto)},
|
||||
{31, typeof (UserLoginDto)},
|
||||
{32, typeof (UserTypeDto)},
|
||||
{33, typeof (UserDto)},
|
||||
{34, typeof (TaskTypeDto)},
|
||||
{35, typeof (TaskDto)},
|
||||
{36, typeof (ContentType2ContentTypeDto)},
|
||||
{
|
||||
37,
|
||||
typeof (ContentTypeAllowedContentTypeDto)
|
||||
},
|
||||
{38, typeof (User2AppDto)},
|
||||
{39, typeof (User2NodeNotifyDto)},
|
||||
{40, typeof (User2NodePermissionDto)}
|
||||
};
|
||||
#endregion
|
||||
|
||||
public DatabaseSchemaCreation(Database database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the database by creating the umbraco db schema
|
||||
/// </summary>
|
||||
public void InitializeDatabaseSchema()
|
||||
{
|
||||
var e = new DatabaseCreationEventArgs();
|
||||
FireBeforeCreation(e);
|
||||
|
||||
if (!e.Cancel)
|
||||
{
|
||||
foreach (var item in OrderedTables.OrderBy(x => x.Key))
|
||||
{
|
||||
_database.CreateTable(false, item.Value);
|
||||
}
|
||||
}
|
||||
|
||||
FireAfterCreation(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates the schema of the current database
|
||||
/// </summary>
|
||||
public DatabaseSchemaResult ValidateSchema()
|
||||
{
|
||||
var result = new DatabaseSchemaResult();
|
||||
|
||||
foreach (var item in OrderedTables.OrderBy(x => x.Key))
|
||||
{
|
||||
var tableNameAttribute = item.Value.FirstAttribute<TableNameAttribute>();
|
||||
if (tableNameAttribute != null)
|
||||
{
|
||||
var tableExist = _database.TableExist(tableNameAttribute.Value);
|
||||
if (tableExist)
|
||||
{
|
||||
result.Successes.Add(tableNameAttribute.Value, "Table exists");
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Errors.Add(tableNameAttribute.Value, "Table does not exist");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// The save event handler
|
||||
/// </summary>
|
||||
@@ -51,83 +152,6 @@ namespace Umbraco.Core.Persistence.Migrations.Initial
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the database by creating the umbraco db schema
|
||||
/// </summary>
|
||||
public void InitializeDatabaseSchema()
|
||||
{
|
||||
var e = new DatabaseCreationEventArgs();
|
||||
FireBeforeCreation(e);
|
||||
|
||||
if (!e.Cancel)
|
||||
{
|
||||
_database.CreateTable<NodeDto>();
|
||||
|
||||
_database.CreateTable<TemplateDto>();
|
||||
|
||||
_database.CreateTable<ContentDto>();
|
||||
_database.CreateTable<ContentVersionDto>();
|
||||
_database.CreateTable<DocumentDto>();
|
||||
|
||||
_database.CreateTable<ContentTypeDto>();
|
||||
_database.CreateTable<DocumentTypeDto>();
|
||||
|
||||
//_database.CreateTable<AppDto>();
|
||||
//_database.CreateTable<AppTreeDto>();
|
||||
|
||||
_database.CreateTable<DataTypeDto>();
|
||||
_database.CreateTable<DataTypePreValueDto>();
|
||||
|
||||
_database.CreateTable<DictionaryDto>();
|
||||
_database.CreateTable<LanguageTextDto>();
|
||||
|
||||
_database.CreateTable<LanguageDto>();
|
||||
_database.CreateTable<DomainDto>();
|
||||
|
||||
_database.CreateTable<LogDto>();
|
||||
|
||||
_database.CreateTable<MacroDto>();
|
||||
_database.CreateTable<MacroPropertyTypeDto>();
|
||||
_database.CreateTable<MacroPropertyDto>();
|
||||
|
||||
_database.CreateTable<MemberTypeDto>();
|
||||
_database.CreateTable<MemberDto>();
|
||||
_database.CreateTable<Member2MemberGroupDto>();
|
||||
|
||||
_database.CreateTable<ContentXmlDto>();
|
||||
_database.CreateTable<PreviewXmlDto>();
|
||||
|
||||
_database.CreateTable<PropertyTypeGroupDto>();
|
||||
_database.CreateTable<PropertyTypeDto>();
|
||||
_database.CreateTable<PropertyDataDto>();
|
||||
|
||||
_database.CreateTable<RelationTypeDto>();
|
||||
_database.CreateTable<RelationDto>();
|
||||
|
||||
_database.CreateTable<StylesheetDto>();
|
||||
_database.CreateTable<StylesheetPropertyDto>();
|
||||
|
||||
_database.CreateTable<TagDto>();
|
||||
_database.CreateTable<TagRelationshipDto>();
|
||||
|
||||
_database.CreateTable<UserLoginDto>();
|
||||
_database.CreateTable<UserTypeDto>();
|
||||
_database.CreateTable<UserDto>();
|
||||
|
||||
_database.CreateTable<TaskTypeDto>();
|
||||
_database.CreateTable<TaskDto>();
|
||||
|
||||
_database.CreateTable<ContentType2ContentTypeDto>();
|
||||
_database.CreateTable<ContentTypeAllowedContentTypeDto>();
|
||||
|
||||
_database.CreateTable<User2AppDto>();
|
||||
_database.CreateTable<User2NodeNotifyDto>();
|
||||
_database.CreateTable<User2NodePermissionDto>();
|
||||
}
|
||||
|
||||
FireAfterCreation(e);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
internal class DatabaseCreationEventArgs : System.ComponentModel.CancelEventArgs{}
|
||||
}
|
||||
@@ -123,6 +123,7 @@
|
||||
<Compile Include="Events\CancellableEventArgs.cs" />
|
||||
<Compile Include="Events\ContentCacheEventArgs.cs" />
|
||||
<Compile Include="Events\CopyEventArgs.cs" />
|
||||
<Compile Include="Events\DatabaseCreationEventArgs.cs" />
|
||||
<Compile Include="Events\DeleteEventArgs.cs" />
|
||||
<Compile Include="Events\CancellableObjectEventArgs.cs" />
|
||||
<Compile Include="Events\DeleteRevisionsEventArgs.cs" />
|
||||
@@ -265,6 +266,7 @@
|
||||
<Compile Include="Persistence\Migrations\IMigrationExpression.cs" />
|
||||
<Compile Include="Persistence\Migrations\Initial\BaseDataCreation.cs" />
|
||||
<Compile Include="Persistence\Migrations\Initial\DatabaseSchemaCreation.cs" />
|
||||
<Compile Include="Persistence\Migrations\Initial\DatabaseSchemaResult.cs" />
|
||||
<Compile Include="Persistence\Migrations\MigrationAttribute.cs" />
|
||||
<Compile Include="Persistence\Migrations\MigrationBase.cs" />
|
||||
<Compile Include="Persistence\Migrations\MigrationContext.cs" />
|
||||
|
||||
Reference in New Issue
Block a user