Skip to content

Commit

Permalink
Support for column store indexes
Browse files Browse the repository at this point in the history
sethreno committed Feb 5, 2017
1 parent e6c61e9 commit 5be7540
Showing 4 changed files with 80 additions and 4 deletions.
7 changes: 5 additions & 2 deletions model/Models/Constraint.cs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
namespace SchemaZen.Library.Models {
public class Constraint : INameable, IScriptable {
public bool Clustered { get; set; }
public bool ColumnStore { get; set; }
public List<ConstraintColumn> Columns { get; set; } = new List<ConstraintColumn>();
public List<string> IncludedColumns { get; set; } = new List<string>();
public string Name { get; set; }
@@ -30,17 +31,19 @@ public static Constraint CreateCheckedConstraint(string name, bool isNotForRepli
return constraint;
}

public string ColumnStoreText => !ColumnStore ? "" : " COLUMNSTORE";

public string ClusteredText => !Clustered ? "NONCLUSTERED" : "CLUSTERED";

public string UniqueText => Type != "PRIMARY KEY" && !Unique ? "" : "UNIQUE";
public string UniqueText => Type != " PRIMARY KEY" && !Unique ? "" : " UNIQUE";

public string ScriptCreate() {
switch (Type) {
case "CHECK":
var notForReplicationOption = _isNotForReplication ? "NOT FOR REPLICATION" : "";
return $"CONSTRAINT [{Name}] CHECK {notForReplicationOption} {_checkConstraintExpression}";
case "INDEX":
var sql = $"CREATE {UniqueText} {ClusteredText} INDEX [{Name}] ON [{Table.Owner}].[{Table.Name}] ({string.Join(", ", Columns.Select(c => c.Script()).ToArray())})";
var sql = $"CREATE{UniqueText} {ClusteredText}{ColumnStoreText} INDEX [{Name}] ON [{Table.Owner}].[{Table.Name}] ({string.Join(", ", Columns.Select(c => c.Script()).ToArray())})";
if (IncludedColumns.Count > 0) {
sql += $" INCLUDE ([{string.Join("], [", IncludedColumns.ToArray())}])";
}
72 changes: 72 additions & 0 deletions test/ConstraintTester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using NUnit.Framework;
using SchemaZen.Library.Models;

namespace SchemaZen.Tests {

[TestFixture]
public class ConstraintTester {

[TestFixture]
public class ScriptCreate {

private static Constraint SetUp() {
return new Constraint("test", "INDEX", "a,b") {
Table = new Table("dbo", "test")
};
}

[Test]
public void clustered_index() {
var c = SetUp();
c.Clustered = true;
Assert.AreEqual(
"CREATE CLUSTERED INDEX [test] ON [dbo].[test] ([a], [b])",
c.ScriptCreate());
}

[Test]
public void nonclustered_index() {
var c = SetUp();
c.Clustered = false;
Assert.AreEqual(
"CREATE NONCLUSTERED INDEX [test] ON [dbo].[test] ([a], [b])",
c.ScriptCreate());
}

[Test]
public void columnstore_index() {
var c = SetUp();
c.ColumnStore = true;
Assert.AreEqual(
"CREATE NONCLUSTERED COLUMNSTORE INDEX [test] ON [dbo].[test] ([a], [b])",
c.ScriptCreate());
}

[Test]
public void primary_key() {
var c = SetUp();
c.Type = "PRIMARY KEY";
Assert.AreEqual(
"CONSTRAINT [test] PRIMARY KEY NONCLUSTERED ([a], [b])",
c.ScriptCreate());
}

[Test]
public void foreign_key() {
var c = SetUp();
c.Type = "FOREIGN KEY";
Assert.AreEqual(
"CONSTRAINT [test] FOREIGN KEY NONCLUSTERED ([a], [b])",
c.ScriptCreate());
}

[Test]
public void check_constraint() {
var c = Constraint.CreateCheckedConstraint("test", true, "[a]>(1)");
Assert.AreEqual(
"CONSTRAINT [test] CHECK NOT FOR REPLICATION [a]>(1)",
c.ScriptCreate());
}
}
}
}
4 changes: 2 additions & 2 deletions test/DatabaseTester.cs
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ public void TestDescIndex() {
};
db.Load();
var result = db.ScriptCreate();
Assert.That(result, Is.StringContaining("CREATE NONCLUSTERED INDEX [MyIndex] ON [dbo].[MyTable] ([Id] DESC)"));
Assert.That(result, Is.StringContaining("CREATE NONCLUSTERED INDEX [MyIndex] ON [dbo].[MyTable] ([Id] DESC)"));

TestHelper.DropDb("test");
}
@@ -123,7 +123,7 @@ public void TestTableIndexesWithFilter() {
var result = db.ScriptCreate();
TestHelper.DropDb("TEST");

Assert.That(result, Is.StringContaining("CREATE NONCLUSTERED INDEX [MyIndex] ON [dbo].[MyTable] ([Id]) WHERE ([EndDate] IS NULL)"));
Assert.That(result, Is.StringContaining("CREATE NONCLUSTERED INDEX [MyIndex] ON [dbo].[MyTable] ([Id]) WHERE ([EndDate] IS NULL)"));
}

[Test]
1 change: 1 addition & 0 deletions test/Tests.csproj
Original file line number Diff line number Diff line change
@@ -76,6 +76,7 @@
<ItemGroup>
<Compile Include="AssemblyTester.cs" />
<Compile Include="ColumnTester.cs" />
<Compile Include="ConstraintTester.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UserTester.cs" />
</ItemGroup>

0 comments on commit 5be7540

Please sign in to comment.