This repository was archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest.js
More file actions
2146 lines (2066 loc) · 72.2 KB
/
Copy pathtest.js
File metadata and controls
2146 lines (2066 loc) · 72.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
var tools = require('a-toolbox')
var pg = require('pg')
var async = require('async')
var Compare = require('./lib/Compare')
// / template of configuration parameters
var configTemplate = {
'connection1': {
'host': 'xxx.xxx.xxx.xxx',
'user': 'username1',
'password': 'username1',
'database': 'dbname1'
},
'connection2': {
'host': 'xxx.xxx.xxx.xxx',
'user': 'username2',
'password': 'username2',
'database': 'dbname2'
},
'compare': {
'schema': {
'tables': true,
'fkeys': true,
'functions': true,
'indexes': true,
'types': true,
'views': true,
'sequences': true,
'rows': ['users']
},
'options': {
'mode': 'full',
'owner': 'owner'
}
},
'output': '/tmp/delta.sql',
'verbose': false
}
// / keep a reference to count success or error tests
var _successCount = 0
var _errorCount = 0
var compare = new Compare()
/**
* Try to pass a config file with a wrong json format
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testWrongJson = function (next) {
console.log('testWrongJson executing')
compare.run({a: 1}, function (err) {
if (err) {
console.log('testWrongJson success')
_successCount++
} else {
console.log('testWrongJson error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without connection to db1
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingConnection1 = function (next) {
console.log('testMissingConnection1 executing')
var _config = tools.object.clone(configTemplate)
_config.connection1 = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingConnection1 success')
_successCount++
} else {
console.log('testMissingConnection1 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without connection to db2
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingConnection2 = function (next) {
console.log('testMissingConnection2 executing')
var _config = tools.object.clone(configTemplate)
_config.connection2 = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingConnection2 success')
_successCount++
} else {
console.log('testMissingConnection2 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without host of db1
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingHost1 = function (next) {
console.log('testMissingHost1 executing')
var _config = tools.object.clone(configTemplate)
_config.connection1.host = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingHost1 success')
_successCount++
} else {
console.log('testMissingHost1 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without host of db2
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingHost2 = function (next) {
console.log('testMissingHost2 executing')
var _config = tools.object.clone(configTemplate)
_config.connection2.host = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingHost2 success')
_successCount++
} else {
console.log('testMissingHost2 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without username to connect to db1
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingUser1 = function (next) {
console.log('testMissingUser1 executing')
var _config = tools.object.clone(configTemplate)
_config.connection1.user = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingUser1 success')
_successCount++
} else {
console.log('testMissingUser1 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without username to connect to db2
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingUser2 = function (next) {
console.log('testMissingUser2 executing')
var _config = tools.object.clone(configTemplate)
_config.connection2.user = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingUser2 success')
_successCount++
} else {
console.log('testMissingUser2 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without password to connect to db1
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingPassword1 = function (next) {
console.log('testMissingPassword1 executing')
var _config = tools.object.clone(configTemplate)
_config.connection1.password = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingPassword1 success')
_successCount++
} else {
console.log('testMissingPassword1 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without password to connect to db2
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingPassword2 = function (next) {
console.log('testMissingPassword2 executing')
var _config = tools.object.clone(configTemplate)
_config.connection2.password = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingPassword2 success')
_successCount++
} else {
console.log('testMissingPassword2 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without database name of db1
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingDatabase1 = function (next) {
console.log('testMissingDatabase1 executing')
var _config = tools.object.clone(configTemplate)
_config.connection1.database = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingDatabase1 success')
_successCount++
} else {
console.log('testMissingDatabase1 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without database name of db2
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingDatabase2 = function (next) {
console.log('testMissingDatabase2 executing')
var _config = tools.object.clone(configTemplate)
_config.connection2.database = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingDatabase2 success')
_successCount++
} else {
console.log('testMissingDatabase2 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file without schema parameters
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testMissingSchema = function (next) {
console.log('testMissingSchema executing')
var _config = tools.object.clone(configTemplate)
_config.compare.schema = null
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testMissingSchema success')
_successCount++
} else {
console.log('testMissingSchema error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file with wrong connection parameters for db1
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testWrongConnection1 = function (next) {
console.log('testWrongConnection1 executing')
var _config = tools.object.clone(configTemplate)
_config.connection1.password = 'xxx'
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testWrongConnection1 success')
_successCount++
} else {
console.log('testWrongConnection1 error')
_errorCount++
}
next && next()
})
}
/**
* Try to pass a config file with wrong connection parameters for db2
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testWrongConnection2 = function (next) {
console.log('testWrongConnection2 executing')
var _config = tools.object.clone(configTemplate)
_config.connection2.password = 'xxx'
compare.run(JSON.stringify(_config), function (err) {
if (err) {
console.log('testWrongConnection2 success')
_successCount++
} else {
console.log('testWrongConnection2 error')
_errorCount++
}
next && next()
})
}
/**
* Connect to 2 dbs, execute scripts in input and then compare them
* @function
* @param {object} prm
* @param {string} prm.sql1 Sql script to execute to db1
* @param {string} prm.sql2 Sql script to execute to db2
* @param {string} prm.sqlDifference Sql delta that is expected to be generated
* @param {object} prm.schema Schema object with items to compare
* @param {string} prm.deltaKey Db item that has to be compared
* @param {function} callback Callback function
*/
var _deltaCompare = function (prm, callback) {
// / use configuration template and adjust it with input parameters
var _config = tools.object.clone(configTemplate)
_config.compare.schema = prm.schema
if (!prm.full) {
_config.compare.options.mode = null
}
// / create two connections
var _conn1 = _config.connection1
var _conn2 = _config.connection2
var _db1 = new pg.Client(_conn1)
var _db2 = new pg.Client(_conn2)
// / sql to clear all items created by test functions
var _clearSql = 'DROP TABLE IF EXISTS factories CASCADE; \n' +
'DROP TABLE IF EXISTS users CASCADE; \n' +
'DROP TABLE IF EXISTS cities CASCADE; \n' +
'DROP FUNCTION IF EXISTS func(text); \n' +
'DROP FUNCTION IF EXISTS func(text, text); \n' +
'DROP FUNCTION IF EXISTS func2(text, text); \n' +
'DROP INDEX IF EXISTS in_users; \n' +
'DROP INDEX IF EXISTS in_users2; \n' +
'DROP TYPE IF EXISTS fruit; \n' +
'DROP TYPE IF EXISTS sport; \n' +
'DROP VIEW IF EXISTS vw; \n' +
'DROP VIEW IF EXISTS vw2; \n' +
'DROP SEQUENCE IF EXISTS seq_users; \n'
prm.sql1 = _clearSql + prm.sql1
prm.sql2 = _clearSql + prm.sql2
// / try to connect to db1
_db1.connect(function (err) {
if (err) {
console.error('error connecting to database 1:', err)
callback && callback(true)
return
}
// / execute query for db1
_db1.query(
prm.sql1,
function (err, result) {
if (err) {
console.error('error inserting in database 1:', err)
callback && callback(true)
return
}
// / close db1 connection
_db1.end()
// / try to connect to db 2
_db2.connect(function (err) {
if (err) {
console.error('error connecting to database 2:', err)
callback && callback(true)
return
}
// / execute query for db2
_db2.query(
prm.sql2,
function (err, result) {
if (err) {
console.error('error inserting database 2:', err)
callback && callback(true)
return
}
// / close db2 connection
_db2.end()
// / compare 2 databases
compare.run(JSON.stringify(_config), function (err) {
if (!err) {
// / retrieve delta sql
var _differences = compare.getDeltaDifference(prm.deltaKey)
// / compare delta sql with the expected one
if (_differences.join('') === prm.sqlDifference) {
callback && callback(false)
} else {
callback && callback(true)
}
} else {
callback && callback(true)
}
})
})
})
})
})
}
/**
* Compare two identical tables
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareSuccess = function (next) {
console.log('testTableCompareSuccess executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL, ' +
"surname VARCHAR NOT NULL, age INTEGER DEFAULT 10, phone VARCHAR, gender CHAR NOT NULL DEFAULT 'M');\n",
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL, ' +
"surname VARCHAR NOT NULL, age INTEGER DEFAULT 10, phone VARCHAR, gender CHAR NOT NULL DEFAULT 'M');\n",
sqlDifference: '',
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareSuccess error')
_errorCount++
} else {
console.log('testTableCompareSuccess success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 has a table that db2 hasn't
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareMissingTable = function (next) {
console.log('testTableCompareMissingTable executing')
var _owner = configTemplate.compare.options.owner ? configTemplate.compare.options.owner : configTemplate.connection2.user
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
"COMMENT ON table users IS 'users';\n" +
"COMMENT ON column users.name IS 'name'",
sql2: 'CREATE TABLE factories (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n',
sqlDifference: 'CREATE TABLE users (\nid serial NOT NULL,name varchar NOT NULL\n);\n' +
'ALTER TABLE users OWNER TO ' + _owner + ';\n' +
"COMMENT ON TABLE users IS 'users';\n" +
"COMMENT ON COLUMN users.name IS 'name';\n",
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareMissingTable error')
_errorCount++
} else {
console.log('testTableCompareMissingTable success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 has a table comment that db2 hasn't
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareMissingTableComment = function (next) {
console.log('testTableCompareMissingTableComment executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
"COMMENT ON TABLE users IS 'users';\n",
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n',
sqlDifference: "COMMENT ON TABLE users IS 'users';\n",
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareMissingTableComment error')
_errorCount++
} else {
console.log('testTableCompareMissingTableComment success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 and db1 have different table comment
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareWrongTableComment = function (next) {
console.log('testTableCompareWrongTableComment executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
"COMMENT ON TABLE users is 'users';\n",
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
"COMMENT ON TABLE users is 'cities';\n",
sqlDifference: "COMMENT ON TABLE users IS 'users';\n",
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareWrongTableComment error')
_errorCount++
} else {
console.log('testTableCompareWrongTableComment success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 has a column not nullable but in db2 is nullable
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareSetNotNull = function (next) {
console.log('testTableCompareSetNotNull executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n',
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR);\n',
sqlDifference: '/* WARNING: column name of table users is not null but without default value*/\n' +
'ALTER TABLE users ALTER COLUMN name SET NOT NULL;\n',
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareSetNotNull error')
_errorCount++
} else {
console.log('testTableCompareSetNotNull success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 has a column that db2 hasn't it
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareMissingColumn = function (next) {
console.log('testTableCompareMissingColumn executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n',
sql2: 'CREATE TABLE users (id SERIAL NOT NULL);\n',
sqlDifference: 'ALTER TABLE users ADD COLUMN name varchar NOT NULL;\n',
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareMissingColumn error')
_errorCount++
} else {
console.log('testTableCompareMissingColumn success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 has a comment on a column that db2 hasn't it
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareMissingColumnComment = function (next) {
console.log('testTableCompareMissingColumnComment executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
"COMMENT ON COLUMN users.name IS 'nome';\n",
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n',
sqlDifference: "COMMENT ON COLUMN users.name IS 'nome';\n",
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareMissingColumnComment error')
_errorCount++
} else {
console.log('testTableCompareMissingColumnComment success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 and db2 have different comment on same column
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareWrongColumnComment = function (next) {
console.log('testTableCompareMissingColumnComment executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
"COMMENT ON COLUMN users.name IS 'nome';\n",
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
"COMMENT ON COLUMN users.name IS 'cognome';\n",
sqlDifference: "COMMENT ON COLUMN users.name IS 'nome';\n",
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareWrongColumnComment error')
_errorCount++
} else {
console.log('testTableCompareWrongColumnComment success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 has a column with default value but in db2 there isn't default value for the same column
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareMissingDefaultValue = function (next) {
console.log('testTableCompareMissingDefaultValue executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, age INTEGER NOT NULL DEFAULT 10);\n',
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, age INTEGER NOT NULL);\n',
sqlDifference: 'ALTER TABLE users ALTER COLUMN age SET DEFAULT 10;\n',
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareMissingDefaultValue error')
_errorCount++
} else {
console.log('testTableCompareMissingDefaultValue success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 and db2 have different default values for the same column
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareWrongDefaultValue = function (next) {
console.log('testTableCompareWrongDefaultValue executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, age INTEGER NOT NULL DEFAULT 10);\n',
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, age INTEGER NOT NULL DEFAULT 20);\n',
sqlDifference: 'ALTER TABLE users ALTER COLUMN age SET DEFAULT 10;\n',
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareWrongDefaultValue error')
_errorCount++
} else {
console.log('testTableCompareWrongDefaultValue success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 and db2 have different data type for the same column
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareWrongDataType = function (next) {
console.log('testTableCompareWrongDataType executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR);\n',
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, name integer);\n',
sqlDifference: 'ALTER TABLE users ALTER COLUMN name SET DATA TYPE varchar;\n',
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareWrongDataType error')
_errorCount++
} else {
console.log('testTableCompareWrongDataType success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db2 has a column on a table but db1 hasn't it
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareDropColumn = function (next) {
console.log('testTableCompareDropColumn executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL);\n',
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR);\n',
sqlDifference: 'ALTER TABLE users DROP COLUMN name;\n',
full: true,
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareDropColumn error')
_errorCount++
} else {
console.log('testTableCompareDropColumn success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db2 has a table that db1 hasn't it
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testTableCompareDropTable = function (next) {
console.log('testTableCompareDropTable executing')
_deltaCompare({
sql1: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR);\n',
sql2: 'CREATE TABLE users (id SERIAL NOT NULL, name VARCHAR);\n' +
'CREATE TABLE factories (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n',
sqlDifference: 'DROP TABLE factories;',
full: true,
schema: {
tables: true
},
deltaKey: 'tables'
}, function (err) {
if (err) {
console.log('testTableCompareDropTable error')
_errorCount++
} else {
console.log('testTableCompareDropTable success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: they have same primary keys
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testPkeysCompareSuccess = function (next) {
console.log('testPkeysCompareSuccess executing')
_deltaCompare({
sql1: 'CREATE TABLE factories (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
'ALTER TABLE factories ADD CONSTRAINT pk_factories PRIMARY KEY (id);\n',
sql2: 'CREATE TABLE factories (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
'ALTER TABLE factories ADD CONSTRAINT pk_factories PRIMARY KEY (id);\n',
sqlDifference: '--primary keys match',
schema: {
tables: true
},
deltaKey: 'pkeys'
}, function (err) {
if (err) {
console.log('testPkeysCompareSuccess error')
_errorCount++
} else {
console.log('testPkeysCompareSuccess success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 has a primary key but db2 hasn't it
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testPkeysCompareMissing = function (next) {
console.log('testPkeysCompareMissing executing')
_deltaCompare({
sql1: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n' +
'ALTER TABLE users ADD CONSTRAINT pk_users PRIMARY KEY (name, surname);\n',
sql2: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n',
sqlDifference: 'ALTER TABLE users\n ADD CONSTRAINT pk_users PRIMARY KEY (name,surname);',
schema: {
tables: true
},
deltaKey: 'pkeys'
}, function (err) {
if (err) {
console.log('testPkeysCompareMissing error')
_errorCount++
} else {
console.log('testPkeysCompareMissing success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 and db2 have different definitions for the same primary key
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testPkeysCompareWrongFields = function (next) {
console.log('testPkeysCompareSuccess executing')
_deltaCompare({
sql1: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n' +
'ALTER TABLE users ADD CONSTRAINT pk_users PRIMARY KEY (name, surname);\n',
sql2: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n' +
'ALTER TABLE users ADD CONSTRAINT pk_users PRIMARY KEY (name);\n',
sqlDifference: 'ALTER TABLE users\n DROP CONSTRAINT pk_users;\n' +
'ALTER TABLE users\n ADD CONSTRAINT pk_users PRIMARY KEY (name,surname);',
schema: {
tables: true
},
deltaKey: 'pkeys'
}, function (err) {
if (err) {
console.log('testPkeysCompareSuccess error')
_errorCount++
} else {
console.log('testPkeysCompareSuccess success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db2 has a primary key but db1 hasn't it
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testPkeysCompareDrop = function (next) {
console.log('testPkeysCompareDrop executing')
_deltaCompare({
sql1: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n' +
'CREATE TABLE factories (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
'ALTER TABLE factories ADD CONSTRAINT pk_factories PRIMARY KEY (id);\n',
sql2: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n' +
'ALTER TABLE users ADD CONSTRAINT pk_users PRIMARY KEY (name, surname);\n' +
'CREATE TABLE factories (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
'ALTER TABLE factories ADD CONSTRAINT pk_factories PRIMARY KEY (id);\n',
sqlDifference: 'ALTER TABLE users\n DROP CONSTRAINT pk_users;',
full: true,
schema: {
tables: true
},
deltaKey: 'pkeys'
}, function (err) {
if (err) {
console.log('testPkeysCompareDrop error')
_errorCount++
} else {
console.log('testPkeysCompareDrop success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 and db2 have the same unique constraints
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testUniqueCompareSuccess = function (next) {
console.log('testUniqueCompareSuccess executing')
_deltaCompare({
sql1: 'CREATE TABLE factories (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
'ALTER TABLE factories ADD CONSTRAINT un_factories UNIQUE (id);\n',
sql2: 'CREATE TABLE factories (id SERIAL NOT NULL, name VARCHAR NOT NULL);\n' +
'ALTER TABLE factories ADD CONSTRAINT un_factories UNIQUE (id);\n',
sqlDifference: '--unique constraints match',
schema: {
tables: true
},
deltaKey: 'unique'
}, function (err) {
if (err) {
console.log('testUniqueCompareSuccess error')
_errorCount++
} else {
console.log('testUniqueCompareSuccess success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 has an unique constraint but db2 hasn't it
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testUniqueCompareMissing = function (next) {
console.log('testUniqueCompareMissing executing')
_deltaCompare({
sql1: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n' +
'ALTER TABLE users ADD CONSTRAINT un_users UNIQUE (name, surname);\n',
sql2: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n',
sqlDifference: 'ALTER TABLE users\n ADD CONSTRAINT un_users UNIQUE (name,surname);',
schema: {
tables: true
},
deltaKey: 'unique'
}, function (err) {
if (err) {
console.log('testUniqueCompareMissing error')
_errorCount++
} else {
console.log('testUniqueCompareMissing success')
_successCount++
}
next && next()
})
}
/**
* Compare dbs: db1 and db2 have different definitions for the same unique constraint
* @function
* @param {function} next Function to execute after finishing this test function
*/
var testUniqueCompareWrongFields = function (next) {
console.log('testUniqueCompareWrongFields executing')
_deltaCompare({
sql1: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n' +
'ALTER TABLE users ADD CONSTRAINT un_users UNIQUE (name, surname);\n',
sql2: 'CREATE TABLE users (name VARCHAR NOT NULL, surname VARCHAR NOT NULL, age INTEGER);\n' +
'ALTER TABLE users ADD CONSTRAINT un_users UNIQUE (name);\n',
sqlDifference: 'ALTER TABLE users\n DROP CONSTRAINT un_users;\n' +
'ALTER TABLE users\n ADD CONSTRAINT un_users UNIQUE (name,surname);',
schema: {
tables: true
},