-
-
Notifications
You must be signed in to change notification settings - Fork 481
/
import-update-tables.bats
1225 lines (1031 loc) · 39.4 KB
/
import-update-tables.bats
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
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/helper/common.bash
setup() {
setup_common
cat <<SQL > 1pk5col-ints-sch.sql
CREATE TABLE test (
pk BIGINT NOT NULL COMMENT 'tag:0',
c1 BIGINT COMMENT 'tag:1',
c2 BIGINT COMMENT 'tag:2',
c3 BIGINT COMMENT 'tag:3',
c4 BIGINT COMMENT 'tag:4',
c5 BIGINT COMMENT 'tag:5',
PRIMARY KEY (pk)
);
SQL
cat <<SQL > 1pk1col-char-sch.sql
CREATE TABLE test (
pk BIGINT NOT NULL COMMENT 'tag:0',
c CHAR(5) COMMENT 'tag:1',
PRIMARY KEY (pk)
);
SQL
cat <<DELIM > 1pk5col-ints.csv
pk,c1,c2,c3,c4,c5
0,1,2,3,4,5
1,1,2,3,4,5
DELIM
cat <<DELIM > 1pk5col-ints-updt.csv
pk,c1,c2,c3,c4,c5
0,1,2,3,4,6
DELIM
cat <<SQL > employees-sch.sql
CREATE TABLE employees (
\`id\` varchar(20) NOT NULL COMMENT 'tag:0',
\`first name\` LONGTEXT COMMENT 'tag:1',
\`last name\` LONGTEXT COMMENT 'tag:2',
\`title\` LONGTEXT COMMENT 'tag:3',
\`start date\` LONGTEXT COMMENT 'tag:4',
\`end date\` LONGTEXT COMMENT 'tag:5',
PRIMARY KEY (id)
);
SQL
cat <<SQL > check-constraint-sch.sql
CREATE TABLE persons (
ID int PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);
SQL
cat <<SQL > nibrs_month_sch.sql
CREATE TABLE \`test\` (
\`STATE_ID\` smallint NOT NULL,
\`NIBRS_MONTH_ID\` bigint NOT NULL,
\`AGENCY_ID\` bigint NOT NULL,
\`AGENCY_TABLE_TYPE_ID\` smallint NOT NULL,
\`MONTH_NUM\` smallint NOT NULL,
\`DATA_YEAR\` smallint NOT NULL,
\`REPORTED_STATUS\` varchar(1) collate utf8mb4_0900_ai_ci,
\`REPORT_DATE\` timestamp,
\`UPDATE_FLAG\` char(1) collate utf8mb4_0900_ai_ci,
\`ORIG_FORMAT\` char(1) collate utf8mb4_0900_ai_ci,
\`DATA_HOME\` varchar(1) collate utf8mb4_0900_ai_ci,
\`DDOCNAME\` varchar(50) collate utf8mb4_0900_ai_ci,
\`DID\` bigint,
\`MONTH_PUB_STATUS\` int,
\`INC_DATA_YEAR\` int,
PRIMARY KEY (\`STATE_ID\`,\`NIBRS_MONTH_ID\`,\`DATA_YEAR\`),
KEY \`AGENCY_TABLE_TYPE_ID\` (\`AGENCY_TABLE_TYPE_ID\`),
KEY \`DATA_YEAR_INDEX\` (\`DATA_YEAR\`),
KEY \`NIBRS_MONTH_ID_INDEX\` (\`NIBRS_MONTH_ID\`),
KEY \`STATE_ID_INDEX\` (\`STATE_ID\`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
SQL
cat <<CSV > nibrs_month_csv.csv
INC_DATA_YEAR,NIBRS_MONTH_ID,AGENCY_ID,MONTH_NUM,DATA_YEAR,REPORTED_STATUS,REPORT_DATE,UPDATE_FLAG,ORIG_FORMAT,DATA_HOME,DDOCNAME,DID,MONTH_PUB_STATUS,STATE_ID,AGENCY_TABLE_TYPE_ID
2019,9128595,9305,3,2019,I,2019-07-18,Y,F,C,2019_03_MN0510000_NIBRS,49502383,0,27,2
CSV
dolt sql <<SQL
CREATE TABLE colors (
id INT NOT NULL,
color VARCHAR(32) NOT NULL,
PRIMARY KEY (id),
INDEX color_index(color)
);
CREATE TABLE objects (
id INT NOT NULL,
name VARCHAR(64) NOT NULL,
color VARCHAR(32),
PRIMARY KEY(id),
FOREIGN KEY (color) REFERENCES colors(color)
);
INSERT INTO colors (id,color) VALUES (1,'red'),(2,'green'),(3,'blue'),(4,'purple');
INSERT INTO objects (id,name,color) VALUES (1,'truck','red'),(2,'ball','green'),(3,'shoe','blue');
SQL
}
teardown() {
assert_feature_version
teardown_common
}
@test "import-update-tables: update table using csv" {
dolt sql < 1pk5col-ints-sch.sql
run dolt table import -u test `batshelper 1pk5col-ints.csv`
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 2, Additions: 2, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
# Sanity check
! [[ "$output" =~ "Warning: There are fewer columns in the import file's schema than the table's schema" ]] || false
# Validate that a successful import with no bad rows does not print the following
! [[ "$output" =~ "The following rows were skipped:" ]] || false
# Run again to get correct Had No Effect amount
run dolt table import -u test `batshelper 1pk5col-ints.csv`
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 2, Additions: 0, Modifications: 0, Had No Effect: 2" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
# Run another update for the correct modification amount
run dolt table import -u test 1pk5col-ints-updt.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 1, Additions: 0, Modifications: 1, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
}
@test "import-update-tables: update table using csv with null (blank) values" {
skip "nulls from csv not working correctly on update"
dolt sql < 1pk5col-ints-sch.sql
run dolt table import -u test `batshelper 1pk5col-nulls.csv`
[ "$status" -eq 0 ]
[[ "$output" =~ "Import completed successfully." ]] || false
# Validate that a successful import with no bad rows does not print the following
! [[ "$output" =~ "The following rows were skipped:" ]] || false
}
@test "import-update-tables: update table using schema with csv" {
dolt sql < 1pk5col-ints-sch.sql
run dolt table import -u -s `batshelper 1pk5col-ints-schema.json` test `batshelper 1pk5col-ints.csv`
[ "$status" -eq 1 ]
[[ "$output" =~ "fatal: schema is not supported for update or replace operations" ]] || false
}
@test "import-update-tables: update table using csv with newlines" {
dolt sql <<SQL
CREATE TABLE test (
pk varchar(20) NOT NULL COMMENT 'tag:0',
c1 LONGTEXT COMMENT 'tag:1',
c2 LONGTEXT COMMENT 'tag:2',
c3 LONGTEXT COMMENT 'tag:3',
c4 LONGTEXT COMMENT 'tag:4',
c5 LONGTEXT COMMENT 'tag:5',
PRIMARY KEY (pk)
);
SQL
run dolt table import -u test `batshelper 1pk5col-strings-newlines.csv`
[ "$status" -eq 0 ]
}
@test "import-update-tables: update table using json" {
dolt sql < employees-sch.sql
run dolt table import -u employees `batshelper employees-tbl.json`
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
}
@test "import-update-tables: update table using wrong json" {
dolt sql <<SQL
CREATE TABLE employees (
\`idz\` varchar(20) NOT NULL COMMENT 'tag:0',
\`first namez\` LONGTEXT COMMENT 'tag:1',
\`last namez\` LONGTEXT COMMENT 'tag:2',
\`titlez\` LONGTEXT COMMENT 'tag:3',
\`start datez\` LONGTEXT COMMENT 'tag:4',
\`end datez\` LONGTEXT COMMENT 'tag:5',
PRIMARY KEY (idz)
);
SQL
run dolt table import -u employees `batshelper employees-tbl.json`
[ "$status" -eq 1 ]
[[ "$output" =~ "not found in schema" ]] || false
}
@test "import-update-tables: update table using schema with json" {
dolt sql < employees-sch.sql
run dolt table import -u -s employees-sch.sql employees `batshelper employees-tbl.json`
[ "$status" -eq 1 ]
[[ "$output" =~ "fatal: schema is not supported for update or replace operations" ]] || false
}
@test "import-update-tables: update table with existing imported data with different schema" {
run dolt table import -c -s employees-sch.sql employees `batshelper employees-tbl.json`
[ "$status" -eq 0 ]
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt table import -u employees `batshelper employees-tbl-schema-wrong.json`
[ "$status" -eq 1 ]
[[ "$output" =~ "not found in schema" ]] || false
}
@test "import-update-tables: update table with json when table does not exist" {
run dolt table import -u employees `batshelper employees-tbl.json`
[ "$status" -eq 1 ]
[[ "$output" =~ "The following table could not be found:" ]] || false
}
@test "import-update-tables: update table with a json with columns in different order" {
dolt sql <<SQL
CREATE TABLE employees (
\`id\` varchar(20) NOT NULL COMMENT 'tag:0',
\`first name\` LONGTEXT COMMENT 'tag:1',
\`last name\` LONGTEXT COMMENT 'tag:2',
\`title\` LONGTEXT COMMENT 'tag:3',
\`start date\` LONGTEXT COMMENT 'tag:4',
\`end date\` LONGTEXT COMMENT 'tag:5',
PRIMARY KEY (id)
);
SQL
run dolt table import -u employees `batshelper employees-tbl-schema-unordered.json`
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt schema export employees
[[ "$status" -eq 0 ]]
[[ "${lines[1]}" =~ "id" ]] || false
[[ "${lines[2]}" =~ "first name" ]] || false
[[ "${lines[3]}" =~ "last name" ]] || false
[[ "${lines[4]}" =~ "title" ]] || false
[[ "${lines[5]}" =~ "start date" ]] || false
[[ "${lines[6]}" =~ "end date" ]] || false
}
@test "import-update-tables: update table with a csv with columns in different order" {
dolt sql <<SQL
CREATE TABLE employees (
\`id\` varchar(20) NOT NULL COMMENT 'tag:0',
\`first name\` LONGTEXT COMMENT 'tag:1',
\`last name\` LONGTEXT COMMENT 'tag:2',
\`title\` LONGTEXT COMMENT 'tag:3',
\`start date\` LONGTEXT COMMENT 'tag:4',
\`end date\` LONGTEXT COMMENT 'tag:5',
PRIMARY KEY (id)
);
SQL
run dolt table import -u employees `batshelper employees-tbl-schema-unordered.csv`
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt schema export employees
[[ "$status" -eq 0 ]]
[[ "${lines[1]}" =~ "id" ]] || false
[[ "${lines[2]}" =~ "first name" ]] || false
[[ "${lines[3]}" =~ "last name" ]] || false
[[ "${lines[4]}" =~ "title" ]] || false
[[ "${lines[5]}" =~ "start date" ]] || false
[[ "${lines[6]}" =~ "end date" ]] || false
}
@test "import-update-tables: updating table by inputting string longer than char column throws an error" {
cat <<DELIM > 1pk1col-rpt-chars.csv
pk,c
1,"123456"
DELIM
dolt sql < 1pk1col-char-sch.sql
run dolt table import -u test 1pk1col-rpt-chars.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "A bad row was encountered while moving data" ]] || false
[[ "$output" =~ "Bad Row: [1,123456]" ]] || false
[[ "$output" =~ 'too large for column' ]] || false
}
@test "import-update-tables: update table with repeat pk in csv does not throw an error" {
cat <<DELIM > 1pk5col-rpt-ints.csv
pk,c1,c2,c3,c4,c5
1,1,2,3,4,5
1,1,2,3,4,5
DELIM
dolt sql < 1pk5col-ints-sch.sql
run dolt table import -u test 1pk5col-rpt-ints.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt sql -r csv -q "select * from test"
[ "${#lines[@]}" -eq 2 ]
[[ "${lines[1]}" =~ "1,1,2,3,4,5" ]] || false
}
@test "import-update-tables: importing into new table renders bad rows" {
cat <<DELIM > persons.csv
ID,LastName,FirstName,Age
1,"jon","doe", 20
2,"little","doe", 10
3,"little","doe",4
4,"little","doe",1
DELIM
dolt sql < check-constraint-sch.sql
run dolt table import -u persons persons.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "A bad row was encountered while moving data" ]] || false
[[ "$output" =~ "Bad Row:" ]] || false
[[ "$output" =~ "[2,little,doe,10]" ]] || false
run dolt table import -u --continue persons persons.csv
[ "$status" -eq 0 ]
[[ "${lines[0]}" =~ "The following rows were skipped:" ]] || false
[[ "${lines[1]}" =~ '[2,little,doe,10]' ]] || false
[[ "${lines[2]}" =~ '[3,little,doe,4]' ]] || false
[[ "${lines[3]}" =~ '[4,little,doe,1]' ]] || false
[[ "${lines[4]}" =~ "Rows Processed: 1, Additions: 1, Modifications: 0, Had No Effect: 0" ]] || false
[[ "${lines[5]}" =~ "Lines skipped: 3" ]] || false
[[ "${lines[6]}" =~ "Import completed successfully." ]] || false
run dolt sql -r csv -q "select * from persons"
[ "${#lines[@]}" -eq 2 ]
[[ "$output" =~ "ID,LastName,FirstName,Age" ]] || false
[[ "$output" =~ "1,jon,doe,20" ]] || false
}
@test "import-update-tables: subsequent runs of same import with duplicate keys produces no difference in final data" {
cat <<DELIM > 1pk5col-rpt-ints.csv
pk,c1,c2,c3,c4,c5
1,1,2,3,4,5
1,1,2,3,4,7
1,1,2,3,4,8
DELIM
dolt sql < 1pk5col-ints-sch.sql
dolt table import -u --continue test 1pk5col-rpt-ints.csv
dolt add .
dolt commit -am "cm1"
run dolt table import -u --continue test 1pk5col-rpt-ints.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Modifications: 3" ]] || falsa
run dolt diff
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test "import-update-tables: importing some columns does not overwrite columns not part of the import" {
dolt sql <1pk5col-ints-sch.sql
echo -e 'pk,c1\n1,1\n2,6'|dolt table import -u test
echo -e 'pk,c2\n1,2\n2,7'|dolt table import -u test
echo -e 'pk,c3,c4,c5\n1,3,4,5\n2,8,9,10'|dolt table import -u test
EXPECTED=$(echo -e "pk,c1,c2,c3,c4,c5\n1,1,2,3,4,5\n2,6,7,8,9,10")
run dolt sql -r csv -q 'SELECT * FROM test'
[ "$status" -eq 0 ]
[[ "$output" =~ "$EXPECTED" ]] || false
}
@test "import-update-tables: poorly written file correctly errors" {
cat <<DELIM > bad-updates.csv
pk,v1
5,5,
6,6,
DELIM
dolt sql -q "CREATE TABLE test(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT 2 NOT NULL, v2 int)"
dolt sql -q "INSERT INTO test (pk, v1, v2) VALUES (1, 2, 3), (2, 3, 4)"
run dolt table import -u test bad-updates.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "A bad row was encountered while moving data" ]] || false
[[ "$output" =~ "CSV reader expected 2 values, but saw 3" ]] || false
[[ "$output" =~ "with the following values left over: '[\"\"]'" ]] || false
run dolt table import -u --continue test bad-updates.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Lines skipped: 2" ]] || false
}
@test "import-update-tables: error during primary key table just skips" {
cat <<DELIM > bad-updates.csv
pk
1
2
100
3
DELIM
dolt sql -q "CREATE TABLE test(pk int PRIMARY KEY CHECK (pk < 10))"
run dolt table import -u test bad-updates.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "A bad row was encountered while moving data" ]] || false
[[ "$output" =~ "[100]" ]] || false
run dolt table import -u --continue test bad-updates.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Lines skipped: 1" ]] || false
run dolt sql -r csv -q "select * from test"
skip "table editors need to handle continue flag"
[ "${#lines[@]}" -eq 4 ]
[[ "$output" =~ "pk" ]] || false
[[ "$output" =~ "1" ]] || false
[[ "$output" =~ "2" ]] || false
[[ "$output" =~ "3" ]] || false
}
@test "import-update-tables: compare tables in database with table imported from parquet file" {
dolt sql -q "CREATE TABLE testTypes (pk BIGINT PRIMARY KEY, v1 TIME, v2 YEAR, v3 DATETIME, v4 BOOL, v5 ENUM('one', 'two', 'three'));"
dolt add .
dolt commit -m "create table"
dolt branch new_branch
dolt sql -q "INSERT INTO testTypes VALUES (1,'11:11:11','2020','2020-04-09 11:11:11',true,'one'),(2,'12:12:12','2020','2020-04-09 12:12:12',false,'three'),(3,'04:12:34','2019','2019-10-10 04:12:34',true,NULL),(4,NULL,'2020','2011-09-19 23:23:14',false,'two');"
dolt add .
dolt commit -m "add rows"
run dolt table export testTypes test.parquet
[ "$status" -eq 0 ]
[ -f test.parquet ]
dolt checkout new_branch
dolt table import -u testTypes test.parquet
dolt add .
dolt commit --allow-empty -m "update table from parquet file"
run dolt diff --summary main new_branch
[ "$status" -eq 0 ]
[[ "$output" = "" ]] || false
}
@test "import-update-tables: bad parquet file import errors" {
dolt sql -q "CREATE TABLE test_table (pk int primary key, col1 text, col2 int);"
echo "This is a bad parquet file" > bad.parquet
run dolt table import -u test_table bad.parquet
[ "$status" -eq 1 ]
[[ "$output" =~ "When attempting to move data from parquet file:bad.parquet to test_table, could not open a reader." ]] || false
}
@test "import-update-tables: Subsequent updates with --continue correctly work" {
dolt sql -q "create table t (pk int primary key, val varchar(1))"
cat <<DELIM > file1.csv
pk,val
1,a
2,b
DELIM
cat <<DELIM > file2.csv
pk,val
1,c
2,gps
3,v
DELIM
cat <<DELIM > file3.csv
pk,val
1,d
4,fg
dsadas,de
DELIM
run dolt table import -u --continue t file1.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 2, Additions: 2, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt sql -r csv -q "select * from t"
[ "${lines[1]}" = "1,a" ]
[ "${lines[2]}" = "2,b" ]
run dolt table import -u --continue t file2.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 3, Additions: 1, Modifications: 2, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
! [[ "$output" =~ "The following rows were skipped:" ]] || false
run dolt sql -r csv -q "select * from t"
[ "${lines[1]}" = "1,c" ]
[ "${lines[2]}" = "2,g" ]
[ "${lines[3]}" = "3,v" ]
run dolt table import -u --continue t file3.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 3, Additions: 2, Modifications: 1, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
! [[ "$output" =~ "The following rows were skipped:" ]] || false
run dolt sql -r csv -q "select * from t order by pk"
[ "${lines[1]}" = "0,d" ]
[ "${lines[2]}" = "1,d" ]
[ "${lines[3]}" = "2,g" ]
[ "${lines[4]}" = "3,v" ]
[ "${lines[5]}" = "4,f" ]
run dolt sql -q "select count(*) from t"
[[ "$output" =~ "5" ]] || false
}
@test "import-update-tables: string too large for column regression" {
dolt sql < nibrs_month_sch.sql
run dolt table import -u test nibrs_month_csv.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 1, Additions: 1, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
}
@test "import-update-tables: csv subsetting throws error with not null column" {
cat <<SQL > 1pk5col-ints-def-sch.sql
CREATE TABLE test (
pk int NOT NULL COMMENT 'tag:0',
c1 int,
c2 int,
c3 int,
c4 int NOT NULL,
c5 int,
PRIMARY KEY (pk)
);
SQL
cat <<DELIM > 1pk5col-ints-updt.csv
pk,c1,c2,c5,c3
0,1,2,6,3
DELIM
dolt sql < 1pk5col-ints-def-sch.sql
run dolt table import -u test 1pk5col-ints-updt.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "Field 'c4' doesn't have a default value" ]] || false
}
@test "import-update-tables: csv subsetting but with defaults" {
cat <<SQL > 1pk5col-ints-def-sch.sql
CREATE TABLE test (
pk int NOT NULL COMMENT 'tag:0',
c1 int,
c2 int,
c3 int,
c4 int DEFAULT 42,
c5 int,
PRIMARY KEY (pk)
);
SQL
cat <<DELIM > 1pk5col-ints-updt.csv
pk,c1,c2,c5,c3
0,1,2,6,3
DELIM
dolt sql < 1pk5col-ints-def-sch.sql
run dolt table import -u test 1pk5col-ints-updt.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Warning: There are fewer columns in the import file's schema than the table's schema" ]] || false
[[ "$output" =~ "Rows Processed: 1, Additions: 1, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt sql -r csv -q "select * from test"
[ "${lines[1]}" = "0,1,2,3,42,6" ]
run dolt sql -q "select count(*) from test"
[[ "$output" =~ "1" ]] || false
}
@test "import-update-tables: csv files has less columns that schema -u" {
cat <<DELIM > 1pk5col-ints-updt.csv
pk,c1,c2,c5,c3
0,1,2,6,3
DELIM
dolt sql < 1pk5col-ints-sch.sql
run dolt table import -u test 1pk5col-ints-updt.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Warning: There are fewer columns in the import file's schema than the table's schema" ]] || false
[[ "$output" =~ "Rows Processed: 1, Additions: 1, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt sql -r csv -q "select * from test"
[ "${lines[1]}" = "0,1,2,3,,6" ]
run dolt sql -q "select count(*) from test"
[[ "$output" =~ "1" ]] || false
}
@test "import-update-tables: csv files has same number of columns but different order than schema" {
cat <<DELIM > 1pk5col-ints-updt.csv
pk,c2,c4,c5,c1,c3
0,2,4,6,1,3
DELIM
dolt sql < 1pk5col-ints-sch.sql
run dolt table import -u test 1pk5col-ints-updt.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 1, Additions: 1, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
! [[ "$output" =~ "Warning: There are fewer columns in the import file's schema than the table's schema" ]] || false
run dolt sql -r csv -q "select * from test"
[ "${lines[1]}" = "0,1,2,3,4,6" ]
run dolt sql -q "select count(*) from test"
[[ "$output" =~ "1" ]] || false
}
@test "import-update-tables: csv files has more column than schema and different order" {
cat <<DELIM > 1pk5col-ints-updt.csv
pk,c2,c4,c5,c1,c3,c7
0,2,4,6,1,3,100
DELIM
dolt sql < 1pk5col-ints-sch.sql
run dolt table import -u test 1pk5col-ints-updt.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 1, Additions: 1, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
! [[ "$output" =~ "Warning: There are fewer columns in the import file's schema than the table's schema" ]] || false
run dolt sql -r csv -q "select * from test"
[ "${lines[1]}" = "0,1,2,3,4,6" ]
run dolt sql -q "select count(*) from test"
[[ "$output" =~ "1" ]] || false
}
@test "import-update-tables: just update one column in a big table" {
cat <<DELIM > 1pk5col-ints-updt.csv
pk,c2
0,7
DELIM
dolt sql < 1pk5col-ints-sch.sql
dolt sql -q "insert into test values (0,1,2,3,4,5)"
run dolt table import -u test 1pk5col-ints-updt.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Warning: There are fewer columns in the import file's schema than the table's schema" ]] || false
[[ "$output" =~ "Rows Processed: 1, Additions: 0, Modifications: 1, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt sql -r csv -q "select * from test"
[ "${lines[1]}" = "0,1,7,3,4,5" ]
}
@test "import-update-tables: updating a table with no primary keys complains" {
cat <<DELIM > 1pk5col-ints-updt.csv
c2
70
DELIM
dolt sql < 1pk5col-ints-sch.sql
run dolt table import -u test 1pk5col-ints-updt.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "Error determining the output schema." ]] || false
}
@test "import-update-tables: partial update on keyless table" {
cat <<SQL > schema.sql
CREATE TABLE keyless (
c0 INT,
c1 INT DEFAULT 42,
c2 INT
);
SQL
dolt sql < schema.sql
dolt sql -q "insert into keyless values (0,1,0)"
cat <<DELIM > data.csv
c0,c2
0,2
DELIM
run dolt table import -u keyless data.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Warning: There are fewer columns in the import file's schema than the table's schema" ]] || false
[[ "$output" =~ "Rows Processed: 1, Additions: 1, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt sql -r csv -q "select * from keyless order by c0, c1 DESC"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[1]}" = "0,42,2" ]
[ "${lines[2]}" = "0,1,0" ]
}
@test "import-update-tables: --ignore-skipped-rows correctly prevents skipped rows from printing" {
cat <<DELIM > persons.csv
ID,LastName,FirstName,Age
1,"jon","doe", 20
2,"little","doe", 10
3,"little","doe",4
4,"little","doe",1
DELIM
dolt sql < check-constraint-sch.sql
run dolt table import -u --continue --ignore-skipped-rows persons persons.csv
[ "$status" -eq 0 ]
! [[ "$output" =~ "The following rows were skipped:" ]] || false
! [[ "$output" =~ "[2,little,doe,10]" ]] || false
! [[ "$output" =~ "[3,little,doe,4]" ]] || false
! [[ "$output" =~ "[4,little,doe,1]" ]] || false
[[ "$output" =~ "Rows Processed: 1, Additions: 1, Modifications: 0, Had No Effect: 0" ]] || false
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt sql -r csv -q "select * from persons"
[[ "$output" =~ "1,jon,doe,20" ]] || false
}
@test "import-update-tables: large amounts of no effect rows" {
dolt sql -q "create table t(pk int primary key)"
dolt sql -q "alter table t add constraint cx CHECK (pk < 10)"
dolt sql -q "Insert into t values (1),(2),(3),(4),(5),(6),(7),(8),(9) "
cat <<DELIM > file.csv
pk
1
2
3
4
5
6
10000
DELIM
run dolt table import -u --continue t file.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 6, Additions: 0, Modifications: 0, Had No Effect: 6" ]] || false
[[ "$output" =~ "The following rows were skipped:" ]] || false
[[ "$output" =~ "[10000]" ]] || false
run dolt sql -r csv -q "select * from t"
[[ "$output" =~ "1" ]] || false
[[ "$output" =~ "2" ]] || false
[[ "$output" =~ "3" ]] || false
[[ "$output" =~ "4" ]] || false
[[ "$output" =~ "5" ]] || false
[[ "$output" =~ "6" ]] || false
}
@test "import-update-tables: import supports tables with dashes in the name" {
cat <<DELIM > file.csv
pk,c1
0,0
DELIM
run dolt table import -c this-is-a-table file.csv
[ $status -eq 0 ]
[[ "$output" =~ "Import completed successfully." ]] || false
run dolt table import -u this-is-a-table file.csv
[ $status -eq 0 ]
run dolt sql -r csv -q "SELECT * FROM \`this-is-a-table\`"
[ $status -eq 0 ]
[[ "$output" =~ "pk,c1" ]] || false
[[ "$output" =~ "0,0" ]] || false
}
@test "import-update-tables: successfully update child table in fk relationship" {
cat <<DELIM > objects-good.csv
id,name,color
4,laptop,blue
5,dollar,green
6,bottle,red
DELIM
run dolt table import -u objects objects-good.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0" ]] || false
run dolt sql -r csv -q "SELECT * FROM objects where id >= 4"
[ $status -eq 0 ]
[[ "$output" =~ "id,name,color" ]] || false
[[ "$output" =~ "4,laptop,blue" ]] || false
[[ "$output" =~ "5,dollar,green" ]] || false
[[ "$output" =~ "6,bottle,red" ]] || false
}
@test "import-update-tables: unsuccessfully update child table in fk relationship" {
cat <<DELIM > objects-bad.csv
id,name,color
4,laptop,blue
5,dollar,green
6,bottle,gray
DELIM
run dolt table import -u objects objects-bad.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "A bad row was encountered while moving data" ]] || false
[[ "$output" =~ "Bad Row: [6,bottle,gray]" ]] || false
[[ "$output" =~ "cannot add or update a child row - Foreign key violation" ]] || false
run dolt table import -u objects objects-bad.csv --continue
[ "$status" -eq 0 ]
[[ "$output" =~ "The following rows were skipped:" ]] || false
[[ "$output" =~ "[6,bottle,gray]" ]] || false
[[ "$output" =~ "Rows Processed: 2, Additions: 2, Modifications: 0, Had No Effect: 0" ]] || false
run dolt sql -r csv -q "SELECT * FROM objects where id >= 4"
[ $status -eq 0 ]
[[ "$output" =~ "id,name,color" ]] || false
[[ "$output" =~ "4,laptop,blue" ]] || false
[[ "$output" =~ "5,dollar,green" ]] || false
! [[ "$output" =~ "6,bottle,red" ]] || false
}
@test "import-update-tables: successfully update child table in multi-key fk relationship" {
dolt sql -q "drop table objects"
dolt sql -q "drop table colors"
dolt sql <<SQL
CREATE TABLE colors (
id INT NOT NULL,
color VARCHAR(32) NOT NULL,
PRIMARY KEY (id),
INDEX color_index(color)
);
CREATE TABLE materials (
id INT NOT NULL,
material VARCHAR(32) NOT NULL,
color VARCHAR(32),
PRIMARY KEY(id),
FOREIGN KEY (color) REFERENCES colors(color),
INDEX color_mat_index(color, material)
);
CREATE TABLE objects (
id INT NOT NULL,
name VARCHAR(64) NOT NULL,
color VARCHAR(32),
material VARCHAR(32),
PRIMARY KEY(id),
FOREIGN KEY (color,material) REFERENCES materials(color,material)
);
INSERT INTO colors (id,color) VALUES (1,'red'),(2,'green'),(3,'blue'),(4,'purple'),(10,'brown');
INSERT INTO materials (id,material,color) VALUES (1,'steel','red'),(2,'rubber','green'),(3,'leather','blue'),(10,'dirt','brown'),(11,'air',NULL);
INSERT INTO objects (id,name,color,material) VALUES (1,'truck','red','steel'),(2,'ball','green','rubber'),(3,'shoe','blue','leather'),(11,'tornado',NULL,'air');
SQL
cat <<DELIM > multi-key-good.csv
id,name,color,material
4,laptop,red,steel
5,dollar,green,rubber
6,bottle,blue,leather
DELIM
run dolt table import -u objects multi-key-good.csv
[ "$status" -eq 0 ]
[[ "$output" =~ "Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0" ]] || false
run dolt sql -r csv -q "SELECT * FROM objects where id >= 4 ORDER BY id"
[ $status -eq 0 ]
[[ "$output" =~ "id,name,color,material" ]] || false
[[ "$output" =~ "4,laptop,red,steel" ]] || false
[[ "$output" =~ "5,dollar,green,rubber" ]] || false
[[ "$output" =~ "6,bottle,blue,leather" ]] || false
cat <<DELIM > multi-key-bad.csv
id,name,color,material
4,laptop,red,steel
5,dollar,green,rubber
6,bottle,blue,steel
DELIM
run dolt table import -u objects multi-key-bad.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "A bad row was encountered while moving data" ]] || false
[[ "$output" =~ "Bad Row: [6,bottle,blue,steel]" ]] || false
[[ "$output" =~ "cannot add or update a child row - Foreign key violation" ]] || false
run dolt table import -u objects multi-key-bad.csv --continue
[ "$status" -eq 0 ]
[[ "$output" =~ "The following rows were skipped:" ]] || false
[[ "$output" =~ "[6,bottle,blue,steel]" ]] || false
[[ "$output" =~ "Rows Processed: 2, Additions: 0, Modifications: 0, Had No Effect: 2" ]] || false
run dolt sql -r csv -q "SELECT * FROM objects where id >= 4 ORDER BY id"
[ $status -eq 0 ]
[[ "$output" =~ "id,name,color,material" ]] || false
[[ "$output" =~ "4,laptop,red,steel" ]] || false
[[ "$output" =~ "5,dollar,green,rubber" ]] || false
! [[ "$output" =~ "6,bottle,blue,steel" ]] || false
}
@test "import-update-tables: import update with CASCADE ON UPDATE" {
dolt sql <<SQL
CREATE TABLE one (
pk int PRIMARY KEY,
v1 int,
v2 int
);
ALTER TABLE one ADD INDEX v1 (v1);
CREATE TABLE two (
pk int PRIMARY KEY,
v1 int,
v2 int,
CONSTRAINT fk_name_1 FOREIGN KEY (v1)
REFERENCES one(v1)
ON DELETE CASCADE
ON UPDATE CASCADE
);
ALTER TABLE two ADD INDEX v1v2 (v1, v2);
CREATE TABLE three (
pk int PRIMARY KEY,
v1 int,
v2 int,
CONSTRAINT fk_name_2 FOREIGN KEY (v1, v2)
REFERENCES two(v1, v2)
ON DELETE CASCADE
ON UPDATE CASCADE
);
INSERT INTO one VALUES (1, 1, 4), (2, 2, 5), (3, 3, 6), (4, 4, 5);
INSERT INTO two VALUES (2, 1, 1), (3, 2, 2), (4, 3, 3), (5, 4, 4);
INSERT INTO three VALUES (3, 1, 1), (4, 2, 2), (5, 3, 3), (6, 4, 4);
SQL
cat <<DELIM > table-one.csv
pk,v1,v2
1,2,2
DELIM
run dolt table import -u one table-one.csv
[ $status -eq 0 ]
[[ "$output" =~ "Rows Processed: 1, Additions: 0, Modifications: 1, Had No Effect: 0" ]] || false
run dolt sql -r csv -q "select * from two where pk = 2"
[ $status -eq 0 ]
[[ "$output" =~ "pk,v1,v2" ]] || false
[[ "$output" =~ "2,2,1" ]] || false
run dolt sql -r csv -q "select * from three where pk = 3"
[ $status -eq 0 ]
[[ "$output" =~ "pk,v1,v2" ]] || false
[[ "$output" =~ "3,2,1" ]] || false
}
@test "import-update-tables: unsuccessfully update parent table in fk relationship" {
cat <<DELIM > colors-bad.csv
id,color
3,dsadasda
5,yellow
DELIM
run dolt table import -u colors colors-bad.csv
[ "$status" -eq 1 ]
[[ "$output" =~ "A bad row was encountered while moving data" ]] || false
[[ "$output" =~ "cannot delete or update a parent row" ]] || false
run dolt table import -u colors colors-bad.csv --continue
[ "$status" -eq 0 ]
run dolt sql -r csv -q "SELECT * from colors where id in (3,5)"
[ "$status" -eq 0 ]
[[ "$output" =~ "id,color" ]] || false
[[ "$output" =~ "3,blue" ]] || false
[[ "$output" =~ "5,yellow" ]] || false
}
@test "import-update-tables: circular foreign keys" {
dolt sql <<SQL
CREATE TABLE tbl (
id int PRIMARY KEY,
v1 int,
v2 int,
INDEX v1 (v1),
INDEX v2 (v2)
);
ALTER TABLE tbl ADD CONSTRAINT fk_named FOREIGN KEY (v2) REFERENCES tbl(v1) ON UPDATE CASCADE ON DELETE CASCADE;
INSERT INTO tbl VALUES (1,1,1), (2, 2, 1), (3, 3, NULL);
SQL
cat <<DELIM > circular-keys-good.csv
id,v1,v2
4,4,2
DELIM
run dolt table import -u tbl circular-keys-good.csv
[ $status -eq 0 ]
[[ "$output" =~ "Rows Processed: 1, Additions: 1, Modifications: 0, Had No Effect: 0" ]] || false