-
Notifications
You must be signed in to change notification settings - Fork 8
/
DatabaseComparer.cs
1318 lines (1172 loc) · 60.2 KB
/
DatabaseComparer.cs
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region using statements
using DataJuggler.Net;
using DataJuggler.Core.UltimateHelper;
using System;
using System.Collections.Generic;
using System.Text;
using DataJuggler.Core.UltimateHelper.Objects;
using System.Linq;
#endregion
namespace DBCompare
{
#region class DatabaseComparer
/// <summary>
/// This class is used to comparison two Databases and reports and changes differences.
/// </summary>
public class DatabaseComparer
{
#region Private Variables
private Database sourceDatabase;
private Database targetDatabase;
private bool ignoreDiagramProcedures;
private string failedReason;
private bool ignoreIndexes;
#endregion
#region Parameterized Constructor(Database sourceDatabase, Database targetDatabase, bool ignoreDiagramProcedures bool, ignoreIndexes)
/// <summary>
/// Create a new instance of a SQLDatabaseComparer object.
/// </summary>
public DatabaseComparer(Database sourceDatabase, Database targetDatabase, bool ignoreDiagramProcedures, bool ignoreIndexes)
{
// if both databases exist
if (NullHelper.Exists(sourceDatabase, targetDatabase))
{
// set the properties
SourceDatabase = sourceDatabase;
TargetDatabase = targetDatabase;
// store the args
this.IgnoreDiagramProcedures = ignoreDiagramProcedures;
this.IgnoreIndexes = ignoreIndexes;
}
else
{
// Raise an error that a null database was passed in.
throw new Exception("Both databases must exist.");
}
}
#endregion
#region Methods
#region Compare(DataTable sourceTable, DataTable targetTable)
/// <summary>
/// This method compares two tables
/// </summary>
private SchemaComparison Compare(DataTable sourceTable, DataTable targetTable)
{
// initial value
SchemaComparison comparison = new SchemaComparison();
// verify both objects exist
if ((sourceTable != null) && (targetTable != null) && (sourceTable.Fields != null) && (targetTable.Fields != null))
{
// Compare the fields for this table
CompareTableFields(sourceTable, targetTable, ref comparison);
// Do a backwards comparison to see if there are any fields in the target table that are not in the source table.
DoBackwardsComparison(targetTable, sourceTable, ref comparison);
// should indexes be ignored
if (!IgnoreIndexes)
{
// Compare the indexes
CompareIndexesForTable(sourceTable, targetTable, ref comparison);
}
// Compare the CheckConstraints for a table
CompareCheckConstraintsForTable(sourceTable, targetTable, ref comparison);
// Update for Version 2 - Compare Foreign Keys
CompareForeignKeys(sourceTable, targetTable, ref comparison);
}
else if (NullHelper.IsNull(targetTable))
{
// if this table is a view
if (sourceTable.IsView)
{
// change the message to say view instead of table
// Add this table was not found to the schema differences collection
comparison.SchemaDifferences.Add("The view '" + sourceTable.Name + "' does not exist in the target database.");
}
else
{
// Add this table was not found to the schema differences collection
comparison.SchemaDifferences.Add("The table '" + sourceTable.Name + "' does not exist in the target database.");
}
}
else if (!ListHelper.HasOneOrMoreItems(sourceTable.Fields))
{
// if the sourceTable is a view
if (sourceTable.IsView)
{
// Add this table was not found to the schema differences collection
comparison.SchemaDifferences.Add("The source database view '" + sourceTable.Name + "' does not contain any fields.");
}
else
{
// Add this table was not found to the schema differences collection
comparison.SchemaDifferences.Add("The source database table '" + sourceTable.Name + "' does not contain any fields.");
}
}
else if (!ListHelper.HasOneOrMoreItems(targetTable.Fields))
{
// if the targetTable is a view
if (targetTable.IsView)
{
// Add this table was not found to the schema differences collection
comparison.SchemaDifferences.Add("The target database view '" + targetTable.Name + "' does not contain any fields.");
}
else
{
// Add this table was not found to the schema differences collection
comparison.SchemaDifferences.Add("The target database table '" + targetTable.Name + "' does not contain any fields.");
}
}
//if there are not any schema differences
if (comparison.SchemaDifferences.Count < 1)
{
// This is an equal
comparison.IsEqual = true;
}
// return value
return comparison;
}
#endregion
#region Compare()
/// <summary>
/// This method compares the two databases.
/// </summary>
public SchemaComparison Compare()
{
// initial value
SchemaComparison comparison = new SchemaComparison();
// if the SourceDatabase & TargetDatabase exist
if ((this.HasSourceDatabase) && (this.HasTargetDatabase))
{
// Do Comparison
comparison = DoComparison();
}
else if (!this.HasSourceDatabase)
{
// Add to the schema differences
comparison.SchemaDifferences.Add("Unable to load the 'Target Database.'");
}
else if (!this.HasTargetDatabase)
{
// Add to the schema differences
comparison.SchemaDifferences.Add("Unable to load the 'Source Database.'");
}
else
{
// Add to the schema differences
comparison.SchemaDifferences.Add("Unable to load the 'Source Database.' & 'Target Database.'");
}
// return value
return comparison;
}
#endregion
#region CompareCheckConstraints(CheckConstraint sourceCheckConstraint, CheckConstraint targetCheckConstraint)
/// <summary>
/// This method returns the Check Constraints
/// </summary>
private bool CompareCheckConstraints(CheckConstraint sourceCheckConstraint, CheckConstraint targetCheckConstraint)
{
// initial value
bool isValid = false;
// if both CheckConstraint objects exist
if (NullHelper.Exists(sourceCheckConstraint, targetCheckConstraint))
{
// set isValid to true
isValid = true;
// if the CheckClause is not the same
if (XmlPatternHelper.Decode(sourceCheckConstraint.CheckClause) != targetCheckConstraint.CheckClause)
{
// set isValid to false
isValid = false;
}
else if (sourceCheckConstraint.ColumnName != targetCheckConstraint.ColumnName)
{
// set isValid to false
isValid = false;
}
else if (sourceCheckConstraint.ConstraintName != targetCheckConstraint.ConstraintName)
{
// set isValid to false
isValid = false;
}
else if (sourceCheckConstraint.TableName != targetCheckConstraint.TableName)
{
// set isValid to false
isValid = false;
}
}
// return value
return isValid;
}
#endregion
#region CompareCheckConstraintsForTable(DataTable sourceTable, DataTable targetTable, ref SchemaComparison comparison)
/// <summary>
/// This method is sued to compare all the CheckConstraints for a table
/// </summary>
/// <param name="sourceTable"></param>
/// <param name="targetTable"></param>
/// <param name="comparison"></param>
public void CompareCheckConstraintsForTable(DataTable sourceTable, DataTable targetTable, ref SchemaComparison comparison)
{
// verify all the objects exist
if (NullHelper.Exists(sourceTable, targetTable, comparison))
{
// if the sourceTable Has Check Constraints
if (sourceTable.HasCheckConstraints)
{
// local
int number = 0;
string constraintName = "";
string schemaDifference = "";
// iterate the CheckConstraints in the sourceTable
foreach (CheckConstraint sourceCheckConstraint in sourceTable.CheckConstraints)
{
// if this name is the same
if (TextHelper.IsEqual(constraintName, sourceCheckConstraint.ConstraintName))
{
// Increment the value for number
number++;
}
else
{
// reset the value for number
number = 1;
}
// set the constraintName
constraintName = sourceCheckConstraint.ConstraintName;
// find the CheckConstraint in the target table
CheckConstraint targetCheckConstraint = targetTable.FindCheckConstraintByNameAndNumber(sourceCheckConstraint.ConstraintName, number);
// if the targetCheckConstraint exists
if (NullHelper.Exists(targetCheckConstraint))
{
// compare the checkConstraint
bool validCheckConstraints = CompareCheckConstraints(sourceCheckConstraint, targetCheckConstraint);
// if not a valid CheckConstraint
if (!validCheckConstraints)
{
// create the schemaDifference
schemaDifference = "The check constraint '" + sourceCheckConstraint.ConstraintName + "' in the target database table '" + targetTable.Name + "' is not valid.";
// see if this item is already in the comparison report (some constraints take up more than one field)
int index = comparison.SchemaDifferences.IndexOf(schemaDifference);
// if the index was not found
if (index < 0)
{
// Add an entry for this missing Check Constraint
comparison.SchemaDifferences.Add(schemaDifference);
}
}
}
else
{
// create the schemaDifference
schemaDifference = "The check constraint '" + sourceCheckConstraint.ConstraintName + "' was not found in the target database table '" + targetTable.Name + "'.";
// see if this item is already in the comparison report (some constraints take up more than one field)
int index = comparison.SchemaDifferences.IndexOf(schemaDifference);
// if the index was not found
if (index < 0)
{
// Add an entry for this missing Check Constraint
comparison.SchemaDifferences.Add(schemaDifference);
}
}
}
}
}
}
#endregion
#region CompareFields(DataField sourceField, DataField targetField, ref string failedDescription)
/// <summary>
/// This method compares two fields
/// </summary>
private bool CompareFields(DataField sourceField, DataField targetField, ref string failedDescription)
{
// initial value
bool fieldsMatch = false;
// if the sourceField and targetField both exist
if ((sourceField != null) && (targetField != null))
{
// default to true
fieldsMatch = true;
// if the data types do not match
if (sourceField.DataType != targetField.DataType)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'DataType' does not match.";
}
else if (sourceField.DBDataType != targetField.DBDataType)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription (fixing message above usually fixes this, but not always)
failedDescription = "The target field value for 'DB DataType' does not match.";
}
else if (sourceField.DBFieldName != targetField.DBFieldName)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription (This is the raw name for the field in the database, they should always match when comparing)
failedDescription = "The target field value for 'DBFieldName' does not match.";
}
else if (sourceField.DecimalPlaces != targetField.DecimalPlaces)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'Decimal Places' do not match";
}
else if (sourceField.IsAutoIncrement != targetField.IsAutoIncrement)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'IsAutoIncremnt' does not match.";
}
else if (sourceField.IsNullable != targetField.IsNullable)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'IsNullable' does not match.";
}
else if (sourceField.IsReadOnly != targetField.IsReadOnly)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'IsReadOnly' does not match.";
}
else if (sourceField.Precision != targetField.Precision)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'Precision' does not match: Decimal(Precision, Scale)";
}
else if (sourceField.PrimaryKey != targetField.PrimaryKey)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'PrimaryKey' does not match";
}
else if (sourceField.Scale != targetField.Scale)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'Scale' does not match: Decimal(Precision, Scale)";
}
else if(sourceField.Size != targetField.Size)
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'Size' does not match";
}
else if (TextHelper.Exists(sourceField.DefaultValue))
{
// if the targetField does not exist
if (!TextHelper.Exists(targetField.DefaultValue))
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'DefaultValue' is null.";
}
else
{
// replace out the parens
string sourceFieldVallue = sourceField.DefaultValue.Replace("(", "").Replace(")", "");
string targetFieldValue = targetField.DefaultValue.Replace("(", "").Replace(")", "");
// if the fields do not match
if (!TextHelper.IsEqual(sourceFieldVallue, targetFieldValue))
{
// the fields do not match
fieldsMatch = false;
// set the failedDescription
failedDescription = "The target field value for 'DefaultValue' does not match.";
}
}
}
}
// return value
return fieldsMatch;
}
#endregion
#region CompareForeignKeys(DataTable sourceTable, DataTable targetTable, ref SchemaComparison comparison)
/// <summary>
/// This method is used to compare the foreign keys for a table
/// </summary>
/// <param name="sourceTable"></param>
/// <param name="targetTable"></param>
/// <param name="comparison"></param>
public void CompareForeignKeys(DataTable sourceTable, DataTable targetTable, ref SchemaComparison comparison)
{
// local
ForeignKeyConstraint targetForeignKey = null;
// verify all the objects exist
if (NullHelper.Exists(sourceTable, targetTable, comparison))
{
// if both tables have foreign keys
if (ListHelper.HasOneOrMoreItems(sourceTable.ForeignKeys))
{
// iterate the foreignKeys for the sourceTable
foreach (ForeignKeyConstraint foreignKey in sourceTable.ForeignKeys)
{
// Attempt to find this foreign key in the target table
targetForeignKey = ForeignKeyConstraintHelper.FindForeignKey(foreignKey.Name, targetTable);
// if the foreign key was found in the target database
if (NullHelper.Exists(targetForeignKey))
{
// compare the table name (should always be true)
if (!TextHelper.IsEqual(foreignKey.Table, targetForeignKey.Table))
{
// Add a schemaDifference because the Table name does not match
comparison.SchemaDifferences.Add("The foreign key " + foreignKey.Name + " has a different value for Table name in the target database.");
}
else if (!TextHelper.IsEqual(foreignKey.ReferencedTable, targetForeignKey.ReferencedTable))
{
// Add a schemaDifference because the Referenced Table name does not match
comparison.SchemaDifferences.Add("The foreign key " + foreignKey.Name + " has a different value for Referenced Table name in the target database.");
}
else if (!TextHelper.IsEqual(foreignKey.ReferencedColumn, targetForeignKey.ReferencedColumn))
{
// Add a schemaDifference because the Referenced Column name does not match
comparison.SchemaDifferences.Add("The foreign key " + foreignKey.Name + " has a different value for Referenced Column name in the target database.");
}
}
else
{
// Add a schemaDifference the foreign key does not exist in the target database
comparison.SchemaDifferences.Add("The foreign key " + foreignKey.Name + " does not exist in the target database.");
}
}
}
}
}
#endregion
#region CompareFunctions(SchemaComparison comparison)
/// <summary>
/// This method returns the Functions
/// </summary>
private void CompareFunctions(SchemaComparison comparison)
{
// local
Function targetFunction = null;
// verify both objects exist
if ((this.HasSourceDatabase) && (this.HasTargetDatabase) && (comparison != null))
{
// if the SourceDatabase.Functions exists and has 1 or more functions
if (this.SourceDatabase.HasOneOrMoreFunctions)
{
// iterate the functions
foreach (Function function in this.SourceDatabase.Functions)
{
// Attempt to find the target function
targetFunction = FindFunction(function.Name, this.TargetDatabase.Functions);
// if the targetFunction exists
if (targetFunction != null)
{
// set the text of each
string sourceFunctionText = function.Text;
// set the targetProcedureText
string targetFunctionText = targetFunction.Text;
// if the text was found for both functions
if (TextHelper.Exists(sourceFunctionText, targetFunctionText))
{
// replace out any double spaces and new line characters and replace commas with spaces so fields parse as words
sourceFunctionText = sourceFunctionText.Replace("\r\n", " ").Replace(" ", " ").Replace(",", " ").Replace("dbo", "").Replace("[", "").Replace("]", "");
targetFunctionText = targetFunctionText.Replace("\r\n", " ").Replace(" ", " ").Replace(",", " ").Replace("dbo", "").Replace("[", "").Replace("]", "");
// Compare Stored Procedure Text
CompareFunctionText(function.Name, sourceFunctionText, targetFunctionText, comparison);
}
else
{
// This stored procedure is not valid
comparison.SchemaDifferences.Add("The function '" + function.Name + "' is not valid.");
}
}
else
{
// This stored procedure is not valid
comparison.SchemaDifferences.Add("The function '" + function.Name + "' was not found.");
}
}
}
}
}
#endregion
#region CompareFunctionText(string sourceFunctionName, string sourceFunctionText, string targetFunctionText, SchemaComparison comparison)
/// <summary>
/// This method compares the text of two SQL Functions
/// </summary>
private void CompareFunctionText(string sourceFunctionName, string sourceFunctionText, string targetFunctionText, SchemaComparison comparison)
{
// locals
bool isEqual = true; // (Default to true)
string failedReason = "";
string sourceWordText = "";
string targetWordText = "";
int wordCompare = 0;
// if the sourceProcedureText exists and the targetProcedureText exists and the comparison object exists
if ((TextHelper.Exists(sourceFunctionText, targetFunctionText)) && (comparison != null))
{
// Get the words for the source procedure text
List<Word> sourceWords = WordParser.GetWords(sourceFunctionText);
// Get the words for the target procedure text
List<Word> targetWords = WordParser.GetWords(targetFunctionText);
// if both sets of words exist
if ((sourceWords != null) && (targetWords != null))
{
// if the number of words is different
if (sourceWords.Count != targetWords.Count)
{
// not equal
isEqual = false;
// set the failedReason
failedReason = "The function '" + sourceFunctionName + "' is not valid.";
}
else
{
// check the text of each word
for (int x = 0; x < sourceWords.Count; x++)
{
// get the text of this word
sourceWordText = sourceWords[x].Text.ToLower();
targetWordText = targetWords[x].Text.ToLower();
// compare this word
wordCompare = String.Compare(sourceWordText, targetWordText);
// if the words did not match
if (wordCompare != 0)
{
// not equal
isEqual = false;
// set the failedReason
failedReason = "The function '" + sourceFunctionName + "' is not valid.";
}
}
}
}
else if (sourceWords != null)
{
// not equal
isEqual = false;
// set the failedReason
failedReason = "The source function '" + sourceFunctionName + "' could not be analyzed.";
}
else if (targetWords != null)
{
// not equal
isEqual = false;
// set the failedReason
failedReason = "The target function '" + sourceFunctionName + "' could not be analyzed.";
}
// if the text is not the same
if (!isEqual)
{
// This stored procedure is not valid
comparison.SchemaDifferences.Add("The function '" + sourceFunctionName + "' is not valid.");
}
}
}
#endregion
#region CompareIndexes(DataIndex sourceIndex, DataIndex targetIndex)
/// <summary>
/// This method returns the Indexes
/// </summary>
private bool CompareIndexes(DataIndex sourceIndex, DataIndex targetIndex)
{
// initial value
bool isValid = false;
// if both indexes exist
if (NullHelper.Exists(sourceDatabase, targetIndex))
{
// set isValid to true
isValid = true;
// if the names do not match
if (sourceIndex.Name != targetIndex.Name)
{
// not valid
isValid = false;
}
else if (sourceIndex.IsUniqueConstraint != targetIndex.IsUniqueConstraint)
{
// not valid
isValid = false;
}
else if (sourceIndex.IsPrimary != targetIndex.IsPrimary)
{
// not valid
isValid = false;
}
else if (sourceIndex.Clustered != targetIndex.Clustered)
{
// not valid
isValid = false;
}
else if (sourceIndex.IgnoreDuplicateKey != targetIndex.IgnoreDuplicateKey)
{
// not valid
isValid = false;
}
}
else if (NullHelper.Exists(sourceDatabase))
{
// Not valid
}
else if (NullHelper.Exists(targetIndex))
{
// Not valid
}
// return value
return isValid;
}
#endregion
#region CompareIndexesForTable(DataTable sourceTable, DataTable targetTable, ref SchemaComparison comparison)
/// <summary>
/// This method is used to compare all the indees for agiven table
/// </summary>
/// <param name="sourceTable"></param>
/// <param name="targetTable"></param>
/// <param name="comparison"></param>
public void CompareIndexesForTable(DataTable sourceTable, DataTable targetTable, ref SchemaComparison comparison)
{
// see if al
if (NullHelper.Exists(sourceTable, targetTable, comparison))
{
// If the sourceTable has an indexes collection
if (sourceTable.HasIndexes)
{
// iterate the indexes in the sourceTable
foreach (DataIndex sourceIndex in sourceTable.Indexes)
{
// find the index in the target table
DataIndex targetIndex = targetTable.FindIndexByName(sourceIndex.Name);
// if the targetIndex exists
if (NullHelper.Exists(targetIndex))
{
// compare the index
bool validIndex = CompareIndexes(sourceIndex, targetIndex);
// if not a valid index
if (!validIndex)
{
// Add an entry for this index
comparison.SchemaDifferences.Add("The index '" + sourceIndex.Name + "' in the target database table '" + targetTable.Name + "' is not valid.");
}
}
else
{
// Add an entry for this index
comparison.SchemaDifferences.Add("The index '" + sourceIndex.Name + "' was not found in the target database table '" + targetTable.Name + "'.");
}
}
}
}
}
#endregion
#region CompareStoredProcedures(StoredProcedure sourceProcedure, Database database, SchemaComparison comparison)
/// <summary>
/// This method returns the Stored Procedures
/// </summary>
private void CompareStoredProcedures(StoredProcedure sourceProcedure, Database database, SchemaComparison comparison)
{
// locals
StoredProcedure targetProcedure = null;
// verify the sourceProcedure and the database both exist
if ((sourceProcedure != null) && (database != null) && (comparison != null))
{
// if this procedure should not be skipped
if (!SkipProcedure(sourceProcedure.ProcedureName))
{
// attempt to find
targetProcedure = database.StoredProcedures.FirstOrDefault(x => x.ProcedureName == sourceProcedure.ProcedureName);
// if the targetProcedure exists
if (targetProcedure != null)
{
// set the text of each
string sourceProcedureText = sourceProcedure.Text;
// set the targetProcedureText
string targetProcedureText = targetProcedure.Text;
// if the text was found for both procedures
if (TextHelper.Exists(sourceProcedureText, targetProcedureText))
{
// replace out any double spaces and new line characters and replace commas with spaces so fields parse as words
sourceProcedureText = sourceProcedureText.Replace("\r\n", " ").Replace(" ", " ").Replace(",", " ");
targetProcedureText = targetProcedureText.Replace("\r\n", " ").Replace(" ", " ").Replace(",", " ");
// Compare Stored Procedure Text
CompareStoredProcedureText(sourceProcedure.ProcedureName, sourceProcedureText, targetProcedureText, comparison);
}
else if (TextHelper.Exists(sourceProcedureText))
{
// This stored procedure is not valid
comparison.SchemaDifferences.Add("The stored procedure '" + sourceProcedure.ProcedureName + "' was not found in the target database.");
}
else if (TextHelper.Exists(targetProcedureText))
{
// This stored procedure is not valid
comparison.SchemaDifferences.Add("The stored procedure '" + sourceProcedure.ProcedureName + "' was not found in the source database.");
}
}
else
{
// This stored procedure is not valid
comparison.SchemaDifferences.Add("The procedure '" + sourceProcedure.ProcedureName + "' was not found.");
}
}
}
}
#endregion
#region CompareStoredProcedures(SchemaComparison comparison)
/// <summary>
/// This method compares the StoredProcedures from the SourceDatabase against the
/// stored procedures from the TargetDatabase.
/// </summary>
private void CompareStoredProcedures(SchemaComparison comparison)
{
// if the SourceDatabase and the TargetDatabase exist
if ((this.HasSourceDatabase) && (this.HasTargetDatabase) && (comparison != null))
{
// if there are one or more stored procedures
if (this.SourceDatabase.HasOneOrMoreStoredProcedures)
{
// if the target database has at least one stored procedure
if (this.TargetDatabase.HasOneOrMoreStoredProcedures)
{
// do compareisons here
// iterate the stored procedures
foreach (StoredProcedure sourceProcedure in this.SourceDatabase.StoredProcedures)
{
// Compare stored procedures
CompareStoredProcedures(sourceProcedure, this.TargetDatabase, comparison);
}
// Update 5.14.2013: Check for extra stored procedures in the TargetDatabase
// iterate the stored procedures
foreach (StoredProcedure targetProcedure in this.TargetDatabase.StoredProcedures)
{
// if this Procedure Should Not Be Skipped
if (!SkipProcedure(targetProcedure.ProcedureName))
{
// attempt to
StoredProcedure sourceProcedure = this.SourceDatabase.StoredProcedures.FirstOrDefault(x => x.ProcedureName == targetProcedure.ProcedureName);
// if the sourceProcedure was not found
if (sourceProcedure == null)
{
// if the comparisionObject hasSchemaDifferences object
if (comparison.HasSchemaDifferences)
{
// Show this extra procedure
comparison.SchemaDifferences.Add("The target database contains an extra stored procedure: '" + targetProcedure.ProcedureName + "'.");
}
}
}
}
}
else
{
// The TargetDatabase does not have any stored procedures
comparison.SchemaDifferences.Add("The target database does not have any stored procedures.");
}
}
}
}
#endregion
#region CompareStoredProcedureText(string sourceProcedureName, string sourceProcedureText, string targetProcedureText, SchemaComparison comparison)
/// <summary>
/// This method returns the Stored Procedure Text
/// </summary>
private void CompareStoredProcedureText(string sourceProcedureName, string sourceProcedureText, string targetProcedureText, SchemaComparison comparison)
{
// locals
bool isEqual = true; // (Default to true)
string failedReason = "";
string sourceWordText = "";
string targetWordText = "";
int wordCompare = 0;
// if the sourceProcedureText exists and the targetProcedureText exists and the comparison object exists
if ((TextHelper.Exists(sourceProcedureText, targetProcedureText)) && (comparison != null))
{
// Get the words for the source procedure text
List<Word> sourceWords = WordParser.GetWords(sourceProcedureText);
// Get the words for the target procedure text
List<Word> targetWords = WordParser.GetWords(targetProcedureText);
// if both sets of words exist
if ((sourceWords != null) && (targetWords != null))
{
// if the number of words is different
if (sourceWords.Count != targetWords.Count)
{
// not equal
isEqual = false;
// set the failedReason
failedReason = "The procedure '" + sourceProcedureName + "' is not valid.";
}
else
{
// check the text of each word
for (int x = 0; x < sourceWords.Count; x++)
{
// get the text of this word
sourceWordText = sourceWords[x].Text.ToLower();
targetWordText = targetWords[x].Text.ToLower();
// compare this word
wordCompare = String.Compare(sourceWordText, targetWordText);
// if the words did not match
if (wordCompare != 0)
{
// not equal
isEqual = false;
// set the failedReason
failedReason = "The procedure '" + sourceProcedureName + "' is not valid.";
}
}
}
}
else if (sourceWords != null)
{
// not equal
isEqual = false;
// set the failedReason
failedReason = "The source stored procedure '" + sourceProcedureName + "' could not be analyzed.";
}
else if (targetWords != null)
{
// not equal
isEqual = false;
// set the failedReason
failedReason = "The target stored procedure '" + sourceProcedureName + "' could not be analyzed.";
}
// if the text is not the same
if (!isEqual)
{
// This stored procedure is not valid
comparison.SchemaDifferences.Add("The procedure '" + sourceProcedureName + "' is not valid.");
}
}
}
#endregion
#region CompareTableFields(DataTable sourceTable, DataTable targetTable, ref SchemaComparison comparison)
/// <summary>
/// This method compares the fields of two tables
/// </summary>
/// <param name="sourceTable"></param>
/// <param name="targetTable"></param>
/// <param name="comparison"></param>
public void CompareTableFields(DataTable sourceTable, DataTable targetTable, ref SchemaComparison comparison)
{
// locals
DataField targetField = null;
string failedDescription = "";
// verify all 3 objects exist
if (NullHelper.Exists(sourceDatabase, targetTable, comparison))
{
// compare each field
foreach (DataField sourceField in sourceTable.Fields)
{
// attempt to find the targetField
targetField = SQLDatabaseConnector.FindField(targetTable.Fields, sourceField.FieldName);
// if the targetField exists
if (targetField != null)
{
// Compare the fields
bool validField = CompareFields(sourceField, targetField, ref failedDescription);
// if the field is not valid
if (!validField)
{
// Add this table was not found to the schema differences collection
comparison.SchemaDifferences.Add("The field '" + sourceTable.Name + "." + sourceField.FieldName + "' is not valid: " + failedDescription);
}
}
else
{
// Add this table was not found to the schema differences collection
comparison.SchemaDifferences.Add("The field '" + sourceTable.Name + "." + sourceField.FieldName + "' was not found in the target database.");
}
}
}
}
#endregion
#region CompareTables()
/// <summary>
/// This method compares the tables from the SourceDatabase and the TargetDatabase.
/// </summary>
private SchemaComparison CompareTables()