mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Fixes #14664: Add migrate/fresh command to truncate database and apply migrations again
This commit is contained in:

committed by
Alexander Makarov

parent
7a38849310
commit
58792cdac5
@ -271,6 +271,35 @@ class MigrateController extends BaseMigrateController
|
||||
])->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* @since 2.0.13
|
||||
*/
|
||||
protected function refreshDatabase()
|
||||
{
|
||||
$db = $this->db;
|
||||
$schemas = $db->schema->getTableSchemas();
|
||||
|
||||
// First drop all foreign keys,
|
||||
foreach ($schemas as $schema) {
|
||||
if ($schema->foreignKeys) {
|
||||
foreach ($schema->foreignKeys as $name => $foreignKey) {
|
||||
$db->createCommand()->dropForeignKey($name, $schema->name)->execute();
|
||||
$this->stdout("Foreign key $name dropped.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Then drop the tables:
|
||||
foreach ($schemas as $schema) {
|
||||
$db->createCommand()->dropTable($schema->name)->execute();
|
||||
$this->stdout("Table {$schema->name} dropped.\n");
|
||||
}
|
||||
|
||||
// The database should be cleaned up. Start the migrations!
|
||||
$this->actionUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
|
Reference in New Issue
Block a user