diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index ce683867b7..a56d4e04d5 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -8,6 +8,7 @@ Yii Framework 2 Change Log - Bug #16394: Fixed issues in `migrate/create` when specifying default values with colons and adding multiple columns (alexkart) - Bug #17341: Re-added fix for error from yii.activeForm.js in strict mode (mikehaertl) - Enh #17396: Added 'invoked by controller' to the debug log message when `\yii\base\Action` is used (alexkart) +- Bug #17325: Fixed "Cannot drop view" for MySQL while `migrate/fresh` (alexkart) - Bug #17384: Fixed SQL error when passing `DISTINCT ON` queries (brandonkelly) diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index c5522d0eab..f2322a1680 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -314,7 +314,7 @@ class MigrateController extends BaseMigrateController $db->createCommand()->dropTable($schema->name)->execute(); $this->stdout("Table {$schema->name} dropped.\n"); } catch (\Exception $e) { - if (strpos($e->getMessage(), 'DROP VIEW to delete view') !== false) { + if ($this->isViewRelated($e->getMessage())) { $db->createCommand()->dropView($schema->name)->execute(); $this->stdout("View {$schema->name} dropped.\n"); } else { @@ -324,6 +324,27 @@ class MigrateController extends BaseMigrateController } } + /** + * Determines whether the error message is related to deleting a view or not + * @param $errorMessage + * @return bool + */ + private function isViewRelated($errorMessage) + { + $dropViewErrors = [ + 'DROP VIEW to delete view', // SQLite + 'SQLSTATE[42S02]', // MySQL + ]; + + foreach ($dropViewErrors as $dropViewError) { + if (strpos($errorMessage, $dropViewError) !== false) { + return true; + } + } + + return false; + } + /** * {@inheritdoc} */ diff --git a/tests/TestCase.php b/tests/TestCase.php index 4dbe1504ae..c03501fe90 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -29,8 +29,8 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase /** * Returns a test configuration param from /data/config.php. - * @param string $name params name - * @param mixed $default default value to use when param is not set. + * @param string $name params name + * @param mixed $default default value to use when param is not set. * @return mixed the value of the configuration param */ public static function getParam($name, $default = null) @@ -216,4 +216,20 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase { self::assertThat($actual, new IsOneOfAssert($expected), $message); } + + /** + * Changes db component config + * @param $db + */ + protected function switchDbConnection($db) + { + $databases = $this->getParam('databases'); + if (isset($databases[$db])) { + $database = $databases[$db]; + Yii::$app->db->close(); + Yii::$app->db->dsn = isset($database['dsn']) ? $database['dsn'] : null; + Yii::$app->db->username = isset($database['username']) ? $database['username'] : null; + Yii::$app->db->password = isset($database['password']) ? $database['password'] : null; + } + } } diff --git a/tests/framework/console/controllers/MigrateControllerTest.php b/tests/framework/console/controllers/MigrateControllerTest.php index 080c40b2e0..5651dc0b5f 100644 --- a/tests/framework/console/controllers/MigrateControllerTest.php +++ b/tests/framework/console/controllers/MigrateControllerTest.php @@ -286,9 +286,16 @@ class MigrateControllerTest extends TestCase /** * Test the migrate:fresh command. + * @dataProvider refreshMigrationDataProvider + * @param $db + * @throws \yii\db\Exception */ - public function testRefreshMigration() + public function testRefreshMigration($db) { + if ($db !== 'default') { + $this->switchDbConnection($db); + } + Yii::$app->db->createCommand('create table hall_of_fame(id int, string varchar(255))') ->execute(); @@ -310,6 +317,14 @@ class MigrateControllerTest extends TestCase $this->assertContains('No new migrations found. Your system is up-to-date.', $result); } + public function refreshMigrationDataProvider() + { + return [ + ['default'], + ['mysql'], + ]; + } + /** * @see https://github.com/yiisoft/yii2/issues/12980 */