Skip to content

Commit df8cd3f

Browse files
Fix a bug when comparing the primary key which have different names on the same table (fixes: #8).
1 parent 9a55469 commit df8cd3f

File tree

6 files changed

+35
-34
lines changed

6 files changed

+35
-34
lines changed

src/Testing.Databases.SqlServer/Comparer/SqlDatabaseComparisonResultsTextGenerator.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,7 @@ public void Visit(SqlTableDifferences differences)
9898

9999
this.Generate(differences.Indexes, "Indexes");
100100

101-
if (differences.PrimaryKey is not null)
102-
{
103-
this.Indent();
104-
this.WriteLine($"------ Primary key ------");
105-
differences.PrimaryKey.Accept(this);
106-
this.Unindent();
107-
}
101+
this.Generate(differences.PrimaryKeys, "Primary keys");
108102

109103
this.Generate(differences.Triggers, "Triggers");
110104

src/Testing.Databases.SqlServer/Comparer/SqlObjectComparer.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static IList<SqlObjectDifferences<TSqlObject>> Compare<TSqlObject>(IReadO
6161

6262
public static IList<SqlTableDifferences> Compare(IReadOnlyList<SqlTable> source, IReadOnlyList<SqlTable> target)
6363
{
64-
return Compare(source, target, t => t.Name, diff => new SqlTableDifferences(diff) { PrimaryKey = null });
64+
return Compare(source, target, t => t.Name, diff => new SqlTableDifferences(diff));
6565
}
6666

6767
public SqlObjectDifferences? Visit(SqlCheckConstraint checkConstraint)
@@ -200,20 +200,17 @@ public static IList<SqlTableDifferences> Compare(IReadOnlyList<SqlTable> source,
200200
var indexesDifferences = Compare(sourceTable.Indexes, table.Indexes, i => i.Name, diff => new SqlIndexDifferences(diff));
201201

202202
// Compare the primary key
203-
var primaryKeyDifferences = (SqlPrimaryKeyDifferences?)Compare(CreateArray(sourceTable.PrimaryKey), CreateArray(table.PrimaryKey), pk => pk.Name).SingleOrDefault();
203+
var primaryKeyDifferences = Compare(CreateArray(sourceTable.PrimaryKey), CreateArray(table.PrimaryKey), pk => pk.Name, diff => new SqlPrimaryKeyDifferences(diff));
204204

205205
// Compare the triggers
206206
var triggersDifferences = Compare(sourceTable.Triggers, table.Triggers, tr => tr.Name);
207207

208208
// Compare the unique constraints
209209
var uniqueConstraintsDifferences = Compare(sourceTable.UniqueConstraints, table.UniqueConstraints, uc => uc.Name, diff => new SqlUniqueConstraintDifferences(diff));
210210

211-
if (columnsDifferences.Count + triggersDifferences.Count + checkConstraintDifferences.Count + indexesDifferences.Count + foreignKeysDifferences.Count + uniqueConstraintsDifferences.Count > 0 || primaryKeyDifferences is not null)
211+
if (columnsDifferences.Count + triggersDifferences.Count + checkConstraintDifferences.Count + indexesDifferences.Count + foreignKeysDifferences.Count + uniqueConstraintsDifferences.Count + primaryKeyDifferences.Count > 0)
212212
{
213-
return new SqlTableDifferences(sourceTable, table, SqlObjectDifferenceType.Different, [], columnsDifferences, triggersDifferences, checkConstraintDifferences, indexesDifferences, foreignKeysDifferences, uniqueConstraintsDifferences)
214-
{
215-
PrimaryKey = primaryKeyDifferences,
216-
};
213+
return new SqlTableDifferences(sourceTable, table, SqlObjectDifferenceType.Different, [], primaryKeyDifferences, columnsDifferences, triggersDifferences, checkConstraintDifferences, indexesDifferences, foreignKeysDifferences, uniqueConstraintsDifferences);
217214
}
218215

219216
return this.CreateDifferences(table);

src/Testing.Databases.SqlServer/Comparer/SqlPrimaryKeyDifferences.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ internal SqlPrimaryKeyDifferences(
2424
this.Columns = new ReadOnlyCollection<SqlObjectDifferences<SqlPrimaryKeyColumn>>(columns);
2525
}
2626

27+
internal SqlPrimaryKeyDifferences(
28+
SqlObjectDifferences<SqlPrimaryKey> differences)
29+
: this(differences.Source, differences.Target, differences.Type, differences.Properties, [])
30+
{
31+
}
32+
2733
/// <summary>
2834
/// Gets the difference of the columns in the primary key compared.
2935
/// </summary>

src/Testing.Databases.SqlServer/Comparer/SqlTableDifferences.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal SqlTableDifferences(
1818
SqlTable? target,
1919
SqlObjectDifferenceType type,
2020
IReadOnlyList<SqlObjectPropertyDifference>? properties,
21+
IList<SqlPrimaryKeyDifferences> primaryKeys,
2122
IList<SqlObjectDifferences<SqlColumn>> columns,
2223
IList<SqlObjectDifferences<SqlTrigger>> triggers,
2324
IList<SqlObjectDifferences<SqlCheckConstraint>> checkConstraints,
@@ -26,6 +27,7 @@ internal SqlTableDifferences(
2627
IList<SqlUniqueConstraintDifferences> uniqueConstraints)
2728
: base(source, target, type, properties)
2829
{
30+
this.PrimaryKeys = new ReadOnlyCollection<SqlPrimaryKeyDifferences>(primaryKeys);
2931
this.Columns = new ReadOnlyCollection<SqlObjectDifferences<SqlColumn>>(columns);
3032
this.Triggers = new ReadOnlyCollection<SqlObjectDifferences<SqlTrigger>>(triggers);
3133
this.CheckConstraints = new ReadOnlyCollection<SqlObjectDifferences<SqlCheckConstraint>>(checkConstraints);
@@ -36,7 +38,7 @@ internal SqlTableDifferences(
3638

3739
internal SqlTableDifferences(
3840
SqlObjectDifferences<SqlTable> differences)
39-
: this(differences.Source, differences.Target, differences.Type, differences.Properties, [], [], [], [], [], [])
41+
: this(differences.Source, differences.Target, differences.Type, differences.Properties, [], [], [], [], [], [], [])
4042
{
4143
}
4244

@@ -58,7 +60,7 @@ internal SqlTableDifferences(
5860
/// <summary>
5961
/// Gets the primary key differences between the two SQL tables.
6062
/// </summary>
61-
public SqlPrimaryKeyDifferences? PrimaryKey { get; internal set; }
63+
public ReadOnlyCollection<SqlPrimaryKeyDifferences> PrimaryKeys { get; }
6264

6365
/// <summary>
6466
/// Gets the foreign keys differences between the two SQL tables.

tests/Testing.Databases.SqlServer.Tests/SqlServerDatabaseComparerTest.CompareAsync.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@
9999
* Position:
100100
Source: 1
101101
Target: 2
102-
------ Primary key ------
102+
------ Primary keys ------
103+
- PrimaryKeyDifference
103104
* Type:
104105
Source: NONCLUSTERED
105106
Target: CLUSTERED

tests/Testing.Databases.SqlServer.Tests/SqlServerDatabaseComparerTest.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -535,21 +535,22 @@ public async Task CompareAsync()
535535
differences.Tables[0].Target.PrimaryKey.Columns[1].Name.Should().Be("Type");
536536
differences.Tables[0].Target.PrimaryKey.Columns[1].Position.Should().Be(2);
537537

538-
differences.Tables[0].PrimaryKey.Columns.Should().HaveCount(2);
539-
differences.Tables[0].PrimaryKey.Columns[0].Source.Should().BeSameAs(differences.Tables[0].Source.PrimaryKey.Columns[1]);
540-
differences.Tables[0].PrimaryKey.Columns[0].Target.Should().BeSameAs(differences.Tables[0].Target.PrimaryKey.Columns[0]);
541-
differences.Tables[0].PrimaryKey.Columns[0].Type.Should().Be(SqlObjectDifferenceType.Different);
542-
differences.Tables[0].PrimaryKey.Columns[1].Source.Should().BeSameAs(differences.Tables[0].Source.PrimaryKey.Columns[0]);
543-
differences.Tables[0].PrimaryKey.Columns[1].Target.Should().BeSameAs(differences.Tables[0].Target.PrimaryKey.Columns[1]);
544-
differences.Tables[0].PrimaryKey.Columns[1].Type.Should().Be(SqlObjectDifferenceType.Different);
545-
546-
differences.Tables[0].PrimaryKey.Properties.Should().HaveCount(1);
547-
differences.Tables[0].PrimaryKey.Properties[0].Name.Should().Be("Type");
548-
differences.Tables[0].PrimaryKey.Properties[0].Source.Should().Be("NONCLUSTERED");
549-
differences.Tables[0].PrimaryKey.Properties[0].Target.Should().Be("CLUSTERED");
550-
differences.Tables[0].PrimaryKey.Source.Should().Be(differences.Tables[0].Source.PrimaryKey);
551-
differences.Tables[0].PrimaryKey.Target.Should().Be(differences.Tables[0].Target.PrimaryKey);
552-
differences.Tables[0].PrimaryKey.Type.Should().Be(SqlObjectDifferenceType.Different);
538+
differences.Tables[0].PrimaryKeys.Should().HaveCount(1);
539+
differences.Tables[0].PrimaryKeys[0].Columns.Should().HaveCount(2);
540+
differences.Tables[0].PrimaryKeys[0].Columns[0].Source.Should().BeSameAs(differences.Tables[0].Source.PrimaryKey.Columns[1]);
541+
differences.Tables[0].PrimaryKeys[0].Columns[0].Target.Should().BeSameAs(differences.Tables[0].Target.PrimaryKey.Columns[0]);
542+
differences.Tables[0].PrimaryKeys[0].Columns[0].Type.Should().Be(SqlObjectDifferenceType.Different);
543+
differences.Tables[0].PrimaryKeys[0].Columns[1].Source.Should().BeSameAs(differences.Tables[0].Source.PrimaryKey.Columns[0]);
544+
differences.Tables[0].PrimaryKeys[0].Columns[1].Target.Should().BeSameAs(differences.Tables[0].Target.PrimaryKey.Columns[1]);
545+
differences.Tables[0].PrimaryKeys[0].Columns[1].Type.Should().Be(SqlObjectDifferenceType.Different);
546+
547+
differences.Tables[0].PrimaryKeys[0].Properties.Should().HaveCount(1);
548+
differences.Tables[0].PrimaryKeys[0].Properties[0].Name.Should().Be("Type");
549+
differences.Tables[0].PrimaryKeys[0].Properties[0].Source.Should().Be("NONCLUSTERED");
550+
differences.Tables[0].PrimaryKeys[0].Properties[0].Target.Should().Be("CLUSTERED");
551+
differences.Tables[0].PrimaryKeys[0].Source.Should().Be(differences.Tables[0].Source.PrimaryKey);
552+
differences.Tables[0].PrimaryKeys[0].Target.Should().Be(differences.Tables[0].Target.PrimaryKey);
553+
differences.Tables[0].PrimaryKeys[0].Type.Should().Be(SqlObjectDifferenceType.Different);
553554

554555
// Tables / Triggers
555556
differences.Tables[0].Source.Triggers.Should().HaveCount(1);
@@ -607,7 +608,7 @@ public async Task CompareAsync()
607608
// Missing tables
608609
differences.Tables[1].Columns.Should().BeEmpty();
609610
differences.Tables[1].Indexes.Should().BeEmpty();
610-
differences.Tables[1].PrimaryKey.Should().BeNull();
611+
differences.Tables[1].PrimaryKeys.Should().BeEmpty();
611612
differences.Tables[1].Source.Should().BeNull();
612613
differences.Tables[1].UniqueConstraints.Should().BeEmpty();
613614
differences.Tables[1].Target.CheckConstraints.Should().HaveCount(1);
@@ -667,7 +668,7 @@ public async Task CompareAsync()
667668

668669
differences.Tables[2].Columns.Should().BeEmpty();
669670
differences.Tables[2].Indexes.Should().BeEmpty();
670-
differences.Tables[2].PrimaryKey.Should().BeNull();
671+
differences.Tables[2].PrimaryKeys.Should().BeEmpty();
671672
differences.Tables[2].UniqueConstraints.Should().BeEmpty();
672673
differences.Tables[2].Source.CheckConstraints.Should().HaveCount(1);
673674
differences.Tables[2].Source.CheckConstraints[0].Name.Should().Be("CheckConstraintSource");

0 commit comments

Comments
 (0)