-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlObjectComparer.cs
More file actions
368 lines (295 loc) · 16.3 KB
/
SqlObjectComparer.cs
File metadata and controls
368 lines (295 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//-----------------------------------------------------------------------
// <copyright file="SqlObjectComparer.cs" company="P.O.S Informatique">
// Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace PosInformatique.Testing.Databases.SqlServer
{
internal sealed class SqlObjectComparer : ISqlObjectVisitor<SqlObjectDifferences?>
{
private readonly SqlObject source;
public SqlObjectComparer(SqlObject source)
{
this.source = source;
}
public static IList<SqlObjectDifferences<TSqlObject>> Compare<TSqlObject>(IReadOnlyList<TSqlObject> source, IReadOnlyList<TSqlObject> target, Func<TSqlObject, object> keySelector)
where TSqlObject : SqlObject
{
var differences = new List<SqlObjectDifferences<TSqlObject>>();
// Iterate on the target objects
foreach (var targetObject in target)
{
var keyValue = keySelector(targetObject);
var sourceObject = Find(source, keySelector, keyValue);
var difference = Compare(sourceObject, targetObject);
if (difference is not null)
{
differences.Add(difference);
}
}
// Iterate on the source objects
foreach (var sourceObject in source)
{
var keyValue = keySelector(sourceObject);
var targetObject = Find(target, keySelector, keyValue);
if (targetObject is null)
{
// Missing in the target.
differences.Add(new SqlObjectDifferences<TSqlObject>(sourceObject, null, SqlObjectDifferenceType.MissingInTarget, null));
}
}
return differences;
}
public static IList<SqlTableDifferences> Compare(IReadOnlyList<SqlTable> source, IReadOnlyList<SqlTable> target)
{
return Compare(source, target, t => t.Name, diff => new SqlTableDifferences(diff));
}
public SqlObjectDifferences? Visit(SqlCheckConstraint checkConstraint)
{
return this.CreateDifferences(
checkConstraint,
this.CompareProperty(checkConstraint, ck => TsqlCodeHelper.RemoveNotUsefulCharacters(ck.Code), nameof(checkConstraint.Code), ck => ck.Code));
}
public SqlObjectDifferences? Visit(SqlColumn column)
{
var sourceColumn = (SqlColumn)this.source;
// Compare the properties
var differenceProperties = GetPropertyDifferences(
this.CompareProperty(column, t => t.Position, nameof(column.Position)),
this.CompareProperty(column, t => t.MaxLength, nameof(column.MaxLength)),
this.CompareProperty(column, t => t.Precision, nameof(column.Precision)),
this.CompareProperty(column, t => t.Scale, nameof(column.Scale)),
this.CompareProperty(column, t => t.IsNullable, nameof(column.IsNullable)),
this.CompareProperty(column, t => t.IsIdentity, nameof(column.IsIdentity)),
this.CompareProperty(column, t => t.CollationName, nameof(column.CollationName)),
this.CompareProperty(column, t => t.IsComputed, nameof(column.IsComputed)),
this.CompareProperty(column, t => TsqlCodeHelper.RemoveNotUsefulCharacters(t.ComputedExpression), nameof(column.ComputedExpression), t => t.ComputedExpression));
// Compare the default constraint
var defaultConstraintDifference = Compare(sourceColumn.DefaultConstraint, column.DefaultConstraint);
if (differenceProperties.Count > 0 || defaultConstraintDifference != null)
{
return new SqlColumnDifferences((SqlColumn)this.source, column, SqlObjectDifferenceType.Different, differenceProperties, defaultConstraintDifference);
}
return null;
}
public SqlObjectDifferences? Visit(SqlDefaultConstraint defaultConstraint)
{
return this.CreateDifferences(
defaultConstraint,
this.CompareProperty(defaultConstraint, df => df.Name, nameof(defaultConstraint.Name)),
this.CompareProperty(defaultConstraint, df => TsqlCodeHelper.RemoveNotUsefulCharacters(df.Expression), nameof(defaultConstraint.Expression), df => df.Expression));
}
public SqlObjectDifferences? Visit(SqlForeignKey foreignKey)
{
var sourceForeignKey = (SqlForeignKey)this.source;
// Compare the columns
var columnsDifferences = Compare(sourceForeignKey.Columns, foreignKey.Columns, c => c.Name);
// Compare the properties of the foreign key
var differenceProperties = GetPropertyDifferences(
this.CompareProperty(foreignKey, fk => fk.DeleteAction, nameof(foreignKey.DeleteAction)),
this.CompareProperty(foreignKey, fk => fk.ReferencedTable, nameof(foreignKey.ReferencedTable)),
this.CompareProperty(foreignKey, fk => fk.UpdateAction, nameof(foreignKey.UpdateAction)));
if (columnsDifferences.Count + differenceProperties.Count > 0)
{
return new SqlForeignKeyDifferences(sourceForeignKey, foreignKey, SqlObjectDifferenceType.Different, differenceProperties, columnsDifferences);
}
return null;
}
public SqlObjectDifferences? Visit(SqlForeignKeyColumn column)
{
return this.CreateDifferences(
column,
this.CompareProperty(column, c => c.Name, nameof(column.Name)),
this.CompareProperty(column, c => c.Position, nameof(column.Position)),
this.CompareProperty(column, c => c.Referenced, nameof(column.Referenced)));
}
public SqlObjectDifferences? Visit(SqlIndex index)
{
var sourceIndex = (SqlIndex)this.source;
// Compare the columns
var columnsDifferences = Compare(sourceIndex.Columns, index.Columns, c => c.Name);
// Compare the included columns
var includedColumnsDifferences = Compare(sourceIndex.IncludedColumns, index.IncludedColumns, c => c.Name);
// Compare the properties of the index
var differenceProperties = GetPropertyDifferences(
this.CompareProperty(index, i => i.IsUnique, nameof(index.IsUnique)),
this.CompareProperty(index, i => TsqlCodeHelper.RemoveNotUsefulCharacters(i.Filter), nameof(index.Filter), i => i.Filter),
this.CompareProperty(index, i => i.Type, nameof(index.Type)));
if (columnsDifferences.Count + includedColumnsDifferences.Count + differenceProperties.Count > 0)
{
return new SqlIndexDifferences(sourceIndex, index, SqlObjectDifferenceType.Different, differenceProperties, columnsDifferences, includedColumnsDifferences);
}
return null;
}
public SqlObjectDifferences? Visit(SqlIndexColumn column)
{
return this.CreateDifferences(
column,
this.CompareProperty(column, c => c.Name, nameof(column.Name)),
this.CompareProperty(column, c => c.Position, nameof(column.Position)));
}
public SqlObjectDifferences? Visit(SqlPrimaryKey primaryKey)
{
var sourcePrimaryKey = (SqlPrimaryKey)this.source;
// Compare the columns
var columnsDifferences = Compare(sourcePrimaryKey.Columns, primaryKey.Columns, c => c.Name);
// Compare the properties of the primary key
var differenceProperties = GetPropertyDifferences(
this.CompareProperty(primaryKey, pk => pk.Name, nameof(primaryKey.Name)),
this.CompareProperty(primaryKey, pk => pk.Type, nameof(primaryKey.Type)));
if (columnsDifferences.Count + differenceProperties.Count > 0)
{
return new SqlPrimaryKeyDifferences(sourcePrimaryKey, primaryKey, SqlObjectDifferenceType.Different, differenceProperties, columnsDifferences);
}
return null;
}
public SqlObjectDifferences? Visit(SqlPrimaryKeyColumn column)
{
return this.CreateDifferences(
column,
this.CompareProperty(column, c => c.Name, nameof(column.Name)),
this.CompareProperty(column, c => c.Position, nameof(column.Position)));
}
public SqlObjectDifferences? Visit(SqlStoredProcedure storedProcedure)
{
return this.CreateDifferences(
storedProcedure,
this.CompareProperty(storedProcedure, sp => TsqlCodeHelper.RemoveNotUsefulCharacters(sp.Code), nameof(storedProcedure.Code), sp => sp.Code));
}
public SqlObjectDifferences? Visit(SqlTable table)
{
var sourceTable = (SqlTable)this.source;
// Compare the check constraints
var checkConstraintDifferences = Compare(sourceTable.CheckConstraints, table.CheckConstraints, tr => tr.Name);
// Compare the columns
var columnsDifferences = Compare(sourceTable.Columns, table.Columns, c => c.Name, diff => new SqlColumnDifferences(diff));
// Compare the foreign keys
var foreignKeysDifferences = Compare(sourceTable.ForeignKeys, table.ForeignKeys, fk => fk.Name, diff => new SqlForeignKeyDifferences(diff));
// Compare the indexes
var indexesDifferences = Compare(sourceTable.Indexes, table.Indexes, i => i.Name, diff => new SqlIndexDifferences(diff));
// Compare the primary key
var primaryKeyDifferences = (SqlPrimaryKeyDifferences?)Compare(sourceTable.PrimaryKey, table.PrimaryKey);
// Compare the triggers
var triggersDifferences = Compare(sourceTable.Triggers, table.Triggers, tr => tr.Name);
// Compare the unique constraints
var uniqueConstraintsDifferences = Compare(sourceTable.UniqueConstraints, table.UniqueConstraints, uc => uc.Name, diff => new SqlUniqueConstraintDifferences(diff));
if (columnsDifferences.Count + triggersDifferences.Count + checkConstraintDifferences.Count + indexesDifferences.Count + foreignKeysDifferences.Count + uniqueConstraintsDifferences.Count > 0 || primaryKeyDifferences is not null)
{
return new SqlTableDifferences(sourceTable, table, SqlObjectDifferenceType.Different, [], columnsDifferences, triggersDifferences, checkConstraintDifferences, indexesDifferences, foreignKeysDifferences, uniqueConstraintsDifferences)
{
PrimaryKey = primaryKeyDifferences,
};
}
return this.CreateDifferences(table);
}
public SqlObjectDifferences? Visit(SqlTrigger trigger)
{
return this.CreateDifferences(
trigger,
this.CompareProperty(trigger, tr => tr.IsInsteadOfTrigger, nameof(trigger.IsInsteadOfTrigger)),
this.CompareProperty(trigger, tr => TsqlCodeHelper.RemoveNotUsefulCharacters(tr.Code), nameof(trigger.Code), tr => tr.Code));
}
public SqlObjectDifferences? Visit(SqlUniqueConstraint uniqueConstraint)
{
var sourceUniqueConstraint = (SqlUniqueConstraint)this.source;
// Compare the columns
var columnsDifferences = Compare(sourceUniqueConstraint.Columns, uniqueConstraint.Columns, c => c.Name);
// Compare the properties of the index
var differenceProperties = GetPropertyDifferences(
this.CompareProperty(uniqueConstraint, uc => uc.Type, nameof(uniqueConstraint.Type)));
if (columnsDifferences.Count + differenceProperties.Count > 0)
{
return new SqlUniqueConstraintDifferences(sourceUniqueConstraint, uniqueConstraint, SqlObjectDifferenceType.Different, differenceProperties, columnsDifferences);
}
return null;
}
public SqlObjectDifferences? Visit(SqlUserType userType)
{
return this.CreateDifferences(
userType,
this.CompareProperty(userType, ut => ut.IsNullable, nameof(userType.IsNullable)),
this.CompareProperty(userType, ut => ut.IsTableType, nameof(userType.IsTableType)),
this.CompareProperty(userType, ut => ut.MaxLength, nameof(userType.MaxLength)));
}
public SqlObjectDifferences? Visit(SqlView view)
{
return this.CreateDifferences(
view,
this.CompareProperty(view, v => TsqlCodeHelper.RemoveNotUsefulCharacters(v.Code), nameof(view.Code), v => v.Code));
}
private static SqlObjectDifferences<TSqlObject>? Compare<TSqlObject>(TSqlObject? source, TSqlObject? target)
where TSqlObject : SqlObject
{
if (source is null)
{
if (target is null)
{
return null;
}
return new SqlObjectDifferences<TSqlObject>(null, target, SqlObjectDifferenceType.MissingInSource, null);
}
else
{
if (target is null)
{
return new SqlObjectDifferences<TSqlObject>(source, null, SqlObjectDifferenceType.MissingInTarget, null);
}
}
var visitor = new SqlObjectComparer(source);
return (SqlObjectDifferences<TSqlObject>?)target.Accept(visitor);
}
private static IList<TDifferences> Compare<TDifferences, TSqlObject>(IReadOnlyList<TSqlObject> source, IReadOnlyList<TSqlObject> target, Func<TSqlObject, object> keySelector, Func<SqlObjectDifferences<TSqlObject>, TDifferences> factory)
where TSqlObject : SqlObject
where TDifferences : SqlObjectDifferences<TSqlObject>
{
var differences = Compare(source, target, keySelector);
var typedDifferences = new List<TDifferences>(differences.Count);
foreach (var difference in differences)
{
if (difference is not TDifferences)
{
typedDifferences.Add(factory(difference));
}
else
{
typedDifferences.Add((TDifferences)difference);
}
}
return typedDifferences;
}
private static IReadOnlyList<SqlObjectPropertyDifference> GetPropertyDifferences(params SqlObjectPropertyDifference?[] differences)
{
return differences.Where(d => d is not null).ToArray()!;
}
private static TSqlObject? Find<TSqlObject, TKey>(IReadOnlyList<TSqlObject> objects, Func<TSqlObject, TKey> keySelector, TKey value)
{
return objects.SingleOrDefault(o => Equals(keySelector(o), value));
}
private SqlObjectPropertyDifference? CompareProperty<TSqlObject>(TSqlObject target, Func<TSqlObject, object?> propertyValueForComparison, string name, Func<TSqlObject, object?>? propertyValueToDisplay = null)
where TSqlObject : SqlObject
{
var source = (TSqlObject)this.source;
if (propertyValueToDisplay is null)
{
propertyValueToDisplay = propertyValueForComparison;
}
var sourceValue = propertyValueForComparison(source);
var targetValue = propertyValueForComparison(target);
if (!Equals(sourceValue, targetValue))
{
return new SqlObjectPropertyDifference(name, propertyValueToDisplay(source), propertyValueToDisplay(target));
}
return null;
}
private SqlObjectDifferences? CreateDifferences<TSqlObject>(TSqlObject target, params SqlObjectPropertyDifference?[] differences)
where TSqlObject : SqlObject
{
var properties = differences.Where(d => d is not null).ToArray();
if (properties.Length == 0)
{
return null;
}
return new SqlObjectDifferences<TSqlObject>((TSqlObject)this.source, target, SqlObjectDifferenceType.Different, properties!);
}
}
}