@@ -5583,6 +5583,49 @@ public function testReversibleMigrationWithIndexConflict()
55835583 $ this ->assertFalse ($ adapter ->hasIndex ('my_table ' , ['entity_id ' ]));
55845584 }
55855585
5586+ public function testReversibleMigrationWithFKConflictOnTableDrop ()
5587+ {
5588+ if (!TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED ) {
5589+ $ this ->markTestSkipped ('Mysql tests disabled. See TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED constant. ' );
5590+ }
5591+ $ configArray = $ this ->getConfigArray ();
5592+ $ adapter = $ this ->manager ->getEnvironment ('production ' )->getAdapter ();
5593+
5594+ // override the migrations directory to use the reversible migrations
5595+ $ configArray ['paths ' ]['migrations ' ] = $ this ->getCorrectedPath (__DIR__ . '/_files/drop_table_with_fk_regression ' );
5596+ $ config = new Config ($ configArray );
5597+
5598+ // ensure the database is empty
5599+ $ adapter ->dropDatabase (TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE );
5600+ $ adapter ->createDatabase (TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE );
5601+ $ adapter ->disconnect ();
5602+
5603+ // migrate to the latest version
5604+ $ this ->manager ->setConfig ($ config );
5605+ $ this ->manager ->migrate ('production ' );
5606+
5607+ // ensure up migrations worked
5608+ $ this ->assertTrue ($ adapter ->hasTable ('orders ' ));
5609+ $ this ->assertTrue ($ adapter ->hasTable ('customers ' ));
5610+ $ this ->assertTrue ($ adapter ->hasColumn ('orders ' , 'order_date ' ));
5611+ $ this ->assertTrue ($ adapter ->hasColumn ('orders ' , 'customer_id ' ));
5612+ $ this ->assertTrue ($ adapter ->hasForeignKey ('orders ' , ['customer_id ' ]));
5613+
5614+ // revert all changes to the first
5615+ $ this ->manager ->rollback ('production ' , '20190928205056 ' );
5616+
5617+ // ensure reversed migrations worked
5618+ $ this ->assertTrue ($ adapter ->hasTable ('orders ' ));
5619+ $ this ->assertTrue ($ adapter ->hasColumn ('orders ' , 'order_date ' ));
5620+ $ this ->assertFalse ($ adapter ->hasColumn ('orders ' , 'customer_id ' ));
5621+ $ this ->assertFalse ($ adapter ->hasTable ('customers ' ));
5622+ $ this ->assertFalse ($ adapter ->hasForeignKey ('orders ' , ['customer_id ' ]));
5623+
5624+ $ this ->manager ->rollback ('production ' );
5625+ $ this ->assertFalse ($ adapter ->hasTable ('orders ' ));
5626+ $ this ->assertFalse ($ adapter ->hasTable ('customers ' ));
5627+ }
5628+
55865629 public function testReversibleMigrationsWorkAsExpectedWithNamespace ()
55875630 {
55885631 if (!TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED ) {
@@ -5943,6 +5986,64 @@ public function testPostgresFullMigration()
59435986 $ this ->assertFalse ($ adapter ->hasTable ('users ' ));
59445987 }
59455988
5989+ public function testMigrationWithDropColumnAndForeignKeyAndIndex ()
5990+ {
5991+ if (!TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED ) {
5992+ $ this ->markTestSkipped ('Mysql tests disabled. See TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED constant. ' );
5993+ }
5994+ $ configArray = $ this ->getConfigArray ();
5995+ $ adapter = $ this ->manager ->getEnvironment ('production ' )->getAdapter ();
5996+
5997+ // override the migrations directory to use the reversible migrations
5998+ $ configArray ['paths ' ]['migrations ' ] = $ this ->getCorrectedPath (__DIR__ . '/_files/drop_column_fk_index_regression ' );
5999+ $ config = new Config ($ configArray );
6000+
6001+ // ensure the database is empty
6002+ $ adapter ->dropDatabase (TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE );
6003+ $ adapter ->createDatabase (TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE );
6004+ $ adapter ->disconnect ();
6005+
6006+ $ this ->manager ->setConfig ($ config );
6007+ $ this ->manager ->migrate ('production ' , '20190928205056 ' );
6008+
6009+ $ this ->assertTrue ($ adapter ->hasTable ('table1 ' ));
6010+ $ this ->assertTrue ($ adapter ->hasTable ('table2 ' ));
6011+ $ this ->assertTrue ($ adapter ->hasTable ('table3 ' ));
6012+ $ this ->assertTrue ($ adapter ->hasColumn ('table1 ' , 'table2_id ' ));
6013+ $ this ->assertTrue ($ adapter ->hasForeignKey ('table1 ' , ['table2_id ' ], 'table1_table2_id ' ));
6014+ $ this ->assertTrue ($ adapter ->hasIndexByName ('table1 ' , 'table1_table2_id ' ));
6015+ $ this ->assertTrue ($ adapter ->hasColumn ('table1 ' , 'table3_id ' ));
6016+ $ this ->assertTrue ($ adapter ->hasForeignKey ('table1 ' , ['table3_id ' ], 'table1_table3_id ' ));
6017+ $ this ->assertTrue ($ adapter ->hasIndexByName ('table1 ' , 'table1_table3_id ' ));
6018+
6019+ // Run the next migration
6020+ $ this ->manager ->migrate ('production ' );
6021+ $ this ->assertTrue ($ adapter ->hasTable ('table1 ' ));
6022+ $ this ->assertTrue ($ adapter ->hasTable ('table2 ' ));
6023+ $ this ->assertTrue ($ adapter ->hasTable ('table3 ' ));
6024+ $ this ->assertTrue ($ adapter ->hasColumn ('table1 ' , 'table2_id ' ));
6025+ $ this ->assertTrue ($ adapter ->hasForeignKey ('table1 ' , ['table2_id ' ], 'table1_table2_id ' ));
6026+ $ this ->assertTrue ($ adapter ->hasIndexByName ('table1 ' , 'table1_table2_id ' ));
6027+ $ this ->assertFalse ($ adapter ->hasColumn ('table1 ' , 'table3_id ' ));
6028+ $ this ->assertFalse ($ adapter ->hasForeignKey ('table1 ' , ['table3_id ' ], 'table1_table3_id ' ));
6029+ $ this ->assertFalse ($ adapter ->hasIndexByName ('table1 ' , 'table1_table3_id ' ));
6030+
6031+ // rollback
6032+ $ this ->manager ->rollback ('production ' );
6033+ $ this ->manager ->rollback ('production ' );
6034+
6035+ // ensure reversed migrations worked
6036+ $ this ->assertTrue ($ adapter ->hasTable ('table1 ' ));
6037+ $ this ->assertTrue ($ adapter ->hasTable ('table2 ' ));
6038+ $ this ->assertTrue ($ adapter ->hasTable ('table3 ' ));
6039+ $ this ->assertFalse ($ adapter ->hasColumn ('table1 ' , 'table2_id ' ));
6040+ $ this ->assertFalse ($ adapter ->hasForeignKey ('table1 ' , ['table2_id ' ], 'table1_table2_id ' ));
6041+ $ this ->assertFalse ($ adapter ->hasIndexByName ('table1 ' , 'table1_table2_id ' ));
6042+ $ this ->assertFalse ($ adapter ->hasColumn ('table1 ' , 'table3_id ' ));
6043+ $ this ->assertFalse ($ adapter ->hasForeignKey ('table1 ' , ['table3_id ' ], 'table1_table3_id ' ));
6044+ $ this ->assertFalse ($ adapter ->hasIndexByName ('table1 ' , 'table1_table3_id ' ));
6045+ }
6046+
59466047 public function setExpectedException ($ exceptionName , $ exceptionMessage = '' , $ exceptionCode = null )
59476048 {
59486049 if (method_exists ($ this , 'expectException ' )) {
0 commit comments