Skip to content

Commit cdd7713

Browse files
The SqlTableDifferences use a primary key object instance of a list to represents the difference of primary key (fixes #8).
1 parent d3ee00d commit cdd7713

9 files changed

Lines changed: 192 additions & 78 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void Visit(SqlTableDifferences differences)
105105

106106
this.Generate(differences.Indexes, "Indexes");
107107

108-
this.Generate(differences.PrimaryKeys, "Primary keys");
108+
this.Generate(differences.PrimaryKey, "Primary key");
109109

110110
this.Generate(differences.Triggers, "Triggers");
111111

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,20 @@ public static IList<SqlTableDifferences> Compare(IReadOnlyList<SqlTable> source,
211211
var indexesDifferences = Compare(sourceTable.Indexes, table.Indexes, i => i.Name, diff => new SqlIndexDifferences(diff));
212212

213213
// Compare the primary key
214-
var primaryKeyDifferences = Compare(CreateArray(sourceTable.PrimaryKey), CreateArray(table.PrimaryKey), pk => pk.Name, diff => new SqlPrimaryKeyDifferences(diff));
214+
var primaryKeyDifferences = (SqlPrimaryKeyDifferences?)Compare(sourceTable.PrimaryKey, table.PrimaryKey);
215215

216216
// Compare the triggers
217217
var triggersDifferences = Compare(sourceTable.Triggers, table.Triggers, tr => tr.Name);
218218

219219
// Compare the unique constraints
220220
var uniqueConstraintsDifferences = Compare(sourceTable.UniqueConstraints, table.UniqueConstraints, uc => uc.Name, diff => new SqlUniqueConstraintDifferences(diff));
221221

222-
if (columnsDifferences.Count + triggersDifferences.Count + checkConstraintDifferences.Count + indexesDifferences.Count + foreignKeysDifferences.Count + uniqueConstraintsDifferences.Count + primaryKeyDifferences.Count > 0)
222+
if (columnsDifferences.Count + triggersDifferences.Count + checkConstraintDifferences.Count + indexesDifferences.Count + foreignKeysDifferences.Count + uniqueConstraintsDifferences.Count > 0 || primaryKeyDifferences is not null)
223223
{
224-
return new SqlTableDifferences(sourceTable, table, SqlObjectDifferenceType.Different, [], primaryKeyDifferences, columnsDifferences, triggersDifferences, checkConstraintDifferences, indexesDifferences, foreignKeysDifferences, uniqueConstraintsDifferences);
224+
return new SqlTableDifferences(sourceTable, table, SqlObjectDifferenceType.Different, [], columnsDifferences, triggersDifferences, checkConstraintDifferences, indexesDifferences, foreignKeysDifferences, uniqueConstraintsDifferences)
225+
{
226+
PrimaryKey = primaryKeyDifferences,
227+
};
225228
}
226229

227230
return this.CreateDifferences(table);
@@ -328,17 +331,6 @@ private static IReadOnlyList<SqlObjectPropertyDifference> GetPropertyDifferences
328331
return objects.SingleOrDefault(o => Equals(keySelector(o), value));
329332
}
330333

331-
private static T[] CreateArray<T>(T? value)
332-
where T : class
333-
{
334-
if (value is null)
335-
{
336-
return [];
337-
}
338-
339-
return [value];
340-
}
341-
342334
private SqlObjectPropertyDifference? CompareProperty<TSqlObject>(TSqlObject target, Func<TSqlObject, object?> propertyValueForComparison, string name, Func<TSqlObject, object?>? propertyValueToDisplay = null)
343335
where TSqlObject : SqlObject
344336
{

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ internal SqlTableDifferences(
1818
SqlTable? target,
1919
SqlObjectDifferenceType type,
2020
IReadOnlyList<SqlObjectPropertyDifference>? properties,
21-
IList<SqlPrimaryKeyDifferences> primaryKeys,
2221
IList<SqlColumnDifferences> columns,
2322
IList<SqlObjectDifferences<SqlTrigger>> triggers,
2423
IList<SqlObjectDifferences<SqlCheckConstraint>> checkConstraints,
@@ -27,7 +26,6 @@ internal SqlTableDifferences(
2726
IList<SqlUniqueConstraintDifferences> uniqueConstraints)
2827
: base(source, target, type, properties)
2928
{
30-
this.PrimaryKeys = new ReadOnlyCollection<SqlPrimaryKeyDifferences>(primaryKeys);
3129
this.Columns = new ReadOnlyCollection<SqlColumnDifferences>(columns);
3230
this.Triggers = new ReadOnlyCollection<SqlObjectDifferences<SqlTrigger>>(triggers);
3331
this.CheckConstraints = new ReadOnlyCollection<SqlObjectDifferences<SqlCheckConstraint>>(checkConstraints);
@@ -38,7 +36,7 @@ internal SqlTableDifferences(
3836

3937
internal SqlTableDifferences(
4038
SqlObjectDifferences<SqlTable> differences)
41-
: this(differences.Source, differences.Target, differences.Type, differences.Properties, [], [], [], [], [], [], [])
39+
: this(differences.Source, differences.Target, differences.Type, differences.Properties, [], [], [], [], [], [])
4240
{
4341
}
4442

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

6563
/// <summary>
6664
/// Gets the foreign keys differences between the two SQL tables.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE [dbo].[TableWithDifferentPrimaryKey]
2+
(
3+
[Id] INT NOT NULL,
4+
5+
CONSTRAINT [PK_TableWithDifferentPrimaryKey_Source] PRIMARY KEY CLUSTERED ([Id] ASC)
6+
)

tests/Testing.Databases.SqlServer.Tests.Source/Testing.Databases.SqlServer.Tests.Source.sqlproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,6 @@
101101
<Build Include="Views\ViewSource.sql" />
102102
<Build Include="Views\ViewDifference.sql" />
103103
<Build Include="Tables\__EFMigrationsHistorySource.sql" />
104+
<Build Include="Tables\TableWithDifferentPrimaryKey.sql" />
104105
</ItemGroup>
105106
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE [dbo].[TableWithDifferentPrimaryKey]
2+
(
3+
[Id] INT NOT NULL,
4+
5+
CONSTRAINT [PK_TableWithDifferentPrimaryKey_Target] PRIMARY KEY CLUSTERED ([Id] ASC)
6+
)

tests/Testing.Databases.SqlServer.Tests.Target/Testing.Databases.SqlServer.Tests.Target.sqlproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,6 @@
115115
<Build Include="Tables\CheckConstraints\CheckConstraintIdentical.sql" />
116116
<Build Include="Tables\Triggers\TriggerIdentical.sql" />
117117
<Build Include="Tables\__EFMigrationsHistoryTarget.sql" />
118+
<Build Include="Tables\TableWithDifferentPrimaryKey.sql" />
118119
</ItemGroup>
119120
</Project>

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
* Position:
115115
Source: 1
116116
Target: 2
117-
------ Primary keys ------
117+
------ Primary key ------
118118
- PrimaryKeyDifference
119119
* Type:
120120
Source: NONCLUSTERED
@@ -165,6 +165,15 @@
165165
Source: 1
166166
Target: 2
167167
- dbo.TableTarget (Missing in the source)
168+
- dbo.TableWithDifferentPrimaryKey
169+
------ Indexes ------
170+
- PK_TableWithDifferentPrimaryKey_Target (Missing in the source)
171+
- PK_TableWithDifferentPrimaryKey_Source (Missing in the target)
172+
------ Primary key ------
173+
- PK_TableWithDifferentPrimaryKey_Source
174+
* Name:
175+
Source: PK_TableWithDifferentPrimaryKey_Source
176+
Target: PK_TableWithDifferentPrimaryKey_Target
168177
- dbo.TableSource (Missing in the target)
169178
------ Stored procedures ------
170179
- dbo.StoredProcedureDifference

0 commit comments

Comments
 (0)