Skip to content

Commit

Permalink
drop constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 13, 2021
1 parent 861bd97 commit 064d042
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
1 change: 1 addition & 0 deletions SqlSchemaCompare.Core/DbStructures/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum ConstraintTypes
public IEnumerable<string> ColumnName { get; init; }
public ConstraintTypes ConstraintType { get; init; }
public string Value { get; init; }
public Table Table { get; init; }
}
public IList<Column> Columns { get; } = new List<Column>();
public IList<TableConstraint> Constraints { get; } = new List<TableConstraint>();
Expand Down
11 changes: 6 additions & 5 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
}
else if (columnTree.table_constraint() != null)
{
table.AddConstraint(CreatePrimaryKeyConstraint(columnTree.table_constraint(), stream, table.Identifier));
table.AddConstraint(CreatePrimaryKeyConstraint(columnTree.table_constraint(), stream, table));
}
}
return table;
Expand All @@ -49,15 +49,16 @@ private Table.Column CreateColumn(TSqlParser.Column_def_table_constraintContext
};
}

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

Expand Down
33 changes: 27 additions & 6 deletions SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,43 @@ public string BuildSeparator()
{
return "GO\r\n";
}
private string BuildTableConstraint(Table.TableConstraint alterTable, Operation operation, ResultProcessDbObject resultProcessDbObject)
private string BuildTableConstraint(Table.TableConstraint constraint, Operation operation, ResultProcessDbObject resultProcessDbObject)
{
switch (operation)
{
case Operation.Create:
if (!resultProcessDbObject.GetDbObject(DbObjectType.Column, Operation.Create).Any(x => alterTable.ColumnName.Contains(x.Name)))
if (!resultProcessDbObject.GetDbObject(DbObjectType.Column, Operation.Create).Any(x => constraint.ColumnName.Contains(x.Name)))
{
if (alterTable.ConstraintType == Table.TableConstraint.ConstraintTypes.PrimaryKey)
if (constraint.ConstraintType == Table.TableConstraint.ConstraintTypes.PrimaryKey)
{
return $"ALTER TABLE ADD {alterTable.Sql}";
return $"ALTER TABLE {constraint.ParentName} ADD {constraint.Sql}";
}
return alterTable.Sql;
return constraint.Sql;
}
return string.Empty;
case Operation.Drop:
return $"ALTER TABLE {alterTable.ParentName} DROP CONSTRAINT {alterTable.Name}";
if (String.IsNullOrEmpty(constraint.Name))
{
var tableSchema = constraint.Table.Schema.Replace("[", "").Replace("]", "");
var tableName = constraint.Table.Name.Replace("[", "").Replace("]", "");
return
@$"DECLARE @PrimaryKeyName_{tableSchema}_{tableName} sysname =
(
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_SCHEMA='{tableSchema}' AND TABLE_NAME = '{tableName}'
)
IF @PrimaryKeyName_{tableSchema}_{tableName} IS NOT NULL
BEGIN
DECLARE @SQL_PK_{tableSchema}_{tableName} NVARCHAR(MAX) = 'ALTER TABLE {constraint.ParentName} DROP CONSTRAINT ' + @PrimaryKeyName_{tableSchema}_{tableName}
EXEC sp_executesql @SQL_PK_{tableSchema}_{tableName};
END";
}
else
{
return $"ALTER TABLE {constraint.ParentName} DROP CONSTRAINT {constraint.Name}";
}
default:
throw new NotSupportedException("Alter not supported on schema");
};
Expand Down
40 changes: 39 additions & 1 deletion SqlSchemaCompare.Test/TSql/TSqlTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ 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
ALTER TABLE [dbo].[tblLookup] 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]
Expand All @@ -480,6 +480,44 @@ ALTER TABLE ADD CONSTRAINT [PK] PRIMARY KEY CLUSTERED
errors.ShouldBeEmpty();
}

[Fact]
public void DropConstraintWithoutName()
{
const string origin =
@"CREATE TABLE [schema].[tbl](
[ID] [int] IDENTITY(0,1) NOT NULL)
GO";

const string destination =
@"CREATE TABLE [schema].[tbl](
[ID] [int] IDENTITY(0,1) NOT NULL,
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 [db]
) ON [db] TEXTIMAGE_ON [db]
GO";

(string updateSchema, string errors) = UtilityTest.UpdateSchema(origin, destination, SelectedObjects);

updateSchema.ShouldBe(
@"DECLARE @PrimaryKeyName_schema_tbl sysname =
(
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_SCHEMA='schema' AND TABLE_NAME = 'tbl'
)
IF @PrimaryKeyName_schema_tbl IS NOT NULL
BEGIN
DECLARE @SQL_PK_schema_tbl NVARCHAR(MAX) = 'ALTER TABLE [schema].[tbl] DROP CONSTRAINT ' + @PrimaryKeyName_schema_tbl
EXEC sp_executesql @SQL_PK_schema_tbl;
END
GO
");
errors.ShouldBeEmpty();
}

[Fact]
public void AddColumnAndAddDefaultConstraintOnSameStatement()
Expand Down

0 comments on commit 064d042

Please sign in to comment.