DatabaseSchemaChecker.php 26.3 KB
Newer Older
Cédric Anne's avatar
Cédric Anne committed
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
<?php
/**
 * ---------------------------------------------------------------------
 * GLPI - Gestionnaire Libre de Parc Informatique
 * Copyright (C) 2015-2021 Teclib' and contributors.
 *
 * http://glpi-project.org
 *
 * based on GLPI - Gestionnaire Libre de Parc Informatique
 * Copyright (C) 2003-2014 by the INDEPNET Development Team.
 *
 * ---------------------------------------------------------------------
 *
 * LICENSE
 *
 * This file is part of GLPI.
 *
 * GLPI is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GLPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GLPI. If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
 */

namespace tests\units\Glpi\System\Diagnostic;

35
class DatabaseSchemaChecker extends \GLPITestCase {
Cédric Anne's avatar
Cédric Anne committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

   protected function sqlProvider() {
      return [
         // AUTO_INCREMENT, integer display width, and comments should not be included in differences.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `is_valid` tinyint NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='some comment with an escaped \' backquote'
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL COMMENT 'name of the object',
  `is_valid` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15
SQL
            ,
59
            'version_string' => '5.7.50-log',
Cédric Anne's avatar
Cédric Anne committed
60
61
62
63
64
65
            'args'           => [
            ],
            'expected_has'   => false,
            'expected_diff'  => '',
         ],

Cédric Anne's avatar
Cédric Anne committed
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
         // Check should detect missing keys and columns.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text,
  `field` text,
  `computers_id` tinyint NOT NULL,
  `is_valid` tinyint NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unicity` (`computers_id`,`is_valid`),
  UNIQUE KEY `name` (`name`),
  FULLTEXT KEY `content` (`content`),
  FULLTEXT KEY `field` (`field`),
  KEY `computers_id` (`computers_id`),
  KEY `is_valid` (`is_valid`)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text,
  `is_valid` tinyint NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  FULLTEXT KEY `content` (`content`),
  KEY `is_valid` (`is_valid`),
) ENGINE=InnoDB
SQL
            ,
            'version_string' => '5.7.34-standard',
            'args'           => [
               'strict' => false,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
-  `computers_id` tinyint NOT NULL,
   `content` text,
-  `field` text,
   `id` int NOT NULL AUTO_INCREMENT,
   `is_valid` tinyint NOT NULL,
   `name` varchar(255) NOT NULL,
   FULLTEXT KEY `content` (`content`),
-  FULLTEXT KEY `field` (`field`),
-  KEY `computers_id` (`computers_id`),
   KEY `is_valid` (`is_valid`),
   PRIMARY KEY (`id`),
-  UNIQUE KEY `name` (`name`),
-  UNIQUE KEY `unicity` (`computers_id`,`is_valid`)
+  UNIQUE KEY `name` (`name`)
 ) ENGINE=InnoDB

DIFF
            ,
         ],

Cédric Anne's avatar
Cédric Anne committed
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
         // Non strict check does not take care of columns/indexes order and ROW_FORMAT.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `is_valid` tinyint NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `is_valid` (`is_valid`)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `is_valid` tinyint NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `is_valid` (`is_valid`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB
SQL
            ,
            'version_string' => '5.7.34-standard',
            'args'           => [
               'strict' => false,
            ],
            'expected_has'   => false,
            'expected_diff'  => '',
         ],

         // Strict check takes care of columns/indexes order and ROW_FORMAT.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `is_valid` tinyint NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `is_valid` (`is_valid`)
) ENGINE=InnoDB
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `is_valid` tinyint NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `is_valid` (`is_valid`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC
SQL
            ,
            'version_string' => '5.7.34-standard',
            'args'           => [
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
   `id` int NOT NULL AUTO_INCREMENT,
+  `is_valid` tinyint NOT NULL,
   `name` varchar(255) NOT NULL,
-  `is_valid` tinyint NOT NULL,
   PRIMARY KEY (`id`),
-  UNIQUE KEY `name` (`name`),
-  KEY `is_valid` (`is_valid`)
-) ENGINE=InnoDB
+  KEY `is_valid` (`is_valid`),
+  UNIQUE KEY `name` (`name`)
+) ENGINE=InnoDB ROW_FORMAT=DYNAMIC

DIFF
            ,
         ],

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
         // utf8mb3 should be normalized to utf8.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
SQL
            ,
            'version_string' => '8.0.24-standard',
            'args'           => [
               'use_utf8mb4' => false,
            ],
            'expected_has'   => false,
            'expected_diff'  => '',
         ],

Cédric Anne's avatar
Cédric Anne committed
238
239
240
241
         // DB using utf8mb4:
         // - should accept missing default charset/collate on columns if matching utf8mb4;
         // - should not accept non utf8mb4 charset;
         // - should accept 'mediumtext' instead of 'text'.
242
         // - should accept 'longtext' instead of 'mediumtext'.
Cédric Anne's avatar
Cédric Anne committed
243
244
245
246
247
248
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text,
249
  `description` mediumtext,
Cédric Anne's avatar
Cédric Anne committed
250
251
252
253
254
255
256
257
258
259
  `bis` varchar(100),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL CHARACTER SET utf8 COLLATE utf8_unicode_ci,
  `content` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
260
  `description` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Cédric Anne's avatar
Cédric Anne committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  `bis` varchar(100) CHARSET latin1,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SQL
            ,
            'version_string' => '5.7.34-standard',
            'args'           => [
               'use_utf8mb4' => true,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
   `id` int NOT NULL AUTO_INCREMENT,
-  `name` varchar(255) NOT NULL,
+  `name` varchar(255) NOT NULL CHARACTER SET utf8 COLLATE utf8_unicode_ci,
   `content` text,
280
   `description` text,
Cédric Anne's avatar
Cédric Anne committed
281
282
283
284
285
286
287
288
289
290
291
292
293
-  `bis` varchar(100),
+  `bis` varchar(100) CHARSET latin1,
   PRIMARY KEY (`id`)
 ) COLLATE=utf8mb4_unicode_ci DEFAULT CHARSET=utf8mb4 ENGINE=InnoDB

DIFF
            ,
         ],

         // DB using utf8:
         // - should accept missing default charset/collate on columns if matching utf8;
         // - should not accept non utf8 charset;
         // - should not accept 'mediumtext' instead of 'text'.
294
         // - should not accept 'longtext' instead of 'mediumtext'.
Cédric Anne's avatar
Cédric Anne committed
295
296
297
298
299
300
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text,
301
  `description` mediumtext,
Cédric Anne's avatar
Cédric Anne committed
302
303
304
305
306
307
308
309
310
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `content` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
311
  `description` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
Cédric Anne's avatar
Cédric Anne committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
SQL
            ,
            'version_string' => '5.7.34-standard',
            'args'           => [
               'use_utf8mb4' => false,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
   `id` int NOT NULL AUTO_INCREMENT,
-  `name` varchar(255) NOT NULL,
-  `content` text,
329
-  `description` mediumtext,
Cédric Anne's avatar
Cédric Anne committed
330
331
+  `name` varchar(255) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
+  `content` mediumtext,
332
+  `description` longtext,
Cédric Anne's avatar
Cédric Anne committed
333
334
335
   PRIMARY KEY (`id`)
 ) COLLATE=utf8_unicode_ci DEFAULT CHARSET=utf8 ENGINE=InnoDB

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
DIFF
            ,
         ],

         // DB using utf8mb3:
         // - should accept missing default charset/collate on columns if matching utf8/utf8mb3;
         // - should not accept non utf8/utf8mb3 charset;
         // - should not accept 'mediumtext' instead of 'text'.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `content` mediumtext CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
SQL
            ,
            'version_string' => '8.0.24-standard',
            'args'           => [
               'use_utf8mb4' => false,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
   `id` int NOT NULL AUTO_INCREMENT,
-  `name` varchar(255) NOT NULL,
-  `content` text,
+  `name` varchar(255) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
+  `content` mediumtext,
   PRIMARY KEY (`id`)
 ) COLLATE=utf8_unicode_ci DEFAULT CHARSET=utf8 ENGINE=InnoDB

Cédric Anne's avatar
Cédric Anne committed
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
DIFF
            ,
         ],

         // Charset/collation difference should be ignored if related to utf8mb4 migration
         // when using ignore_utf8mb4_migration flag.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
SQL
            ,
406
            'version_string' => '10.2.36-MariaDB',
Cédric Anne's avatar
Cédric Anne committed
407
408
409
410
411
412
413
414
            'args'           => [
               'use_utf8mb4' => false,
               'ignore_utf8mb4_migration' => true,
            ],
            'expected_has'   => false,
            'expected_diff'  => '',
         ],

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
         // Charset/collation difference should be ignored if related to utf8mb4 migration
         // when using ignore_utf8mb4_migration flag.
         // utf8mb3 case
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `content` text CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
SQL
            ,
            'version_string' => '8.0.24-standard',
            'args'           => [
               'use_utf8mb4' => false,
               'ignore_utf8mb4_migration' => true,
            ],
            'expected_has'   => false,
            'expected_diff'  => '',
         ],

Cédric Anne's avatar
Cédric Anne committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
         // Charset/collation difference should NOT be ignored if related to utf8mb4 migration
         // when NOT using ignore_utf8mb4_migration flag.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
SQL
            ,
467
            'version_string' => '10.2.36-MariaDB',
Cédric Anne's avatar
Cédric Anne committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
            'args'           => [
               'use_utf8mb4' => false,
               'ignore_utf8mb4_migration' => false,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
   `id` int NOT NULL AUTO_INCREMENT,
   `name` varchar(255) NOT NULL,
-  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
+  `content` text,
   PRIMARY KEY (`id`)
-) COLLATE=utf8mb4_unicode_ci DEFAULT CHARSET=utf8mb4 ENGINE=InnoDB
+) COLLATE=utf8_unicode_ci DEFAULT CHARSET=utf8 ENGINE=InnoDB

DIFF
            ,
         ],

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
         // Charset/collation difference should NOT be ignored if related to utf8mb4 migration
         // when NOT using ignore_utf8mb4_migration flag.
         // utf8mb3 case
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
SQL
            ,
            'version_string' => '8.0.24-standard',
            'args'           => [
               'use_utf8mb4' => false,
               'ignore_utf8mb4_migration' => false,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
   `id` int NOT NULL AUTO_INCREMENT,
   `name` varchar(255) NOT NULL,
-  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
+  `content` text,
   PRIMARY KEY (`id`)
-) COLLATE=utf8mb4_unicode_ci DEFAULT CHARSET=utf8mb4 ENGINE=InnoDB
+) COLLATE=utf8_unicode_ci DEFAULT CHARSET=utf8 ENGINE=InnoDB

DIFF
            ,
         ],

         // Charset/collation difference should NOT be ignored if not related to utf8mb4 migration
Cédric Anne's avatar
Cédric Anne committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
         // when using ignore_utf8mb4_migration flag.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL CHARACTER SET utf8 COLLATE utf8_unicode_ci,
  `content` text CHARACTER SET latin1 COLLATE latin1_general_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
SQL
            ,
556
            'version_string' => '10.2.36-MariaDB',
Cédric Anne's avatar
Cédric Anne committed
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
            'args'           => [
               'use_utf8mb4' => false,
               'ignore_utf8mb4_migration' => true,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
   `id` int NOT NULL AUTO_INCREMENT,
   `name` varchar(255) NOT NULL,
-  `content` text,
+  `content` text CHARACTER SET latin1 COLLATE latin1_general_ci,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB

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
DIFF
            ,
         ],

         // Charset/collation difference should NOT be ignored if not related to utf8mb4 migration
         // when using ignore_utf8mb4_migration flag.
         // utf8mb3 case
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `content` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci,
  `content` text CHARACTER SET latin1 COLLATE latin1_general_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
SQL
            ,
            'version_string' => '8.0.24-standard',
            'args'           => [
               'use_utf8mb4' => false,
               'ignore_utf8mb4_migration' => true,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
   `id` int NOT NULL AUTO_INCREMENT,
   `name` varchar(255) NOT NULL,
-  `content` text,
+  `content` text CHARACTER SET latin1 COLLATE latin1_general_ci,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB

Cédric Anne's avatar
Cédric Anne committed
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
DIFF
            ,
         ],

         // datetime/timestamp difference should be ignored when using ignore_timestamps_migration flag.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `created_at` timestamp NOT NULL,
  `updated_at` timestamp NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `created_at` datetime NOT NULL,
  `updated_at` datetime,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
SQL
            ,
642
            'version_string' => '10.2.36-MariaDB',
Cédric Anne's avatar
Cédric Anne committed
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
            'args'           => [
               'ignore_timestamps_migration' => true,
            ],
            'expected_has'   => false,
            'expected_diff'  => '',
         ],

         // datetime/timestamp difference should NOT be ignored when NOT using ignore_timestamps_migration flag.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `created_at` timestamp NOT NULL,
  `updated_at` timestamp NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `created_at` datetime NOT NULL,
  `updated_at` datetime,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
SQL
            ,
670
            'version_string' => '10.2.36-MariaDB',
Cédric Anne's avatar
Cédric Anne committed
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
            'args'           => [
               'ignore_timestamps_migration' => false,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
 CREATE TABLE `table` (
   `id` int NOT NULL AUTO_INCREMENT,
-  `created_at` timestamp NOT NULL,
-  `updated_at` timestamp NULL,
+  `created_at` datetime NOT NULL,
+  `updated_at` datetime,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB

DIFF
            ,
         ],

         // ROW_FORMAT difference should be ignored when using ignore_dynamic_row_format_migration flag.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB ROW_FORMAT=COMPACT
SQL
            ,
            'version_string' => '10.2.36-MariaDB',
            'args'           => [
               'ignore_dynamic_row_format_migration' => true,
            ],
            'expected_has'   => false,
            'expected_diff'  => '',
         ],

         // ROW_FORMAT difference should NOT be ignored when NOT using ignore_dynamic_row_format_migration flag.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB ROW_FORMAT=COMPACT
SQL
            ,
            'version_string' => '10.2.36-MariaDB',
            'args'           => [
               'ignore_dynamic_row_format_migration' => false,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
   `id` int NOT NULL AUTO_INCREMENT,
   `name` varchar(255) NOT NULL,
   PRIMARY KEY (`id`)
-) ENGINE=InnoDB
+) ENGINE=InnoDB ROW_FORMAT=COMPACT

DIFF
         ],

         // ENGINE difference should be ignored when using ignore_innodb_migration flag.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM
SQL
            ,
            'version_string' => '10.2.36-MariaDB',
            'args'           => [
               'ignore_innodb_migration' => true,
            ],
            'expected_has'   => false,
            'expected_diff'  => '',
         ],

         // ENGINE difference should NOT be ignored when NOT using ignore_innodb_migration flag.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM
SQL
            ,
            'version_string' => '10.2.36-MariaDB',
            'args'           => [
               'ignore_innodb_migration' => false,
            ],
            'expected_has'   => true,
            'expected_diff'  => <<<DIFF
--- Original
+++ New
@@ @@
   `id` int NOT NULL AUTO_INCREMENT,
   `name` varchar(255) NOT NULL,
   PRIMARY KEY (`id`)
-) ENGINE=InnoDB
+) ENGINE=MyISAM

DIFF
         ],

         // DB on MariaDB 10.2+ resuls should be normalized by:
         // - surrounding default numeric values by quotes;
         // - replacing current_timestamp() by CURRENT_TIMESTAMP;
         // - removing DEFAULT NULL on text fields.
         [
            'proper_sql'     => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `description` text NULL,
  `value` int NOT NULL DEFAULT '0',
  `steps` float NOT NULL DEFAULT '-0.7',
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB
SQL
            ,
            'effective_sql'  => <<<SQL
CREATE TABLE `table` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `description` text NULL DEFAULT NULL,
  `value` int NOT NULL DEFAULT 0,
  `steps` float NOT NULL DEFAULT -0.7,
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=MyISAM
SQL
            ,
            'version_string' => '10.2.36-MariaDB',
            'args'           => [
               'ignore_innodb_migration' => true,
            ],
            'expected_has'   => false,
            'expected_diff'  => '',
         ],
      ];
   }

   /**
    * @dataProvider sqlProvider
    */
   public function testDifferences(
      string $proper_sql,
      string $effective_sql,
      string $version_string,
      array $args,
      bool $expected_has,
      string $expected_diff
   ) {

      $this->mockGenerator->orphanize('__construct');

      $db = new \mock\DBmysql();
      $db->use_utf8mb4 = $args['use_utf8mb4'] ?? true;
      $this->calling($db)->getVersion = $version_string;

      $this->mockGenerator->orphanize('__construct');
      $query_result = new \mock\mysqli_result();
      $this->calling($query_result)->fetch_assoc = ['Create Table' => $effective_sql];
      $this->calling($db)->query = $query_result;

      $this->newTestedInstance(
         $db,
         $args['strict'] ?? true,
         $args['ignore_innodb_migration'] ?? false,
         $args['ignore_timestamps_migration'] ?? false,
         $args['ignore_utf8mb4_migration'] ?? false,
         $args['ignore_dynamic_row_format_migration'] ?? false
      );

      $this->boolean($this->testedInstance->hasDifferences('table', $proper_sql))->isEqualTo($expected_has);
      $this->string($this->testedInstance->getDiff('table', $proper_sql))->isEqualTo($expected_diff);
   }
}