Skip to content

Commit

Permalink
Use include when create index
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 13, 2021
1 parent fff950c commit c01dea8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 3 additions & 1 deletion SqlSchemaCompare.Core/TSql/Factory/TSqlIndexFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ public class TSqlIndexFactory : FactoryBase, IFactory
public DbObject Create(ParserRuleContext context, ICharStream stream)
{
var indexContext = context as TSqlParser.Create_indexContext;
var columnsInclude = indexContext.column_name_list() != null ? indexContext.column_name_list().id_().Select(x => x.GetText()) : Enumerable.Empty<string>();
return new Index
{
Sql = stream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)),
Name = indexContext.id_()[0].GetText(),
Schema = string.Empty,
Operation = GetOperation(indexContext.GetChild(0).GetText()),
ParentName = indexContext.table_name().GetText(),
ColumnNames = indexContext.column_name_list_with_order().id_().Select(x => x.GetText()),
ColumnNames = indexContext.column_name_list_with_order().id_().Select(x => x.GetText())
.Union(columnsInclude)
};
}
}
Expand Down
18 changes: 12 additions & 6 deletions SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,22 @@ public override void ExitCreate_type([NotNull] TSqlParser.Create_typeContext con

public override void ExitCreate_index([NotNull] TSqlParser.Create_indexContext context)
{
var index = _indexFactory.Create(context, _stream);
var table = DbObjects.OfType<Table>().Single(x => x.Identifier == index.ParentName);
table.AddIndex(index as DbStructures.Index);
if (!ObjectInsideDDL(context))
{
var index = _indexFactory.Create(context, _stream);
var table = DbObjects.OfType<Table>().Single(x => x.Identifier == index.ParentName);
table.AddIndex(index as DbStructures.Index);
}
}

public override void ExitAlter_table([NotNull] TSqlParser.Alter_tableContext context)
{
var constraint = _tableFactory.CreateAlterTable(context);
var table = DbObjects.OfType<Table>().Single(x => x.Identifier == constraint.ParentName);
table.AddConstraint(constraint);
if (!ObjectInsideDDL(context))
{
var constraint = _tableFactory.CreateAlterTable(context);
var table = DbObjects.OfType<Table>().Single(x => x.Identifier == constraint.ParentName);
table.AddConstraint(constraint);
}
}

public override void ExitCreate_nonclustered_columnstore_index([NotNull] Create_nonclustered_columnstore_indexContext context)
Expand Down
9 changes: 6 additions & 3 deletions SqlSchemaCompare.Test/TSql/TSqlIndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public TSqlIndexTest()
public void CreateIndex()
{
const string sql =
@"CREATE TABLE [dbo].[table] ([ID] [INT] NOT NULL)
@"CREATE TABLE [dbo].[table] ([ID] [INT] NOT NULL, [col1] int )
GO
CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [indexTable]
) INCLUDE ([col1]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [indexTable]
GO";

var objectFactory = new TSqlObjectFactory();
Expand All @@ -36,11 +36,14 @@ [ID] ASC
table.Indexes.Single().Name.ShouldBe("[indexName]");
table.Indexes.Single().Schema.ShouldBeEmpty();
table.Indexes.Single().Identifier.ShouldBe("[indexName]");
table.Indexes.Single().ColumnNames.ShouldContain("[ID]");
table.Indexes.Single().ColumnNames.ShouldContain("[col1]");
table.Indexes.Single().ColumnNames.Count().ShouldBe(2);
table.Indexes.Single().Sql.ShouldBe(
@"CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table]
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [indexTable]");
) INCLUDE ([col1]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [indexTable]");
errors.Count().ShouldBe(0);
}

Expand Down
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Test/TSql/TSqlTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ REFERENCES [dbo].[tblLookup] ([ID])
");
errors.ShouldBeEmpty();
}

[Fact]
public void DropIndexBeforeAlterColumn()
{
Expand Down

0 comments on commit c01dea8

Please sign in to comment.