Skip to content

Commit

Permalink
Make sure indexes are created as unique when using .Unique() syntax b…
Browse files Browse the repository at this point in the history
…uilder (#3319) (#3363)
  • Loading branch information
dawoe authored and nul800sebastiaan committed Oct 24, 2018
1 parent fa2bd54 commit 8262269
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ public static IndexDefinition GetIndexDefinition(Type modelType, PropertyInfo pr
Name = indexName,
IndexType = attribute.IndexType,
ColumnName = columnName,
TableName = tableName,
IsClustered = attribute.IndexType == IndexTypes.Clustered,
IsUnique = attribute.IndexType == IndexTypes.UniqueNonClustered
TableName = tableName,
};

if (string.IsNullOrEmpty(attribute.ForColumns) == false)
Expand All @@ -174,4 +172,4 @@ public static IndexDefinition GetIndexDefinition(Type modelType, PropertyInfo pr
return definition;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Umbraco.Core.Persistence.DatabaseAnnotations;

Expand All @@ -14,9 +15,13 @@ public IndexDefinition()
public virtual string SchemaName { get; set; }
public virtual string TableName { get; set; }
public virtual string ColumnName { get; set; }

[Obsolete("Use the IndexType property instead and set it to IndexTypes.UniqueNonClustered")]
public virtual bool IsUnique { get; set; }

[Obsolete("Use the IndexType property instead and set it to IndexTypes.Clustered")]
public bool IsClustered { get; set; }
public virtual ICollection<IndexColumnDefinition> Columns { get; set; }
public IndexTypes IndexType { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using Umbraco.Core.Persistence.DatabaseAnnotations;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Core.Persistence.Migrations.Syntax.Alter.Expressions;
using Umbraco.Core.Persistence.Migrations.Syntax.Expressions;
Expand Down Expand Up @@ -139,10 +140,12 @@ public IAlterColumnOptionSyntax Unique(string indexName)
{
Name = indexName,
SchemaName = Expression.SchemaName,
TableName = Expression.TableName,
IsUnique = true
TableName = Expression.TableName,
IndexType = IndexTypes.UniqueNonClustered
});



index.Index.Columns.Add(new IndexColumnDefinition
{
Name = Expression.Column.Name
Expand Down Expand Up @@ -242,4 +245,4 @@ public IAlterColumnOptionSyntax OnDeleteOrUpdate(Rule rule)
return this;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using Umbraco.Core.Persistence.DatabaseAnnotations;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Core.Persistence.Migrations.Syntax.Alter.Expressions;
using Umbraco.Core.Persistence.Migrations.Syntax.Expressions;
Expand Down Expand Up @@ -124,8 +125,8 @@ public IAlterTableColumnOptionSyntax Unique(string indexName)
{
Name = indexName,
SchemaName = Expression.SchemaName,
TableName = Expression.TableName,
IsUnique = true
TableName = Expression.TableName,
IndexType = IndexTypes.UniqueNonClustered
});

index.Index.Columns.Add(new IndexColumnDefinition
Expand Down Expand Up @@ -259,4 +260,4 @@ public IAlterTableColumnOptionSyntax OnDeleteOrUpdate(Rule rule)
return this;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using Umbraco.Core.Persistence.DatabaseAnnotations;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Core.Persistence.Migrations.Syntax.Expressions;
using Umbraco.Core.Persistence.SqlSyntax;
Expand Down Expand Up @@ -114,8 +115,8 @@ public ICreateColumnOptionSyntax Unique(string indexName)
{
Name = indexName,
SchemaName = Expression.SchemaName,
TableName = Expression.TableName,
IsUnique = true
TableName = Expression.TableName,
IndexType = IndexTypes.UniqueNonClustered
});

index.Index.Columns.Add(new IndexColumnDefinition
Expand Down Expand Up @@ -217,4 +218,4 @@ public ICreateColumnOptionSyntax OnDeleteOrUpdate(Rule rule)
return this;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,27 @@ public ICreateIndexOnColumnSyntax Descending()
}

ICreateIndexOnColumnSyntax ICreateIndexColumnOptionsSyntax.Unique()
{
Expression.Index.IsUnique = true;
//if it is Unique then it must be unique nonclustered and set the other flags
Expression.Index.IndexType = IndexTypes.UniqueNonClustered;
Expression.Index.IsClustered = false;
{
Expression.Index.IndexType = IndexTypes.UniqueNonClustered;
return this;
}

public ICreateIndexOnColumnSyntax NonClustered()
{
Expression.Index.IndexType = IndexTypes.NonClustered;
Expression.Index.IsClustered = false;
Expression.Index.IndexType = IndexTypes.NonClustered;
Expression.Index.IsUnique = false;
Expression.Index.IndexType = IndexTypes.NonClustered;
return this;
}

public ICreateIndexOnColumnSyntax Clustered()
{
Expression.Index.IndexType = IndexTypes.Clustered;
Expression.Index.IsClustered = true;
//if it is clustered then we have to change the index type set the other flags
Expression.Index.IndexType = IndexTypes.Clustered;
Expression.Index.IsClustered = true;
Expression.Index.IsUnique = false;
return this;
{
Expression.Index.IndexType = IndexTypes.Clustered;
return this;
}

ICreateIndexOnColumnSyntax ICreateIndexOptionsSyntax.Unique()
{
Expression.Index.IndexType = IndexTypes.UniqueNonClustered;
Expression.Index.IsUnique = true;
Expression.Index.IndexType = IndexTypes.UniqueNonClustered;
return this;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using Umbraco.Core.Persistence.DatabaseAnnotations;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Core.Persistence.Migrations.Syntax.Create.Expressions;
using Umbraco.Core.Persistence.Migrations.Syntax.Expressions;
Expand Down Expand Up @@ -164,8 +165,8 @@ public ICreateTableColumnOptionSyntax Unique(string indexName)
{
Name = indexName,
SchemaName = Expression.SchemaName,
TableName = Expression.TableName,
IsUnique = true
TableName = Expression.TableName,
IndexType = IndexTypes.UniqueNonClustered
});

index.Index.Columns.Add(new IndexColumnDefinition
Expand Down Expand Up @@ -267,4 +268,4 @@ public ICreateTableColumnOptionSyntax OnDeleteOrUpdate(Rule rule)
return this;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void Format_SqlServer_NonClusteredIndexDefinition_UsingIsClusteredFalse_A
var sqlSyntax = new SqlServerSyntaxProvider();

var indexDefinition = CreateIndexDefinition();
indexDefinition.IsClustered = false;
indexDefinition.IndexType = IndexTypes.Clustered;

var actual = sqlSyntax.Format(indexDefinition);
Assert.AreEqual("CREATE CLUSTERED INDEX [IX_A] ON [TheTable] ([A])", actual);
Expand Down Expand Up @@ -159,4 +159,4 @@ private static IndexDefinition CreateIndexDefinition()
}

}
}
}

0 comments on commit 8262269

Please sign in to comment.