-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPrimaryKeyCollisionIsolationMutilationTests.cs
926 lines (718 loc) · 40.2 KB
/
PrimaryKeyCollisionIsolationMutilationTests.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
using FAnsi;
using FAnsi.Discovery;
using NUnit.Framework;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.Curation.Data.DataLoad;
using Rdmp.Core.Curation.Data.EntityNaming;
using Rdmp.Core.DataLoad.Engine.DatabaseManagement.EntityNaming;
using Rdmp.Core.DataLoad.Engine.Job;
using Rdmp.Dicom.PipelineComponents;
using Rdmp.Core.ReusableLibraryCode.Checks;
using Rdmp.Core.ReusableLibraryCode.DataAccess;
using System;
using System.Data;
using System.Linq;
using Tests.Common;
using TypeGuesser;
namespace Rdmp.Dicom.Tests.Unit;
class PrimaryKeyCollisionIsolationMutilationTests : DatabaseTests
{
[TestCase(DatabaseType.MicrosoftSQLServer)]
[TestCase(DatabaseType.MySql)]
[TestCase(DatabaseType.PostgreSql)]
public void Test_IsolateSingleTable_Check(DatabaseType dbType)
{
var db = GetCleanedServer(dbType);
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Rows.Add("Fish", 12);
var tbl = db.CreateTable("CoolTable", dt);
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tbl, out var tableInfoCreated, out var columnInfosCreated);
//lie about the primary key status
var a = columnInfosCreated.Single(c => c.GetRuntimeName().Equals("A"));
a.IsPrimaryKey = true;
a.SaveToDatabase();
var mutilator = GetMutilator(db, tableInfoCreated);
//first time no tables exist so they must be created
mutilator.Check(new AcceptAllCheckNotifier());
var isolationTable = db.ExpectTable("CoolTable_Isolation");
Assert.Multiple(() =>
{
Assert.That(isolationTable.Exists());
Assert.That(isolationTable.DiscoverColumns().Any(c => c.GetRuntimeName().Equals("A")));
Assert.That(isolationTable.DiscoverColumns().Any(c => c.GetRuntimeName().Equals("hic_dataLoadRunID")));
});
//the check should pass second time without needing to accept any fixes
mutilator.Check(ThrowImmediatelyCheckNotifier.Quiet);
}
private PrimaryKeyCollisionIsolationMutilation GetMutilator(DiscoveredDatabase db, params ITableInfo[] tableInfoCreated)
{
//tell the mutilator to resolve the primary key collision on column A by isolating the rows
var mutilation = new PrimaryKeyCollisionIsolationMutilation { TablesToIsolate = tableInfoCreated.Cast<TableInfo>().ToArray() };
//tell the mutilator to set up isolation into the provided database
var serverPointer = new ExternalDatabaseServer(CatalogueRepository, "Isolation Db", null);
serverPointer.SetProperties(db);
mutilation.IsolationDatabase = serverPointer;
return mutilation;
}
[TestCase(DatabaseType.MicrosoftSQLServer)]
[TestCase(DatabaseType.MySql)]
[TestCase(DatabaseType.PostgreSql)]
public void Test_IsolateSingleTable_Duplication(DatabaseType dbType)
{
var db = GetCleanedServer(dbType);
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Rows.Add("Fish", 1);
dt.Rows.Add("Fish", 2);
dt.Rows.Add("Fish", 3);
dt.Rows.Add("Frank", 2);
dt.Rows.Add("Candy", 2);
var tbl = db.CreateTable("MyCoolTable2", dt);
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tbl, out var tableInfoCreated, out var columnInfosCreated);
//lie about the primary key status
var a = columnInfosCreated.Single(c => c.GetRuntimeName().Equals("A"));
a.IsPrimaryKey = true;
a.SaveToDatabase();
var mutilator = GetMutilator(db, tableInfoCreated);
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, RdmpMockFactory.Mock_INameDatabasesAndTablesDuringLoads(db, "MyCoolTable2"));
var job = new ThrowImmediatelyDataLoadJob(config, tableInfoCreated);
mutilator.Initialize(db, LoadStage.AdjustRaw);
mutilator.Mutilate(job);
using var dt2 = tbl.GetDataTable();
Assert.That(dt2.Rows, Has.Count.EqualTo(2));
using var dtIsolation = tbl.Database.ExpectTable("MyCoolTable2_Isolation").GetDataTable();
Assert.That(dtIsolation.Rows, Has.Count.EqualTo(3));
}
[TestCase(".[dbo].", true)]
[TestCase(".[dbo].", false)]
[TestCase(".dbo.", true)]
[TestCase(".dbo.", false)]
[TestCase("..", true)]
[TestCase("..", false)]
public void Test_IsolateSingleTableWithSchema_Duplication(string schemaExpression, bool includeQualifiers)
{
var db = GetCleanedServer(DatabaseType.MicrosoftSQLServer);
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Rows.Add("Fish", 1);
dt.Rows.Add("Fish", 2);
dt.Rows.Add("Fish", 3);
dt.Rows.Add("Frank", 2);
dt.Rows.Add("Candy", 2);
var tbl = db.CreateTable("MyCoolTable2", dt);
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tbl, out var tableInfoCreated, out var columnInfosCreated);
var syntax = db.Server.GetQuerySyntaxHelper();
tableInfoCreated.Name =
(includeQualifiers ? syntax.EnsureWrapped(db.GetRuntimeName()) : db.GetRuntimeName())
+ schemaExpression +
(includeQualifiers ? syntax.EnsureWrapped(tbl.GetRuntimeName()) : tbl.GetRuntimeName());
tableInfoCreated.SaveToDatabase();
foreach (var column in columnInfosCreated)
{
column.Name =
$"{tableInfoCreated.Name}.{(includeQualifiers ? syntax.EnsureWrapped(column.GetRuntimeName()) : column.GetRuntimeName())}";
column.SaveToDatabase();
}
//lie about the primary key status
var a = columnInfosCreated.Single(c => c.GetRuntimeName().Equals("A"));
a.IsPrimaryKey = true;
a.SaveToDatabase();
var mutilator = GetMutilator(db, tableInfoCreated);
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, RdmpMockFactory.Mock_INameDatabasesAndTablesDuringLoads(db, "MyCoolTable2"));
var job = new ThrowImmediatelyDataLoadJob(config, tableInfoCreated);
mutilator.Initialize(db, LoadStage.AdjustRaw);
mutilator.Mutilate(job);
using var dt2 = tbl.GetDataTable();
Assert.That(dt2.Rows, Has.Count.EqualTo(2));
using var dtIsolation = tbl.Database.ExpectTable("MyCoolTable2_Isolation").GetDataTable();
Assert.That(dtIsolation.Rows, Has.Count.EqualTo(3));
}
[TestCase(DatabaseType.MicrosoftSQLServer)]
[TestCase(DatabaseType.MySql)]
[TestCase(DatabaseType.PostgreSql)]
public void Test_IsolateTwoTables_Duplication(DatabaseType dbType)
{
var db = GetCleanedServer(dbType);
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("SeriesInstanceUID");
dt.Columns.Add("Seriesly");
dt.Rows.Add("1.2.3", 1); //collision with children (see d2)
dt.Rows.Add("1.2.3", 2);
dt.Rows.Add("2.3.4", 3); //collision with no children
dt.Rows.Add("2.3.4", 4);
dt.Rows.Add("5.2.1", 2); //unique
dt.Rows.Add("9.9.9", 2); // not a collision but should be deleted because of child collision
//Create a table in 'RAW' (has no constraints)
using var dt2 = new DataTable();
dt2.Columns.Add("SeriesInstanceUID");
dt2.Columns.Add("SOPInstanceUID");
dt2.Columns.Add("PatientName");
dt2.Rows.Add("1.2.3", "1.2.1", "Frank");
//these are not a collision because SOPInstanceUID is the pk for this table but they should also be isolated because their series contains duplication
dt2.Rows.Add("1.2.3", "1.2.2", "Dave");
dt2.Rows.Add("5.2.1", "1.1.1", "jjj"); //some more normal records in child
dt2.Rows.Add("5.2.1", "1.1.2", "fff");
dt2.Rows.Add("5.2.1", "1.1.3", "kkk");
dt2.Rows.Add("9.9.9", "1.1.5", "zkk"); //collision on SOPInstanceUID ("1.1.5")
dt2.Rows.Add("9.9.9", "1.1.5", "zzb");
var tblParent = db.CreateTable("Parent", dt, new[]
{
new DatabaseColumnRequest("SeriesInstanceUID",new DatabaseTypeRequest(typeof(string)))
});
var tblChild = db.CreateTable("Child", dt2, new[]
{
new DatabaseColumnRequest("SeriesInstanceUID",new DatabaseTypeRequest(typeof(string))),
new DatabaseColumnRequest("SOPInstanceUID",new DatabaseTypeRequest(typeof(string)))
});
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tblParent, out var parentTableInfo, out var parentColumnInfosCreated);
Import(tblChild, out var childTableInfo, out var childColumnInfosCreated);
//make sure RDMP knows joins start with this table
parentTableInfo.IsPrimaryExtractionTable = true;
parentTableInfo.SaveToDatabase();
//lie about the primary key statuses
var seriesInstanceUIdCol =
parentColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("SeriesInstanceUID"));
seriesInstanceUIdCol.IsPrimaryKey = true;
seriesInstanceUIdCol.SaveToDatabase();
var sopInstanceUIdCol = childColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("SOPInstanceUID"));
sopInstanceUIdCol.IsPrimaryKey = true;
sopInstanceUIdCol.SaveToDatabase();
//Create a new mutilator for these two tables
var mutilator = GetMutilator(db, parentTableInfo, childTableInfo);
//checking should fail because it doesn't know how to join tables
var ex = Assert.Throws<Exception>(() => mutilator.Check(new AcceptAllCheckNotifier()));
Assert.That(ex.Message, Does.Contain("join")); //should be complaining about missing join infos
//tell RDMP about how to join tables
_ = new JoinInfo(CatalogueRepository, childColumnInfosCreated.Single(
c => c.GetRuntimeName().Equals("SeriesInstanceUID")),
parentColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("SeriesInstanceUID")),
ExtractionJoinType.Right, null);
//now that we have a join it should pass checks
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, new ReturnSameString());
var job = new ThrowImmediatelyDataLoadJob(config, parentTableInfo, childTableInfo);
mutilator.Initialize(db, LoadStage.AdjustRaw);
mutilator.Mutilate(job);
//parent should now only have "5.2.1"
using var dtParent = parentTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtParent.Rows, Has.Count.EqualTo(1));
//isolation should have 5 ("1.2.3", "2.3.4" and "9.9.9")
using var dtParentIsolation = db.ExpectTable("Parent_Isolation").GetDataTable();
Assert.That(dtParentIsolation.Rows, Has.Count.EqualTo(5));
//child table should now only have 3 ("1.1.1", "1.1.2" and "1.1.3")
using var dtChild = childTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtChild.Rows, Has.Count.EqualTo(3));
//child isolation table should have 4:
/*
"1.2.3","1.2.1","Frank"
"1.2.3","1.2.2","Dave"
"9.9.9", "1.1.5", "zkk"
"9.9.9", "1.1.5", "zzb"
*/
using var dtChildIsolation = db.ExpectTable("Child_Isolation").GetDataTable();
Assert.That(dtChildIsolation.Rows, Has.Count.EqualTo(4));
}
[TestCase(DatabaseType.MicrosoftSQLServer, false)]
[TestCase(DatabaseType.MySql, false)]
[TestCase(DatabaseType.MicrosoftSQLServer, true)]
[TestCase(DatabaseType.MySql, true)]
[TestCase(DatabaseType.PostgreSql, false)]
[TestCase(DatabaseType.PostgreSql, true)]
public void Test_IsolateTwoTables_MultipleConflictingColumns(DatabaseType dbType, bool whitespace)
{
var db = GetCleanedServer(dbType);
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("Pk");
dt.Columns.Add("OtherCol");
dt.Rows.Add("A", 1); //these are colliding on pk "A" with different values of "OtherCol"
dt.Rows.Add(whitespace ? "A " : "A", 2);
//Create a table in 'RAW' (has no constraints)
using var dt2 = new DataTable();
dt2.Columns.Add("Pk2");
dt2.Columns.Add("Fk");
dt2.Columns.Add("OtherCol2");
dt2.Columns.Add("OtherCol3");
dt2.Rows.Add(whitespace ? "X " : "X", "A", "FF", DBNull.Value); //these are colliding on pk "X" with different values of "OtherCol2"
dt2.Rows.Add("X", whitespace ? "A " : "A", "GG", DBNull.Value);
dt2.Rows.Add(whitespace ? "X " : "X", "A", "FF", "HH"); //these are colliding on pk "X" with different values of "OtherCol2"
dt2.Rows.Add("X", whitespace ? "A " : "A", "GG", "HH");
var tblParent = db.CreateTable("Parent", dt);
var tblChild = db.CreateTable("Child", dt2);
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tblParent, out var parentTableInfo, out var parentColumnInfosCreated);
Import(tblChild, out var childTableInfo, out var childColumnInfosCreated);
//make sure RDMP knows joins start with this table
parentTableInfo.IsPrimaryExtractionTable = true;
parentTableInfo.SaveToDatabase();
//lie about the primary key statuses (to simulate live)
var seriesInstanceUIdCol =
parentColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk"));
seriesInstanceUIdCol.IsPrimaryKey = true;
seriesInstanceUIdCol.SaveToDatabase();
var sopInstanceUIdCol = childColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk2"));
sopInstanceUIdCol.IsPrimaryKey = true;
sopInstanceUIdCol.SaveToDatabase();
//Create a new mutilator for these two tables
var mutilator = GetMutilator(db, parentTableInfo, childTableInfo);
//tell RDMP about how to join tables
_=new JoinInfo(CatalogueRepository, childColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Fk")),
parentColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Pk")),
ExtractionJoinType.Right, null);
//now that we have a join it should pass checks
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, new ReturnSameString());
var job = new ThrowImmediatelyDataLoadJob(config, parentTableInfo, childTableInfo);
mutilator.Initialize(db, LoadStage.AdjustRaw);
mutilator.Mutilate(job);
//parent should now be empty
using var dtParent = parentTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtParent.Rows, Is.Empty);
//isolation should have 2
using var dtParentIsolation = db.ExpectTable("Parent_Isolation").GetDataTable();
Assert.That(dtParentIsolation.Rows, Has.Count.EqualTo(2));
//child table should also be empty
using var dtChild = childTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtChild.Rows, Is.Empty);
//child isolation table should have 4:
using var dtChildIsolation = db.ExpectTable("Child_Isolation").GetDataTable();
Assert.That(dtChildIsolation.Rows, Has.Count.EqualTo(4));
}
[TestCase(DatabaseType.MicrosoftSQLServer)]
[TestCase(DatabaseType.MySql)]
[TestCase(DatabaseType.PostgreSql)]
public void Test_IsolateTwoTables_IntKeys(DatabaseType dbType)
{
/***************************************
* Parent(Pk) Child (Pk2,Fk,OtherCol)
* 4 -> 8,4,1
* ⬍ (collision causes pk 4 to be migrated)
* 8,4,2
* 5 -> 9,5,1 (good record with no collisions anywhere)
**********************************/
var db = GetCleanedServer(dbType);
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("Pk");
dt.Columns.Add("OtherCol");
dt.Rows.Add(4, 1);
dt.Rows.Add(5, 2);
//Create a table in 'RAW' (has no constraints)
using var dt2 = new DataTable();
dt2.Columns.Add("Pk2");
dt2.Columns.Add("Fk");
dt2.Columns.Add("OtherCol");
dt2.Rows.Add(8, 4, 1); //these are colliding on pk 8 which will ship full hierarchy of parent pk 4 to the isolation table
dt2.Rows.Add(8, 4, 2);
dt2.Rows.Add(9, 5, 1); //good record with no collisions, should not be deleted!
var tblParent = db.CreateTable("Parent", dt);
var tblChild = db.CreateTable("Child", dt2);
//make sure FAnsi made an int column
Assert.That(tblParent.DiscoverColumn("Pk").GetGuesser().Guess.CSharpType, Is.EqualTo(typeof(int)));
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tblParent, out var parentTableInfo, out var parentColumnInfosCreated);
Import(tblChild, out var childTableInfo, out var childColumnInfosCreated);
//make sure RDMP knows joins start with this table
parentTableInfo.IsPrimaryExtractionTable = true;
parentTableInfo.SaveToDatabase();
//lie about the primary key statuses (to simulate live)
var seriesInstanceUIdCol =
parentColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk"));
seriesInstanceUIdCol.IsPrimaryKey = true;
seriesInstanceUIdCol.SaveToDatabase();
var sopInstanceUIdCol = childColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk2"));
sopInstanceUIdCol.IsPrimaryKey = true;
sopInstanceUIdCol.SaveToDatabase();
//Create a new mutilator for these two tables
var mutilator = GetMutilator(db, parentTableInfo, childTableInfo);
//tell RDMP about how to join tables
_=new JoinInfo(CatalogueRepository, childColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Fk")),
parentColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Pk")),
ExtractionJoinType.Right, null);
//now that we have a join it should pass checks
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, new ReturnSameString());
var job = new ThrowImmediatelyDataLoadJob(config, parentTableInfo, childTableInfo);
mutilator.Initialize(db, LoadStage.AdjustRaw);
mutilator.Mutilate(job);
//parent should now have 1
using var dtParent = parentTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtParent.Rows, Has.Count.EqualTo(1));
//isolation should have 1
using var dtParentIsolation = db.ExpectTable("Parent_Isolation").GetDataTable();
Assert.That(dtParentIsolation.Rows, Has.Count.EqualTo(1));
//child table should have the good 1
using var dtChild = childTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtChild.Rows, Has.Count.EqualTo(1));
//child isolation table should have 2:
using var dtChildIsolation = db.ExpectTable("Child_Isolation").GetDataTable();
Assert.That(dtChildIsolation.Rows, Has.Count.EqualTo(2));
}
[TestCase(DatabaseType.MicrosoftSQLServer)]
[TestCase(DatabaseType.MySql)]
[TestCase(DatabaseType.PostgreSql)]
public void Test_IsolateTwoTables_MultipleCollidingChildren(DatabaseType dbType)
{
/***************************************
* Parent(Pk) Child (Pk2,Fk,OtherCol)
* A -> X,A,1
* ⬍ (collision causes A delete)
* X,A,2
* -> Y,A,1
* ⬍ (collision causes repeated attempt to delete A)
* Y,A,2
*
**********************************/
var db = GetCleanedServer(dbType);
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("Pk");
dt.Columns.Add("OtherCol");
dt.Rows.Add("A", 1);
//Create a table in 'RAW' (has no constraints)
using var dt2 = new DataTable();
dt2.Columns.Add("Pk2");
dt2.Columns.Add("Fk");
dt2.Columns.Add("OtherCol");
dt2.Rows.Add("X", "A", 1); //these are colliding on pk "X" which will ship A to the isolation table
dt2.Rows.Add("X", "A", 2);
dt2.Rows.Add("Y", "A", 2); //these are colliding on pk "Y" but also reference A (which has already been shipped to isolation)
dt2.Rows.Add("Y", "A", 1);
var tblParent = db.CreateTable("Parent", dt);
var tblChild = db.CreateTable("Child", dt2);
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tblParent, out var parentTableInfo, out var parentColumnInfosCreated);
Import(tblChild, out var childTableInfo, out var childColumnInfosCreated);
//make sure RDMP knows joins start with this table
parentTableInfo.IsPrimaryExtractionTable = true;
parentTableInfo.SaveToDatabase();
//lie about the primary key statuses (to simulate live)
var seriesInstanceUIdCol =
parentColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk"));
seriesInstanceUIdCol.IsPrimaryKey = true;
seriesInstanceUIdCol.SaveToDatabase();
var sopInstanceUIdCol = childColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk2"));
sopInstanceUIdCol.IsPrimaryKey = true;
sopInstanceUIdCol.SaveToDatabase();
//Create a new mutilator for these two tables
var mutilator = GetMutilator(db, parentTableInfo, childTableInfo);
//tell RDMP about how to join tables
_=new JoinInfo(CatalogueRepository, childColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Fk")),
parentColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Pk")),
ExtractionJoinType.Right, null);
//now that we have a join it should pass checks
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, new ReturnSameString());
var job = new ThrowImmediatelyDataLoadJob(config, parentTableInfo, childTableInfo);
mutilator.Initialize(db, LoadStage.AdjustRaw);
mutilator.Mutilate(job);
//parent should now have 0...
using var dtParent = parentTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtParent.Rows, Is.Empty);
//isolation should have 1
using var dtParentIsolation = db.ExpectTable("Parent_Isolation").GetDataTable();
Assert.That(dtParentIsolation.Rows, Has.Count.EqualTo(1));
//child table should also be empty
using var dtChild = childTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtChild.Rows, Is.Empty);
//child isolation table should have 4:
using var dtChildIsolation = db.ExpectTable("Child_Isolation").GetDataTable();
Assert.That(dtChildIsolation.Rows, Has.Count.EqualTo(4));
}
[TestCase(DatabaseType.MicrosoftSQLServer)]
[TestCase(DatabaseType.MySql)]
[TestCase(DatabaseType.PostgreSql)]
public void Test_IsolateTables_Orphans(DatabaseType dbType)
{
var db = GetCleanedServer(dbType);
/***************************************
* Parent(Pk) Child (Pk2,Fk,OtherCol)
* A <> X,B,FF
* ⬍ (collision on pk X causes lookup of Fk B which does not exist in Parent)
* X,B,GG
*
**********************************/
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("Pk");
dt.Columns.Add("OtherCol");
dt.Rows.Add("A", 1); //these are colliding on pk "A" with different values of "OtherCol"
dt.Rows.Add("A", 2);
//Create a table in 'RAW' (has no constraints)
using var dt2 = new DataTable();
dt2.Columns.Add("Pk2");
dt2.Columns.Add("Fk");
dt2.Columns.Add("OtherCol2");
dt2.Columns.Add("OtherCol3");
dt2.Rows.Add("X", "B", "FF", DBNull.Value); //these are colliding (on pk 'X') and also orphans (B does not appear in parent table dt)
dt2.Rows.Add("X", "B", "GG", DBNull.Value);
var tblParent = db.CreateTable("Parent", dt);
var tblChild = db.CreateTable("Child", dt2);
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tblParent, out var parentTableInfo, out var parentColumnInfosCreated);
Import(tblChild, out var childTableInfo, out var childColumnInfosCreated);
//make sure RDMP knows joins start with this table
parentTableInfo.IsPrimaryExtractionTable = true;
parentTableInfo.SaveToDatabase();
//lie about the primary key statuses (to simulate live)
var seriesInstanceUIdCol =
parentColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk"));
seriesInstanceUIdCol.IsPrimaryKey = true;
seriesInstanceUIdCol.SaveToDatabase();
var sopInstanceUIdCol = childColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk2"));
sopInstanceUIdCol.IsPrimaryKey = true;
sopInstanceUIdCol.SaveToDatabase();
//Create a new mutilator for these two tables
var mutilator = GetMutilator(db, parentTableInfo, childTableInfo);
//tell RDMP about how to join tables
_=new JoinInfo(CatalogueRepository, childColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Fk")),
parentColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Pk")),
ExtractionJoinType.Right, null);
//now that we have a join it should pass checks
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, new ReturnSameString());
var job = new ThrowImmediatelyDataLoadJob(config, parentTableInfo, childTableInfo);
mutilator.Initialize(db, LoadStage.AdjustRaw);
var ex = Assert.Throws<Exception>(() => mutilator.Mutilate(job));
Assert.That(ex.Message, Is.EqualTo("Primary key value not found for X"));
}
[TestCase(DatabaseType.MicrosoftSQLServer)]
[TestCase(DatabaseType.MySql)]
[TestCase(DatabaseType.PostgreSql)]
public void Test_IsolateTables_NullForeignKey(DatabaseType dbType)
{
var db = GetCleanedServer(dbType);
/***************************************
* Parent(Pk) Child (Pk2,Fk,OtherCol)
* A <> X,A,FF
* ⬍ (collision on pk X causes lookup of A but then we throw because the second record has no Fk listed)
* X,NULL,GG
*
**********************************/
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("Pk");
dt.Columns.Add("OtherCol");
dt.Rows.Add("A", 1); //these are colliding on pk "A" with different values of "OtherCol"
//Create a table in 'RAW' (has no constraints)
using var dt2 = new DataTable();
dt2.Columns.Add("Pk2");
dt2.Columns.Add("Fk");
dt2.Columns.Add("OtherCol2");
dt2.Columns.Add("OtherCol3");
dt2.Rows.Add("X", "A", "FF", DBNull.Value); //these are colliding (on pk 'X'). "A" exists but the null value in the other record is a problem
dt2.Rows.Add("X", DBNull.Value, "GG", DBNull.Value);
var tblParent = db.CreateTable("Parent", dt);
var tblChild = db.CreateTable("Child", dt2);
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tblParent, out var parentTableInfo, out var parentColumnInfosCreated);
Import(tblChild, out var childTableInfo, out var childColumnInfosCreated);
//make sure RDMP knows joins start with this table
parentTableInfo.IsPrimaryExtractionTable = true;
parentTableInfo.SaveToDatabase();
//lie about the primary key statuses (to simulate live)
var seriesInstanceUIdCol =
parentColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk"));
seriesInstanceUIdCol.IsPrimaryKey = true;
seriesInstanceUIdCol.SaveToDatabase();
var sopInstanceUIdCol = childColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk2"));
sopInstanceUIdCol.IsPrimaryKey = true;
sopInstanceUIdCol.SaveToDatabase();
//Create a new mutilator for these two tables
var mutilator = GetMutilator(db, parentTableInfo, childTableInfo);
//tell RDMP about how to join tables
_=new JoinInfo(CatalogueRepository, childColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Fk")),
parentColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Pk")),
ExtractionJoinType.Right, null);
//now that we have a join it should pass checks
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, new ReturnSameString());
var job = new ThrowImmediatelyDataLoadJob(config, parentTableInfo, childTableInfo);
mutilator.Initialize(db, LoadStage.AdjustRaw);
mutilator.Mutilate(job);
//parent should now have 0...
using var dtParent = parentTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtParent.Rows, Is.Empty);
//isolation should have 1 (A)
using var dtParentIsolation = db.ExpectTable("Parent_Isolation").GetDataTable();
Assert.That(dtParentIsolation.Rows, Has.Count.EqualTo(1));
AssertContains(dtParentIsolation, "A", true, 0);
//child table should have the null record only
using var dtChild = childTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtChild.Rows, Has.Count.EqualTo(1));
AssertContains(dtChild, "X", DBNull.Value, "GG", DBNull.Value);
//child isolation table should have 1 record (the X,A,FF)
using var dtChildIsolation = db.ExpectTable("Child_Isolation").GetDataTable();
Assert.That(dtChildIsolation.Rows, Has.Count.EqualTo(1));
AssertContains(dtChildIsolation, "X", "A", "FF", DBNull.Value, 0);
}
private static void AssertContains(DataTable dt, params object[] rowValues)
{
Assert.That(dt.Rows.Cast<DataRow>().Any(r =>
rowValues.All(v => r.ItemArray.Contains(v))), $"Did not find expected row {string.Join(",", rowValues)}{Environment.NewLine}Rows seen were:{string.Join(Environment.NewLine,
dt.Rows.Cast<DataRow>().Select(r => string.Join(",", r.ItemArray)))}");
}
[TestCase(DatabaseType.MicrosoftSQLServer)]
[TestCase(DatabaseType.MySql)]
[TestCase(DatabaseType.PostgreSql)]
public void Test_IsolateTables_AmbiguousFk(DatabaseType dbType)
{
var db = GetCleanedServer(dbType);
/***************************************
* Parent(Pk) Child (Pk2,Fk,OtherCol)
* A -> X,A,FF
* ⬍ (collision on pk X is a big problem because they list different fks!)
* B -> X,B,GG
* Y,B,AA (good record, if we have to isolate both A and B we had better isolate this too)
**********************************/
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("Pk");
dt.Columns.Add("OtherCol");
dt.Rows.Add("A", 1);
dt.Rows.Add("B", 2);
//Create a table in 'RAW' (has no constraints)
using var dt2 = new DataTable();
dt2.Columns.Add("Pk2");
dt2.Columns.Add("Fk");
dt2.Columns.Add("OtherCol2");
dt2.Columns.Add("OtherCol3");
dt2.Rows.Add("X", "A", "FF", DBNull.Value); //these are colliding (on pk 'X') but list two different (but existing) pks!
dt2.Rows.Add("X", "B", "GG", DBNull.Value);
dt2.Rows.Add("Y", "B", "AA", DBNull.Value); //good record but has to be isolated because it is child of B which is involved in the above collision
var tblParent = db.CreateTable("Parent", dt);
var tblChild = db.CreateTable("Child", dt2);
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tblParent, out var parentTableInfo, out var parentColumnInfosCreated);
Import(tblChild, out var childTableInfo, out var childColumnInfosCreated);
//make sure RDMP knows joins start with this table
parentTableInfo.IsPrimaryExtractionTable = true;
parentTableInfo.SaveToDatabase();
//lie about the primary key statuses (to simulate live)
var seriesInstanceUIdCol =
parentColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk"));
seriesInstanceUIdCol.IsPrimaryKey = true;
seriesInstanceUIdCol.SaveToDatabase();
var sopInstanceUIdCol = childColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk2"));
sopInstanceUIdCol.IsPrimaryKey = true;
sopInstanceUIdCol.SaveToDatabase();
//Create a new mutilator for these two tables
var mutilator = GetMutilator(db, parentTableInfo, childTableInfo);
//tell RDMP about how to join tables
_=new JoinInfo(CatalogueRepository, childColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Fk")),
parentColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Pk")),
ExtractionJoinType.Right, null);
//now that we have a join it should pass checks
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, new ReturnSameString());
var job = new ThrowImmediatelyDataLoadJob(config, parentTableInfo, childTableInfo);
mutilator.Initialize(db, LoadStage.AdjustRaw);
mutilator.Mutilate(job);
//parent should now have 0...
using var dtParent = parentTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtParent.Rows, Is.Empty);
//isolation should have 2 (A and B)
using var dtParentIsolation = db.ExpectTable("Parent_Isolation").GetDataTable();
Assert.That(dtParentIsolation.Rows, Has.Count.EqualTo(2));
//child table should also be empty
using var dtChild = childTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtChild.Rows, Is.Empty);
//child isolation table should have 3 (both bad records and the good record that would otherwise be an orphan in live)
using var dtChildIsolation = db.ExpectTable("Child_Isolation").GetDataTable();
Assert.That(dtChildIsolation.Rows, Has.Count.EqualTo(3));
}
[TestCase(DatabaseType.MicrosoftSQLServer)]
[TestCase(DatabaseType.MySql)]
[TestCase(DatabaseType.PostgreSql)]
public void Test_IsolateTables_NoRecordsLeftBehind(DatabaseType dbType)
{
var db = GetCleanedServer(dbType);
/***************************************
* Parent(Pk) Child (Pk2,Fk,OtherCol)
* A -> X,A,FF
* ⬍ (collision on Pk X means we migrate A but we must make sure to ship Y too or it will be an orphan)
* X,A,GG
* Y,A,HH ('good' record but must be isolated because referenced foreign key A is going away).
*
**********************************/
//Create a table in 'RAW' (has no constraints)
using var dt = new DataTable();
dt.Columns.Add("Pk");
dt.Columns.Add("OtherCol");
dt.Rows.Add("A", 1);
//Create a table in 'RAW' (has no constraints)
using var dt2 = new DataTable();
dt2.Columns.Add("Pk2");
dt2.Columns.Add("Fk");
dt2.Columns.Add("OtherCol2");
dt2.Columns.Add("OtherCol3");
dt2.Rows.Add("X", "A", "FF", DBNull.Value); //these are colliding (on pk 'X')
dt2.Rows.Add("X", "A", "GG", DBNull.Value);
dt2.Rows.Add("Y", "A", "HH", DBNull.Value); //must not be left behind
var tblParent = db.CreateTable("Parent", dt);
var tblChild = db.CreateTable("Child", dt2);
//import the table and make A look like a primary key to the metadata layer (and A would be pk in LIVE but not in RAW ofc)
Import(tblParent, out var parentTableInfo, out var parentColumnInfosCreated);
Import(tblChild, out var childTableInfo, out var childColumnInfosCreated);
//make sure RDMP knows joins start with this table
parentTableInfo.IsPrimaryExtractionTable = true;
parentTableInfo.SaveToDatabase();
//lie about the primary key statuses (to simulate live)
var seriesInstanceUIdCol =
parentColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk"));
seriesInstanceUIdCol.IsPrimaryKey = true;
seriesInstanceUIdCol.SaveToDatabase();
var sopInstanceUIdCol = childColumnInfosCreated.Single(c => c.GetRuntimeName().Equals("Pk2"));
sopInstanceUIdCol.IsPrimaryKey = true;
sopInstanceUIdCol.SaveToDatabase();
//Create a new mutilator for these two tables
var mutilator = GetMutilator(db, parentTableInfo, childTableInfo);
//tell RDMP about how to join tables
_=new JoinInfo(CatalogueRepository, childColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Fk")),
parentColumnInfosCreated.Single(static c => c.GetRuntimeName().Equals("Pk")),
ExtractionJoinType.Right, null);
//now that we have a join it should pass checks
mutilator.Check(new AcceptAllCheckNotifier());
var config = new HICDatabaseConfiguration(db.Server, new ReturnSameString());
var job = new ThrowImmediatelyDataLoadJob(config, parentTableInfo, childTableInfo);
mutilator.Initialize(db, LoadStage.AdjustRaw);
Assert.DoesNotThrow(() => mutilator.Mutilate(job));
//parent should now have 0...
using var dtParent = parentTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtParent.Rows, Is.Empty);
//isolation should have 1
using var dtParentIsolation = db.ExpectTable("Parent_Isolation").GetDataTable();
Assert.That(dtParentIsolation.Rows, Has.Count.EqualTo(1));
//child table should also be empty
using var dtChild = childTableInfo.Discover(DataAccessContext.InternalDataProcessing).GetDataTable();
Assert.That(dtChild.Rows, Is.Empty);
//child isolation table should have 3 (both bad records and the good record that would otherwise be an orphan in live)
using var dtChildIsolation = db.ExpectTable("Child_Isolation").GetDataTable();
Assert.That(dtChildIsolation.Rows, Has.Count.EqualTo(3));
}
class ReturnSameString : INameDatabasesAndTablesDuringLoads
{
public string GetDatabaseName(string rootDatabaseName, LoadBubble convention)
{
return rootDatabaseName;
}
public string GetName(string tableName, LoadBubble convention)
{
return tableName;
}
}
}