Fixes #17325: Fixed "Cannot drop view" for MySQL while migrate/fresh

This commit is contained in:
Alexander Kartavenko
2019-06-30 19:53:50 +03:00
committed by Alexander Makarov
parent f2212a3183
commit 757bc42645
4 changed files with 57 additions and 4 deletions

View File

@ -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)

View File

@ -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}
*/

View File

@ -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;
}
}
}

View File

@ -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
*/