Ensures not to add new indexes if they already exist

This commit is contained in:
Shannon
2014-03-12 18:37:22 +11:00
parent d0fc3ddba5
commit aee10348e5
8 changed files with 49 additions and 7 deletions

View File

@@ -0,0 +1,13 @@
namespace Umbraco.Core.Persistence.DatabaseModelDefinitions
{
/// <summary>
/// Represents a database index definition retreived by querying the database
/// </summary>
internal class DbIndexDefinition
{
public virtual string IndexName { get; set; }
public virtual string TableName { get; set; }
public virtual string ColumnName { get; set; }
public virtual bool IsUnique { get; set; }
}
}

View File

@@ -125,6 +125,16 @@ namespace Umbraco.Core.Persistence.Migrations.Initial
{
var result = new DatabaseSchemaResult();
//get the db index defs
result.DbIndexDefinitions = SqlSyntaxContext.SqlSyntaxProvider.GetDefinedIndexes(_database)
.Select(x => new DbIndexDefinition()
{
TableName = x.Item1,
IndexName = x.Item2,
ColumnName = x.Item3,
IsUnique = x.Item4
}).ToArray();
foreach (var item in OrderedTables.OrderBy(x => x.Key))
{
var tableDefinition = DefinitionFactory.GetTableDefinition(item.Value);

View File

@@ -29,6 +29,8 @@ namespace Umbraco.Core.Persistence.Migrations.Initial
public List<string> ValidConstraints { get; set; }
internal IEnumerable<DbIndexDefinition> DbIndexDefinitions { get; set; }
/// <summary>
/// Determines the version of the currently installed database.
/// </summary>

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Umbraco.Core.Configuration;
using Umbraco.Core.Persistence.Migrations.Initial;
@@ -12,10 +13,24 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSixTwoZero
var dbSchema = new DatabaseSchemaCreation(Context.Database);
var schemaResult = dbSchema.ValidateSchema();
Create.Index("IX_umbracoNodeTrashed").OnTable("umbracoNode").OnColumn("trashed").Ascending().WithOptions().NonClustered();
Create.Index("IX_cmsContentVersion_ContentId").OnTable("cmsContentVersion").OnColumn("ContentId").Ascending().WithOptions().NonClustered();
Create.Index("IX_cmsDocument_published").OnTable("cmsDocument").OnColumn("published").Ascending().WithOptions().NonClustered();
Create.Index("IX_cmsDocument_newest").OnTable("cmsDocument").OnColumn("newest").Ascending().WithOptions().NonClustered();
//do not create any indexes if they already exist in the database
if (schemaResult.DbIndexDefinitions.Any(x => x.IndexName == "IX_umbracoNodeTrashed") == false)
{
Create.Index("IX_umbracoNodeTrashed").OnTable("umbracoNode").OnColumn("trashed").Ascending().WithOptions().NonClustered();
}
if (schemaResult.DbIndexDefinitions.Any(x => x.IndexName == "IX_cmsContentVersion_ContentId") == false)
{
Create.Index("IX_cmsContentVersion_ContentId").OnTable("cmsContentVersion").OnColumn("ContentId").Ascending().WithOptions().NonClustered();
}
if (schemaResult.DbIndexDefinitions.Any(x => x.IndexName == "IX_cmsDocument_published") == false)
{
Create.Index("IX_cmsDocument_published").OnTable("cmsDocument").OnColumn("published").Ascending().WithOptions().NonClustered();
}
if (schemaResult.DbIndexDefinitions.Any(x => x.IndexName == "IX_cmsDocument_newest") == false)
{
Create.Index("IX_cmsDocument_newest").OnTable("cmsDocument").OnColumn("newest").Ascending().WithOptions().NonClustered();
}
}
public override void Down()

View File

@@ -130,7 +130,7 @@ ORDER BY TABLE_NAME, INDEX_NAME",
list =
indexes.Select(
item =>
new Tuple<string, string, string, bool>(item.TABLE_NAME, item.INDEX_NAME, item.COLUMN_NAME, item.UNIQUE))
new Tuple<string, string, string, bool>(item.TABLE_NAME, item.INDEX_NAME, item.COLUMN_NAME, item.UNIQUE == 1))
.ToList();
}
finally

View File

@@ -233,7 +233,7 @@ namespace Umbraco.Core.Persistence.SqlSyntax
"SELECT TABLE_NAME, INDEX_NAME, COLUMN_NAME, [UNIQUE] FROM INFORMATION_SCHEMA.INDEXES ORDER BY TABLE_NAME, INDEX_NAME");
return
items.Select(
item => new Tuple<string, string, string, bool>(item.TABLE_NAME, item.INDEX_NAME, item.COLUMN_NAME, item.UNIQUE));
item => new Tuple<string, string, string, bool>(item.TABLE_NAME, item.INDEX_NAME, item.COLUMN_NAME, item.UNIQUE == 1));
}
public override bool DoesTableExist(Database db, string tableName)

View File

@@ -161,7 +161,8 @@ from sys.tables as T inner join sys.indexes as I on T.[object_id] = I.[object_id
inner join sys.index_columns as IC on IC.[object_id] = I.[object_id] and IC.[index_id] = I.[index_id]
inner join sys.all_columns as AC on IC.[object_id] = AC.[object_id] and IC.[column_id] = AC.[column_id]
order by T.name, I.name");
return items.Select(item => new Tuple<string, string, string, bool>(item.TABLE_NAME, item.INDEX_NAME, item.COLUMN_NAME, item.UNIQUE)).ToList();
return items.Select(item => new Tuple<string, string, string, bool>(item.TABLE_NAME, item.INDEX_NAME, item.COLUMN_NAME,
item.UNIQUE == 1)).ToList();
}

View File

@@ -211,6 +211,7 @@
<Compile Include="Models\PublishedContent\PublishedContentModel.cs" />
<Compile Include="Models\PublishedContent\PublishedContentModelFactoryResolver.cs" />
<Compile Include="Models\TemplateNode.cs" />
<Compile Include="Persistence\DatabaseModelDefinitions\DbIndexDefinition.cs" />
<Compile Include="Persistence\Factories\MemberGroupFactory.cs" />
<Compile Include="Persistence\Mappers\MemberGroupMapper.cs" />
<Compile Include="Persistence\Querying\SqlStringExtensions.cs" />