Skip to content

Commit

Permalink
Drop PK on alter column and create
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 10, 2021
1 parent c4784a7 commit 441db14
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
11 changes: 3 additions & 8 deletions SqlSchemaCompare.Core/DbStructures/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@ public class TableConstraint : DbObject
public enum ConstraintTypes
{
Default,
ForeignKey
ForeignKey,
PrimaryKey
}
public override DbObjectType DbObjectType => DbObjectType.TableContraint;
public string ColumnName { get; init; }
public IEnumerable<string> ColumnName { get; init; }
public ConstraintTypes ConstraintType { get; init; }
public string Value { get; init; }
}
public IList<Column> Columns { get; } = new List<Column>();
public IList<TableConstraint> Constraints { get; } = new List<TableConstraint>();
public string Constraint { get; private set; }

public void AdColumns(Column Colum) => Columns.Add(Colum);
public void AddConstraint(TableConstraint tableConstraint) => Constraints.Add(tableConstraint);
public void SetConstraint(string constraint)
{
Constraint = constraint;
}
}
}
18 changes: 15 additions & 3 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using Antlr4.Runtime.Misc;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SqlSchemaCompare.Core.TSql.Factory
{
Expand All @@ -30,7 +30,7 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
}
else if (columnTree.table_constraint() != null)
{
table.SetConstraint(stream.GetText(new Interval(columnTree.table_constraint().start.StartIndex, columnTree.table_constraint().stop.StopIndex)));
table.AddConstraint(CreatePrimaryKeyConstraint(columnTree.table_constraint(), stream, table.Identifier));
}
}
return table;
Expand All @@ -49,6 +49,18 @@ private Table.Column CreateColumn(TSqlParser.Column_def_table_constraintContext
};
}

public Table.TableConstraint CreatePrimaryKeyConstraint(TSqlParser.Table_constraintContext constraintContext, ICharStream stream, string tableName)
{
return new Table.TableConstraint
{
Sql = stream.GetText(new Interval(constraintContext.start.StartIndex, constraintContext.stop.StopIndex)),
Name = constraintContext.id_()[0].GetText(),
ParentName = tableName,
ColumnName = constraintContext.column_name_list_with_order().id_().Select(x => x.GetText()),
ConstraintType = Table.TableConstraint.ConstraintTypes.PrimaryKey
};
}

