Fix #17437: Fixed generating namespaced migrations

This commit is contained in:
Bizley
2019-07-23 16:28:43 +02:00
committed by Alexander Makarov
parent a4a22ae1fb
commit e50a07c30c
29 changed files with 1467 additions and 237 deletions

View File

@ -13,6 +13,7 @@ use yii\db\Query;
use yii\di\Instance;
use yii\helpers\ArrayHelper;
use yii\helpers\Console;
use yii\helpers\Inflector;
/**
* Manages application migrations.
@ -375,6 +376,26 @@ class MigrateController extends BaseMigrateController
return static::MAX_NAME_LENGTH;
}
/**
* Normalizes table name for generator.
* When name is preceded with underscore name case is kept - otherwise it's converted from camelcase to underscored.
* Last underscore is always trimmed so if there should be underscore at the end of name use two of them.
* @param string $name
* @return string
*/
private function normalizeTableName($name)
{
if (substr($name, -1) === '_') {
$name = substr($name, 0, -1);
}
if (strpos($name, '_') === 0) {
return substr($name, 1);
}
return Inflector::underscore($name);
}
/**
* {@inheritdoc}
* @since 2.0.8
@ -386,13 +407,20 @@ class MigrateController extends BaseMigrateController
$foreignKeys = $parsedFields['foreignKeys'];
$name = $params['name'];
if ($params['namespace']) {
$name = substr($name, strrpos($name, '\\') + 1);
}
$templateFile = $this->templateFile;
$table = null;
if (preg_match('/^create_junction(?:_table_for_|_for_|_)(.+)_and_(.+)_tables?$/', $name, $matches)) {
if (preg_match(
'/^create_?junction_?(?:table)?_?(?:for)?(.+)_?and(.+)_?tables?$/i',
$name,
$matches
)) {
$templateFile = $this->generatorTemplateFiles['create_junction'];
$firstTable = $matches[1];
$secondTable = $matches[2];
$firstTable = $this->normalizeTableName($matches[1]);
$secondTable = $this->normalizeTableName($matches[2]);
$fields = array_merge(
[
@ -420,20 +448,20 @@ class MigrateController extends BaseMigrateController
$foreignKeys[$firstTable . '_id']['column'] = null;
$foreignKeys[$secondTable . '_id']['column'] = null;
$table = $firstTable . '_' . $secondTable;
} elseif (preg_match('/^add_(.+)_columns?_to_(.+)_table$/', $name, $matches)) {
} elseif (preg_match('/^add(.+)columns?_?to(.+)table$/i', $name, $matches)) {
$templateFile = $this->generatorTemplateFiles['add_column'];
$table = $matches[2];
} elseif (preg_match('/^drop_(.+)_columns?_from_(.+)_table$/', $name, $matches)) {
$table = $this->normalizeTableName($matches[2]);
} elseif (preg_match('/^drop(.+)columns?_?from(.+)table$/i', $name, $matches)) {
$templateFile = $this->generatorTemplateFiles['drop_column'];
$table = $matches[2];
} elseif (preg_match('/^create_(.+)_table$/', $name, $matches)) {
$table = $this->normalizeTableName($matches[2]);
} elseif (preg_match('/^create(.+)table$/i', $name, $matches)) {
$this->addDefaultPrimaryKey($fields);
$templateFile = $this->generatorTemplateFiles['create_table'];
$table = $matches[1];
} elseif (preg_match('/^drop_(.+)_table$/', $name, $matches)) {
$table = $this->normalizeTableName($matches[1]);
} elseif (preg_match('/^drop(.+)table$/i', $name, $matches)) {
$this->addDefaultPrimaryKey($fields);
$templateFile = $this->generatorTemplateFiles['drop_table'];
$table = $matches[1];
$table = $this->normalizeTableName($matches[1]);
}
foreach ($foreignKeys as $column => $foreignKey) {