mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-13 22:06:51 +08:00
Fixed incorrect order of migrations history in case yii\console\controllers\MigrateController::$migrationNamespaces
is in use
This commit is contained in:
@ -36,7 +36,7 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #12881: Added `removeValue` method to `yii\helpers\BaseArrayHelper` (nilsburg)
|
- Enh #12881: Added `removeValue` method to `yii\helpers\BaseArrayHelper` (nilsburg)
|
||||||
- Enh #12901: Added `getDefaultHelpHeader` method to the `yii\console\controllers\HelpController` class to be able to override default help header in a class heir (diezztsk)
|
- Enh #12901: Added `getDefaultHelpHeader` method to the `yii\console\controllers\HelpController` class to be able to override default help header in a class heir (diezztsk)
|
||||||
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
|
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)
|
||||||
- Bug #12974: Changed order of migrations history in `yii\console\controllers\MigrateController::getMigrationHistory()` (evgen-d)
|
- Bug #12974: Fixed incorrect order of migrations history in case `yii\console\controllers\MigrateController::$migrationNamespaces` is in use (evgen-d, klimov-paul)
|
||||||
|
|
||||||
|
|
||||||
2.0.10 October 20, 2016
|
2.0.10 October 20, 2016
|
||||||
|
@ -199,27 +199,48 @@ class MigrateController extends BaseMigrateController
|
|||||||
if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) {
|
if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) {
|
||||||
$this->createMigrationHistoryTable();
|
$this->createMigrationHistoryTable();
|
||||||
}
|
}
|
||||||
$query = new Query;
|
$query = (new Query())
|
||||||
$rows = $query->select(['version', 'apply_time'])
|
->select(['version', 'apply_time'])
|
||||||
->from($this->migrationTable)
|
->from($this->migrationTable)
|
||||||
->orderBy('apply_time DESC, version DESC')
|
->orderBy(['apply_time' => SORT_DESC, 'version' => SORT_DESC]);
|
||||||
->limit($limit)
|
|
||||||
->createCommand($this->db)
|
if (empty($this->migrationNamespaces)) {
|
||||||
->queryAll();
|
$query->limit($limit);
|
||||||
|
$rows = $query->all($this->db);
|
||||||
|
$history = ArrayHelper::map($rows, 'version', 'apply_time');
|
||||||
|
unset($history[self::BASE_MIGRATION]);
|
||||||
|
return $history;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows = $query->all($this->db);
|
||||||
|
|
||||||
$history = [];
|
$history = [];
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $key => $row) {
|
||||||
if ($row['version'] === self::BASE_MIGRATION) {
|
if ($row['version'] === self::BASE_MIGRATION) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (preg_match('/m?(\d{6}_?\d{6})(\D.*)?$/is', $row['version'], $matches)) {
|
if (preg_match('/m?(\d{6}_?\d{6})(\D.*)?$/is', $row['version'], $matches)) {
|
||||||
$time = str_replace('_', '', $matches[1]);
|
$time = str_replace('_', '', $matches[1]);
|
||||||
$history['m' . $time . $matches[2]] = $row;
|
$row['canonicalVersion'] = $time;
|
||||||
|
} else {
|
||||||
|
$row['canonicalVersion'] = $row['version'];
|
||||||
}
|
}
|
||||||
|
$row['apply_time'] = (int)$row['apply_time'];
|
||||||
|
$history[] = $row;
|
||||||
}
|
}
|
||||||
// sort history in desc order
|
|
||||||
uksort($history, function ($a, $b) {
|
usort($history, function ($a, $b) {
|
||||||
return strcasecmp($b, $a);
|
if ($a['apply_time'] === $b['apply_time']) {
|
||||||
|
if (($compareResult = strcasecmp($b['canonicalVersion'], $a['canonicalVersion'])) !== 0) {
|
||||||
|
return $compareResult;
|
||||||
|
}
|
||||||
|
return strcasecmp($b['version'], $a['version']);
|
||||||
|
}
|
||||||
|
return ($a['apply_time'] > $b['apply_time']) ? -1 : +1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$history = array_slice($history, 0, $limit);
|
||||||
|
|
||||||
$history = ArrayHelper::map($history, 'version', 'apply_time');
|
$history = ArrayHelper::map($history, 'version', 'apply_time');
|
||||||
|
|
||||||
return $history;
|
return $history;
|
||||||
|
@ -218,4 +218,60 @@ class MigrateControllerTest extends TestCase
|
|||||||
$this->assertCommandCreatedFile('junction_test', $migrationName, 'post_tag');
|
$this->assertCommandCreatedFile('junction_test', $migrationName, 'post_tag');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://github.com/yiisoft/yii2/issues/12980
|
||||||
|
*/
|
||||||
|
public function testGetMigrationHistory()
|
||||||
|
{
|
||||||
|
$controllerConfig = [
|
||||||
|
'migrationPath' => null,
|
||||||
|
'migrationNamespaces' => [$this->migrationNamespace]
|
||||||
|
];
|
||||||
|
$this->runMigrateControllerAction('history', [], $controllerConfig);
|
||||||
|
|
||||||
|
$controller = $this->createMigrateController($controllerConfig);
|
||||||
|
$controller->db = Yii::$app->db;
|
||||||
|
|
||||||
|
Yii::$app->db->createCommand()
|
||||||
|
->batchInsert(
|
||||||
|
'migration',
|
||||||
|
['version', 'apply_time'],
|
||||||
|
[
|
||||||
|
['app\migrations\M140506102106One', 10],
|
||||||
|
['app\migrations\M160909083544Two', 10],
|
||||||
|
['app\modules\foo\migrations\M161018124749Three', 10],
|
||||||
|
['app\migrations\M160930135248Four', 20],
|
||||||
|
['app\modules\foo\migrations\M161025123028Five', 20],
|
||||||
|
['app\migrations\M161110133341Six', 20],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
->execute();
|
||||||
|
|
||||||
|
$rows = $this->invokeMethod($controller, 'getMigrationHistory', [10]);
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
[
|
||||||
|
'app\migrations\M161110133341Six',
|
||||||
|
'app\modules\foo\migrations\M161025123028Five',
|
||||||
|
'app\migrations\M160930135248Four',
|
||||||
|
'app\modules\foo\migrations\M161018124749Three',
|
||||||
|
'app\migrations\M160909083544Two',
|
||||||
|
'app\migrations\M140506102106One',
|
||||||
|
],
|
||||||
|
array_keys($rows)
|
||||||
|
);
|
||||||
|
|
||||||
|
$rows = $this->invokeMethod($controller, 'getMigrationHistory', [4]);
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
[
|
||||||
|
'app\migrations\M161110133341Six',
|
||||||
|
'app\modules\foo\migrations\M161025123028Five',
|
||||||
|
'app\migrations\M160930135248Four',
|
||||||
|
'app\modules\foo\migrations\M161018124749Three',
|
||||||
|
],
|
||||||
|
array_keys($rows)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user