public Table.TableConstraint CreateAlterTable(ParserRuleContext context)
{
var alterTableContext = context as TSqlParser.Alter_tableContext;
Expand Down Expand Up @@ -83,7 +95,7 @@ public Table.TableConstraint CreateAlterTable(ParserRuleContext context)
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
Name = name,
ParentName = tableName,
ColumnName = columnName,
ColumnName = new List<string> {columnName},
ConstraintType = constraintType,
Value = value.ToString()
};
Expand Down
8 changes: 6 additions & 2 deletions SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ private string BuildTableConstraint(Table.TableConstraint alterTable, Operation
switch (operation)
{
case Operation.Create:
if (!resultProcessDbObject.GetDbObject(DbObjectType.Column, Operation.Create).Any(x => x.Name == alterTable.ColumnName))
if (!resultProcessDbObject.GetDbObject(DbObjectType.Column, Operation.Create).Any(x => alterTable.ColumnName.Contains(x.Name)))
{
if (alterTable.ConstraintType == Table.TableConstraint.ConstraintTypes.PrimaryKey)
{
return $"ALTER TABLE ADD {alterTable.Sql}";
}
return alterTable.Sql;
}
return string.Empty;
Expand All @@ -81,7 +85,7 @@ private string BuildColumn(Table.Column column, Operation operation)
{
case Operation.Create:
var sql = $"ALTER TABLE {column.ParentName} ADD { column.Sql}";
var constraintRelated = column.Table.Constraints.SingleOrDefault(x => x.ColumnName == column.Name && x.ConstraintType == Table.TableConstraint.ConstraintTypes.Default);
var constraintRelated = column.Table.Constraints.SingleOrDefault(x => x.ColumnName.Contains(column.Name) && x.ConstraintType == Table.TableConstraint.ConstraintTypes.Default);
if (constraintRelated != null)
{
sql = $"{sql} CONSTRAINT {constraintRelated.Name} DEFAULT {constraintRelated.Value}";
Expand Down
4 changes: 2 additions & 2 deletions SqlSchemaCompare.Core/UpdateSchemaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ private void ProcessTableColumn(Table table, Table destinationTable, IEnumerable
resultProcessDbObject.AddOperation(columnsToDrop, Operation.Drop);

var columnsOfCostraintToDropAndCreate = destinationTable
.Constraints.Select(x => x.ColumnName)
.Constraints.SelectMany(x => x.ColumnName)
.Intersect(columnsToAlter.Select(x => x.Name));

var constrainttoDropAndCreate = destinationTable.Constraints
.Where(x => columnsOfCostraintToDropAndCreate.Contains(x.ColumnName))
.Where(x => columnsOfCostraintToDropAndCreate.Intersect(x.ColumnName).Any())
.Except(resultProcessDbObject.GetDbObject(DbObjectType.TableContraint, Operation.Drop))//constraint that we have to drop
.ToList();
resultProcessDbObject.AddOperation(constrainttoDropAndCreate, Operation.Drop);
Expand Down
25 changes: 22 additions & 3 deletions SqlSchemaCompare.Test/TSql/TSqlTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ [col1] [char](8) NULL,
column2.Name.ShouldBe("[col1]");
column2.Sql.ShouldBe("[col1] [char](8) NULL");

table.Constraint.ShouldBe(constraint);
table.Constraints.Single().Sql.ShouldBe(constraint);
errors.Count().ShouldBe(0);
}
[Fact]
Expand Down Expand Up @@ -406,7 +406,12 @@ [ID] [int] IDENTITY(1,1) NOT NULL,
CREATE TABLE [dbo].[tblLookup](
[ID] [int] NOT NULL,
[description] [nvarchar](50) NOT NULL)
[description] [nvarchar](50) NOT NULL,
CONSTRAINT [PK] PRIMARY KEY CLUSTERED
(
[ID] ASC, [description] DESC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [db]
) ON [db]
GO
ALTER TABLE [dbo].[tbl] WITH CHECK ADD CONSTRAINT [FK_constraint] FOREIGN KEY([column1])
Expand All @@ -423,7 +428,12 @@ [ID] [int] IDENTITY(1,1) NOT NULL,
CREATE TABLE [dbo].[tblLookup](
[ID] [tinyint] NOT NULL,
[description] [nvarchar](50) NOT NULL)
[description] [nvarchar](50) NOT NULL,
CONSTRAINT [PK] PRIMARY KEY CLUSTERED
(
[ID] ASC, [description] DESC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [db]
) ON [db]
GO
ALTER TABLE [dbo].[tbl] WITH CHECK ADD CONSTRAINT [FK_constraint] FOREIGN KEY([column1])
Expand All @@ -438,6 +448,9 @@ REFERENCES [dbo].[tblLookup] ([ID])
@"ALTER TABLE [dbo].[tbl] DROP CONSTRAINT [FK_constraint]
GO
ALTER TABLE [dbo].[tblLookup] DROP CONSTRAINT [PK]
GO
ALTER TABLE [dbo].[tbl] ALTER COLUMN [column1] [int] NOT NULL
GO
Expand All @@ -448,6 +461,12 @@ ALTER TABLE [dbo].[tbl] WITH CHECK ADD CONSTRAINT [FK_constraint] FOREIGN KEY([c
REFERENCES [dbo].[tblLookup] ([ID])
GO
ALTER TABLE ADD CONSTRAINT [PK] PRIMARY KEY CLUSTERED
(
[ID] ASC, [description] DESC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [db]
GO
");
errors.ShouldBeEmpty();
}
Expand Down

0 comments on commit 441db14

Please sign in to comment.