From e55b3e0ba129531095ce8456d4fcc8a1c3adb82d Mon Sep 17 00:00:00 2001 From: Elvira Sheina Date: Sat, 30 Jun 2018 04:19:40 +0500 Subject: [PATCH] Fixes #16278: Fixed drop existing views when console `migrate/fresh` command runs --- framework/CHANGELOG.md | 1 + framework/console/controllers/MigrateController.php | 13 +++++++++++-- .../console/controllers/MigrateControllerTest.php | 4 ++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 70cb03daaa..35135fa3ce 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -26,6 +26,7 @@ Yii Framework 2 Change Log - Bug #14636: Views can now use relative paths even when using themed views (sammousa) - Bug #16245: Fixed `__isset()` in `BaseActiveRecord` not catching errors (sammousa) - Bug #16266: Fixed `yii\helpers\BaseStringHelper` where explode would not allow 0 as trim string (Thoulah) +- Bug #16278: Fixed drop existing views when console `migrate/fresh` command runs (developeruz) - Bug #16277: Fixed `yii\db\Query::from()` to respect `yii\db\ExpressionInterface` (noname007) - Bug #16280: Fixed `yii\base\Model::getActiveValidators()` to return correct validators for attribute on scenario (paweljankowiak06) - Enh #16191: Enhanced `yii\helpers\Inflector` to work correctly with UTF-8 (silverfire) diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index 5211834f0f..6909c75774 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -310,8 +310,17 @@ class MigrateController extends BaseMigrateController // Then drop the tables: foreach ($schemas as $schema) { - $db->createCommand()->dropTable($schema->name)->execute(); - $this->stdout("Table {$schema->name} dropped.\n"); + try { + $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) { + $db->createCommand()->dropView($schema->name)->execute(); + $this->stdout("View {$schema->name} dropped.\n"); + } else { + $this->stdout("Cannot drop {$schema->name} Table .\n"); + } + } } } diff --git a/tests/framework/console/controllers/MigrateControllerTest.php b/tests/framework/console/controllers/MigrateControllerTest.php index b214b6a532..7f09058447 100644 --- a/tests/framework/console/controllers/MigrateControllerTest.php +++ b/tests/framework/console/controllers/MigrateControllerTest.php @@ -287,10 +287,14 @@ class MigrateControllerTest extends TestCase Yii::$app->db->createCommand("insert into hall_of_fame values(2, 'Alexander Makarov');") ->execute(); + Yii::$app->db->createCommand('create view view_hall_of_fame as select * from hall_of_fame') + ->execute(); + $result = $this->runMigrateControllerAction('fresh'); // Drop worked $this->assertContains('Table hall_of_fame dropped.', $result); + $this->assertContains('View view_hall_of_fame dropped.', $result); // Migration was restarted $this->assertContains('No new migrations found. Your system is up-to-date.', $result);