Fixes #14664: Add migrate/fresh command to truncate database and apply migrations again

This commit is contained in:
Herbert Maschke
2017-08-20 00:00:41 +02:00
committed by Alexander Makarov
parent 7a38849310
commit 58792cdac5
6 changed files with 97 additions and 0 deletions

View File

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