Skip to content

Commit

Permalink
alter index management
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 15, 2021
1 parent c01dea8 commit 69502aa
Show file tree
Hide file tree
Showing 13 changed files with 16,354 additions and 16,105 deletions.
4 changes: 0 additions & 4 deletions SqlSchemaCompare.Core/Common/ErrorListener.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Atn;
using Antlr4.Runtime.Dfa;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
using System;
using System.Collections;
using System.Collections.Generic;

namespace SqlSchemaCompare.Core.Common
Expand Down
8 changes: 7 additions & 1 deletion SqlSchemaCompare.Core/DbStructures/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ public class TablePrimaryKeyConstraint : TableConstraint
public void AdColumns(Column Colum) => Columns.Add(Colum);
public void AddConstraint(TableConstraint tableConstraint) => Constraints.Add(tableConstraint);
public void AddIndex(Index index) => Indexes.Add(index);
public bool PrimaryKeyDefinedInsideCreateTable { get; set; }
}
}

public abstract class TableConstraint : DbObject
{
public Table Table { get; init; }
public Table Table { get; set; }
public IEnumerable<string> ColumnNames { get; init; }

public void SetTable(Table table)
{
Table = table;
}
}
13 changes: 13 additions & 0 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlIndexFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,18 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
.Union(columnsInclude)
};
}

