@@ -393,18 +393,50 @@ private function deleteTableFor58CreateMigrationForColumnPositionChangeIfAFieldP
393
393
Yii::$ app ->db ->createCommand ('DROP TABLE IF EXISTS {{%fruits}} ' )->execute ();
394
394
}
395
395
396
- public function test58DeleteLastCol ( )
396
+ private function for58 ( $ schema , $ expected )
397
397
{
398
398
$ deleteTable = function () {
399
399
Yii::$ app ->db ->createCommand ('DROP TABLE IF EXISTS {{%fruits}} ' )->execute ();
400
400
};
401
401
$ createTable = function () {
402
402
Yii::$ app ->db ->createCommand ()->createTable ('{{%fruits}} ' , [
403
403
'id ' => 'pk ' ,
404
- 'description ' => 'text not null ' ,
405
404
'name ' => 'text not null ' ,
405
+ 'description ' => 'text not null ' ,
406
+ 'colour ' => 'text not null ' ,
407
+ 'size ' => 'text not null ' ,
406
408
])->execute ();
407
409
};
410
+
411
+ $ config = [
412
+ 'openApiPath ' => 'data://text/plain;base64, ' .base64_encode ($ schema ),
413
+ 'generateUrls ' => false ,
414
+ 'generateModels ' => false ,
415
+ 'generateControllers ' => false ,
416
+ 'generateMigrations ' => true ,
417
+ 'generateModelFaker ' => false ,
418
+ ];
419
+ $ tmpConfigFile = Yii::getAlias ("@runtime " )."/tmp-config.php " ;
420
+ file_put_contents ($ tmpConfigFile , '<?php return ' .var_export ($ config , true ).'; ' );
421
+
422
+ foreach (['Mysql ' , 'Mariadb ' ] as $ db ) {
423
+ $ this ->{"changeDbTo $ db " }();
424
+ $ deleteTable ();
425
+ $ createTable ();
426
+
427
+ $ dbStr = str_replace ('db ' , '' , strtolower ($ db ));
428
+ $ this ->runGenerator ($ tmpConfigFile , $ dbStr );
429
+ $ this ->runActualMigrations ($ dbStr , 1 );
430
+ $ actual = file_get_contents (Yii::getAlias ('@app ' ).'/migrations_ ' .$ dbStr .'_db/m200000_000000_change_table_fruits.php ' );
431
+ $ this ->assertSame ($ expected , $ actual );
432
+
433
+ $ deleteTable ();
434
+ }
435
+ FileHelper::unlink ($ tmpConfigFile );
436
+ }
437
+
438
+ public function test58DeleteLastCol ()
439
+ {
408
440
$ schema = <<<YAML
409
441
openapi: 3.0.3
410
442
info:
@@ -417,26 +449,22 @@ public function test58DeleteLastCol()
417
449
properties:
418
450
id:
419
451
type: integer
452
+ name:
453
+ type: string
454
+ nullable: false
420
455
description:
421
456
type: string
422
457
nullable: false
458
+ colour:
459
+ type: string
460
+ nullable: false
423
461
paths:
424
462
'/':
425
463
get:
426
464
responses:
427
465
'200':
428
466
description: OK
429
467
YAML ;
430
- $ config = [
431
- 'openApiPath ' => 'data://text/plain;base64, ' .base64_encode ($ schema ),
432
- 'generateUrls ' => false ,
433
- 'generateModels ' => false ,
434
- 'generateControllers ' => false ,
435
- 'generateMigrations ' => true ,
436
- 'generateModelFaker ' => false ,
437
- ];
438
- $ tmpConfigFile = Yii::getAlias ("@runtime " )."/tmp-config.php " ;
439
- file_put_contents ($ tmpConfigFile , '<?php return ' .var_export ($ config , true ).'; ' );
440
468
441
469
$ expected = <<<'PHP'
442
470
<?php
@@ -448,30 +476,234 @@ class m200000_000000_change_table_fruits extends \yii\db\Migration
448
476
{
449
477
public function up()
450
478
{
451
- $this->dropColumn('{{%fruits}}', 'name ');
479
+ $this->dropColumn('{{%fruits}}', 'size ');
452
480
}
453
481
454
482
public function down()
455
483
{
456
- $this->addColumn('{{%fruits}}', 'name ', $this->text()->notNull()->after('description '));
484
+ $this->addColumn('{{%fruits}}', 'size ', $this->text()->notNull()->after('colour '));
457
485
}
458
486
}
459
487
460
488
PHP;
461
489
462
- foreach (['Mysql ' , 'Mariadb ' ] as $ db ) {
463
- $ this ->{"changeDbTo $ db " }();
464
- $ deleteTable ();
465
- $ createTable ();
490
+ $ this ->for58 ($ schema , $ expected );
491
+ }
466
492
467
- $ dbStr = str_replace ('db ' , '' , strtolower ($ db ));
468
- $ this ->runGenerator ($ tmpConfigFile , $ dbStr );
469
- $ this ->runActualMigrations ($ dbStr , 1 );
470
- $ actual = file_get_contents (Yii::getAlias ('@app ' ).'/migrations_ ' .$ dbStr .'_db/m200000_000000_change_table_fruits.php ' );
471
- $ this ->assertSame ($ expected , $ actual );
472
-
473
- $ deleteTable ();
474
- }
475
- FileHelper::unlink ($ tmpConfigFile );
493
+ public function test58DeleteLast2ConsecutiveCol ()
494
+ {
495
+ $ schema = <<<YAML
496
+ openapi: 3.0.3
497
+ info:
498
+ title: 'test58DeleteLastCol'
499
+ version: 1.0.0
500
+ components:
501
+ schemas:
502
+ Fruit:
503
+ type: object
504
+ properties:
505
+ id:
506
+ type: integer
507
+ name:
508
+ type: string
509
+ nullable: false
510
+ description:
511
+ type: string
512
+ nullable: false
513
+ paths:
514
+ '/':
515
+ get:
516
+ responses:
517
+ '200':
518
+ description: OK
519
+ YAML ;
520
+
521
+ $ expected = <<<'PHP'
522
+ <?php
523
+
524
+ /**
525
+ * Table for Fruit
526
+ */
527
+ class m200000_000000_change_table_fruits extends \yii\db\Migration
528
+ {
529
+ public function up()
530
+ {
531
+ $this->dropColumn('{{%fruits}}', 'colour');
532
+ $this->dropColumn('{{%fruits}}', 'size');
533
+ }
534
+
535
+ public function down()
536
+ {
537
+ $this->addColumn('{{%fruits}}', 'size', $this->text()->notNull());
538
+ $this->addColumn('{{%fruits}}', 'colour', $this->text()->notNull()->after('description'));
539
+ }
540
+ }
541
+
542
+ PHP;
543
+
544
+ $ this ->for58 ($ schema , $ expected );
545
+ }
546
+
547
+ public function test58DeleteAColInBetween ()
548
+ {
549
+ $ schema = <<<YAML
550
+ openapi: 3.0.3
551
+ info:
552
+ title: 'test58DeleteLastCol'
553
+ version: 1.0.0
554
+ components:
555
+ schemas:
556
+ Fruit:
557
+ type: object
558
+ properties:
559
+ id:
560
+ type: integer
561
+ name:
562
+ type: string
563
+ nullable: false
564
+ colour:
565
+ type: string
566
+ nullable: false
567
+ size:
568
+ type: string
569
+ nullable: false
570
+ paths:
571
+ '/':
572
+ get:
573
+ responses:
574
+ '200':
575
+ description: OK
576
+ YAML ;
577
+
578
+ $ expected = <<<'PHP'
579
+ <?php
580
+
581
+ /**
582
+ * Table for Fruit
583
+ */
584
+ class m200000_000000_change_table_fruits extends \yii\db\Migration
585
+ {
586
+ public function up()
587
+ {
588
+ $this->dropColumn('{{%fruits}}', 'description');
589
+ }
590
+
591
+ public function down()
592
+ {
593
+ $this->addColumn('{{%fruits}}', 'description', $this->text()->notNull()->after('name'));
594
+ }
595
+ }
596
+
597
+ PHP;
598
+
599
+ $ this ->for58 ($ schema , $ expected );
600
+ }
601
+
602
+ public function test58Delete2ConsecutiveColInBetween ()
603
+ {
604
+ $ schema = <<<YAML
605
+ openapi: 3.0.3
606
+ info:
607
+ title: 'test58DeleteLastCol'
608
+ version: 1.0.0
609
+ components:
610
+ schemas:
611
+ Fruit:
612
+ type: object
613
+ properties:
614
+ id:
615
+ type: integer
616
+ name:
617
+ type: string
618
+ nullable: false
619
+ size:
620
+ type: string
621
+ nullable: false
622
+ paths:
623
+ '/':
624
+ get:
625
+ responses:
626
+ '200':
627
+ description: OK
628
+ YAML ;
629
+
630
+ $ expected = <<<'PHP'
631
+ <?php
632
+
633
+ /**
634
+ * Table for Fruit
635
+ */
636
+ class m200000_000000_change_table_fruits extends \yii\db\Migration
637
+ {
638
+ public function up()
639
+ {
640
+ $this->dropColumn('{{%fruits}}', 'description');
641
+ $this->dropColumn('{{%fruits}}', 'colour');
642
+ }
643
+
644
+ public function down()
645
+ {
646
+ $this->addColumn('{{%fruits}}', 'colour', $this->text()->notNull());
647
+ $this->addColumn('{{%fruits}}', 'description', $this->text()->notNull()->after('name'));
648
+ }
649
+ }
650
+
651
+ PHP;
652
+
653
+ $ this ->for58 ($ schema , $ expected );
654
+ }
655
+
656
+ public function test58Delete2NonConsecutiveColInBetween ()
657
+ {
658
+ $ schema = <<<YAML
659
+ openapi: 3.0.3
660
+ info:
661
+ title: 'test58DeleteLastCol'
662
+ version: 1.0.0
663
+ components:
664
+ schemas:
665
+ Fruit:
666
+ type: object
667
+ properties:
668
+ id:
669
+ type: integer
670
+ description:
671
+ type: string
672
+ nullable: false
673
+ size:
674
+ type: string
675
+ nullable: false
676
+ paths:
677
+ '/':
678
+ get:
679
+ responses:
680
+ '200':
681
+ description: OK
682
+ YAML ;
683
+
684
+ $ expected = <<<'PHP'
685
+ <?php
686
+
687
+ /**
688
+ * Table for Fruit
689
+ */
690
+ class m200000_000000_change_table_fruits extends \yii\db\Migration
691
+ {
692
+ public function up()
693
+ {
694
+ $this->dropColumn('{{%fruits}}', 'name');
695
+ $this->dropColumn('{{%fruits}}', 'colour');
696
+ }
697
+
698
+ public function down()
699
+ {
700
+ $this->addColumn('{{%fruits}}', 'colour', $this->text()->notNull()->after('description'));
701
+ $this->addColumn('{{%fruits}}', 'name', $this->text()->notNull()->after('id'));
702
+ }
703
+ }
704
+
705
+ PHP;
706
+
707
+ $ this ->for58 ($ schema , $ expected );
476
708
}
477
709
}
0 commit comments