public DbObject CreateAlter(ParserRuleContext context, ICharStream stream)
{
var indexContext = context as TSqlParser.Alter_indexContext;
return new Index
{
Sql = stream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)),
Name = indexContext.id_().GetText(),
Schema = string.Empty,
Operation = GetOperation(indexContext.GetChild(0).GetText()),
ParentName = indexContext.table_name().GetText()
};
}
}
}
1 change: 1 addition & 0 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
else if (columnTree.table_constraint() != null)
{
table.AddConstraint(CreatePrimaryKeyConstraint(columnTree.table_constraint(), stream, table));
table.PrimaryKeyDefinedInsideCreateTable = true;
}
}
return table;
Expand Down
11 changes: 11 additions & 0 deletions SqlSchemaCompare.Core/TSql/ITSqlParserListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3043,6 +3043,17 @@ public interface ITSqlParserListener : IParseTreeListener {
/// <param name="context">The parse tree.</param>
void ExitCreate_index([NotNull] TSqlParser.Create_indexContext context);

/// <summary>
/// Enter a parse tree produced by <see cref="TSqlParser.alter_index"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterAlter_index([NotNull] TSqlParser.Alter_indexContext context);
/// <summary>
/// Exit a parse tree produced by <see cref="TSqlParser.alter_index"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitAlter_index([NotNull] TSqlParser.Alter_indexContext context);

/// <summary>
/// Enter a parse tree produced by <see cref="TSqlParser.create_columnstore_index"/>.
/// </summary>
Expand Down
32,323 changes: 16,239 additions & 16,084 deletions SqlSchemaCompare.Core/TSql/TSqlParser.cs

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions SqlSchemaCompare.Core/TSql/TSqlParserBaseListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3583,6 +3583,19 @@ public virtual void EnterCreate_index([NotNull] TSqlParser.Create_indexContext c
/// <param name="context">The parse tree.</param>
public virtual void ExitCreate_index([NotNull] TSqlParser.Create_indexContext context) { }

/// <summary>
/// Enter a parse tree produced by <see cref="TSqlParser.alter_index"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void EnterAlter_index([NotNull] TSqlParser.Alter_indexContext context) { }
/// <summary>
/// Exit a parse tree produced by <see cref="TSqlParser.alter_index"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void ExitAlter_index([NotNull] TSqlParser.Alter_indexContext context) { }

/// <summary>
/// Enter a parse tree produced by <see cref="TSqlParser.create_columnstore_index"/>.
/// <para>The default implementation does nothing.</para>
Expand Down
16 changes: 16 additions & 0 deletions SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ public override void ExitEnable_trigger([NotNull] TSqlParser.Enable_triggerConte
var enabled = _triggerFactory.CreateEnable(context, _stream);
var trigger = DbObjects.OfType<Trigger>().Single(x => x.Name == enabled.Name);
trigger.SetEnabled(enabled);
DbObjects.Add(enabled);
}

public override void ExitDisable_trigger([NotNull] TSqlParser.Disable_triggerContext context)
{
var enabled = _triggerFactory.CreateDisable(context, _stream);
var trigger = DbObjects.OfType<Trigger>().Single(x => x.Name == enabled.Name);
trigger.SetEnabled(enabled);
DbObjects.Add(enabled);
}
public override void ExitAlter_db_role([NotNull] TSqlParser.Alter_db_roleContext context)
{
Expand All @@ -126,6 +128,18 @@ public override void ExitCreate_index([NotNull] TSqlParser.Create_indexContext c
var index = _indexFactory.Create(context, _stream);
var table = DbObjects.OfType<Table>().Single(x => x.Identifier == index.ParentName);
table.AddIndex(index as DbStructures.Index);
DbObjects.Add(index);
}
}

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

Expand All @@ -136,6 +150,8 @@ public override void ExitAlter_table([NotNull] TSqlParser.Alter_tableContext con
var constraint = _tableFactory.CreateAlterTable(context);
var table = DbObjects.OfType<Table>().Single(x => x.Identifier == constraint.ParentName);
table.AddConstraint(constraint);
constraint.SetTable(table);
DbObjects.Add(constraint);
}
}

Expand Down
7 changes: 6 additions & 1 deletion SqlSchemaCompare.Core/UpdateSchemaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static SqlSchemaCompare.Core.DbStructures.Table;

namespace SqlSchemaCompare.Core
{
Expand Down Expand Up @@ -195,7 +196,11 @@ private void ProcessTable(IEnumerable<DbObject> sourceObjects, IEnumerable<DbObj
{
if (!destinationIdentifier.Contains(tableOrigin.Identifier))
{
resultProcessDbObject.AddOperation(tableOrigin.Constraints, Operation.Create);
if (tableOrigin.PrimaryKeyDefinedInsideCreateTable)
resultProcessDbObject.AddOperation(tableOrigin.Constraints.Where(x => x is not TablePrimaryKeyConstraint).ToList(), Operation.Create);
else
resultProcessDbObject.AddOperation(tableOrigin.Constraints, Operation.Create);

resultProcessDbObject.AddOperation(tableOrigin.Indexes, Operation.Create);
resultProcessDbObject.AddOperation<Table>(tableOrigin, Operation.Create);
}
Expand Down
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Test/TSql/TSqlIndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ [ID] ASC

var objectFactory = new TSqlObjectFactory();
(var objects, var errors) = objectFactory.CreateObjectsForUpdateOperation(sql);
var table = objects.Single() as Table;
var table = objects.First() as Table;

table.Indexes.Single().Name.ShouldBe("[indexName]");
table.Indexes.Single().Schema.ShouldBeEmpty();
Expand Down
57 changes: 45 additions & 12 deletions SqlSchemaCompare.Test/TSql/TSqlTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,55 @@ public void CreateTable()
[Id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [db1]";

var sqlTable = $@"CREATE TABLE [schema].[TableName](
[Id] [int] IDENTITY(1,1) NOT NULL,
[col1] [char](8) NULL,
{constraint}
) ON [db1]";
var sqlTable =
$@"CREATE TABLE [schema].[TableName](
[Id] [int] IDENTITY(1,1) NOT NULL,
[col1] [char](8) NULL,
[col2] [DateTime] NOT NULL,
{constraint}
) ON [db1]
GO
ALTER TABLE [schema].[TableName] WITH NOCHECK ADD CONSTRAINT [FK_Name1] FOREIGN KEY([col1])
REFERENCES [dbo].[TBL2] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [schema].[TableName] ADD CONSTRAINT [constraintName] DEFAULT (getdate()) FOR [column1]
GO
";

var sql = $@"{sqlTable}
GO";

var objectFactory = new TSqlObjectFactory();
(var dbObjects, var errors) = objectFactory.CreateObjectsForUpdateOperation(sql);
var table = dbObjects.Single() as Table;
var table = dbObjects.First() as Table;

table.Name.ShouldBe("[TableName]");
table.Schema.ShouldBe("[schema]");
table.Sql.ShouldBe(sqlTable);
table.Sql.ShouldBe(
@"CREATE TABLE [schema].[TableName](
[Id] [int] IDENTITY(1,1) NOT NULL,
[col1] [char](8) NULL,
[col2] [DateTime] NOT NULL,
CONSTRAINT [PK_TableName_Id] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [db1]
) ON [db1]");

var column1 = table.Columns.ElementAt(0);
column1.Name.ShouldBe("[Id]");
column1.Sql.ShouldBe("[Id] [int] IDENTITY(1,1) NOT NULL");

var column2 = table.Columns.ElementAt(1);
column2.Name.ShouldBe("[col1]");
column2.Sql.ShouldBe("[col1] [char](8) NULL");
column2.Sql.ShouldBe("[col1] [char](8) NULL");

table.Constraints.Single().Sql.ShouldBe(constraint);
table.Constraints.First().Sql.ShouldBe(constraint);
table.Constraints[1].Table.ShouldBe(table);
table.Constraints[2].Table.ShouldBe(table);
errors.Count().ShouldBe(0);
}
[Fact]
Expand Down Expand Up @@ -123,7 +146,12 @@ public void CreateDbObject()
const string origin =
@"CREATE TABLE [dbo].[TBL] (
[ID] [int] IDENTITY(1,1) NOT NULL,
[column1] [Date] NOT NULL)
[column1] [Date] NOT NULL,
CONSTRAINT [PK] PRIMARY KEY NONCLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
ALTER TABLE [dbo].[TBL] WITH NOCHECK ADD CONSTRAINT [FK_Name1] FOREIGN KEY([column1])
Expand All @@ -141,7 +169,12 @@ ALTER TABLE [dbo].[TBL] CHECK CONSTRAINT [FK_Name1]
updateSchema.ShouldBe(
@"CREATE TABLE [dbo].[TBL] (
[ID] [int] IDENTITY(1,1) NOT NULL,
[column1] [Date] NOT NULL)
[column1] [Date] NOT NULL,
CONSTRAINT [PK] PRIMARY KEY NONCLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
ALTER TABLE [dbo].[TBL] WITH NOCHECK ADD CONSTRAINT [FK_Name1] FOREIGN KEY([column1])
Expand Down Expand Up @@ -324,7 +357,7 @@ ALTER TABLE [dbo].[TBL] ADD CONSTRAINT [constraintName] DEFAULT (getdate()) FO

var objectFactory = new TSqlObjectFactory();
(var dbObjects, var errors) = objectFactory.CreateObjectsForUpdateOperation(origin);
var table = dbObjects.Single() as Table;
var table = dbObjects.First() as Table;

table.Identifier.ShouldBe("[dbo].[TBL]");

Expand Down
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Test/TSql/TSqlTriggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ declare @variable int
{enableSql}
GO"
); ;
var dbobject = dbObjects.Single() as Trigger;
var dbobject = dbObjects.First() as Trigger;

dbobject.Name.ShouldBe("[trg1]");
dbobject.Schema.ShouldBeEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SignAssembly>true</SignAssembly>
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
<Version>1.0.18</Version>
<Version>1.0.19</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 69502aa

Please sign in to comment.