mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 21:41:19 +08:00
Fix #17437: Fixed generating namespaced migrations
This commit is contained in:
committed by
Alexander Makarov
parent
a4a22ae1fb
commit
e50a07c30c
@ -7,6 +7,7 @@ Yii Framework 2 Change Log
|
||||
- Bug #17219: Fixed quoting of table names with spaces in MSSQL (alexkart)
|
||||
- Bug #10020: Fixed quoting of column names with dots in MSSQL (alexkart)
|
||||
- Bug #17424: Subdomain support for `User::loginRequired` (alex-code)
|
||||
- Bug #17437: Fixed generating namespaced migrations (bizley)
|
||||
|
||||
|
||||
2.0.23 July 16, 2019
|
||||
|
||||
@ -17,6 +17,7 @@ use yii\console\ExitCode;
|
||||
use yii\db\MigrationInterface;
|
||||
use yii\helpers\Console;
|
||||
use yii\helpers\FileHelper;
|
||||
use yii\helpers\Inflector;
|
||||
|
||||
/**
|
||||
* BaseMigrateController is the base class for migrate controllers.
|
||||
@ -661,17 +662,15 @@ abstract class BaseMigrateController extends Controller
|
||||
if (strpos($name, '\\') !== false) {
|
||||
$namespace = substr($name, 0, strrpos($name, '\\'));
|
||||
$name = substr($name, strrpos($name, '\\') + 1);
|
||||
} else {
|
||||
if ($this->migrationPath === null) {
|
||||
$migrationNamespaces = $this->migrationNamespaces;
|
||||
$namespace = array_shift($migrationNamespaces);
|
||||
}
|
||||
} elseif ($this->migrationPath === null) {
|
||||
$migrationNamespaces = $this->migrationNamespaces;
|
||||
$namespace = array_shift($migrationNamespaces);
|
||||
}
|
||||
|
||||
if ($namespace === null) {
|
||||
$class = 'm' . gmdate('ymd_His') . '_' . $name;
|
||||
} else {
|
||||
$class = 'M' . gmdate('ymdHis') . ucfirst($name);
|
||||
$class = 'M' . gmdate('ymdHis') . Inflector::camelize($name);
|
||||
}
|
||||
|
||||
return [$namespace, $class];
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -9,9 +9,6 @@
|
||||
/* @var $table string the name table */
|
||||
/* @var $fields array the fields */
|
||||
|
||||
preg_match('/^add_(.+)_columns?_to_(.+)_table$/', $name, $matches);
|
||||
$columns = str_replace('_column_', ', ', $matches[1]);
|
||||
|
||||
echo "<?php\n";
|
||||
if (!empty($namespace)) {
|
||||
echo "\nnamespace {$namespace};\n";
|
||||
@ -21,7 +18,7 @@ if (!empty($namespace)) {
|
||||
use yii\db\Migration;
|
||||
|
||||
/**
|
||||
* Handles adding <?= $columns ?> to table `<?= $table ?>`.
|
||||
* Handles adding columns to table `<?= $table ?>`.
|
||||
<?= $this->render('_foreignTables', [
|
||||
'foreignKeys' => $foreignKeys,
|
||||
]) ?>
|
||||
|
||||
@ -8,8 +8,6 @@
|
||||
/* @var $namespace string the new migration class namespace */
|
||||
/* @var $table string the name table */
|
||||
/* @var $fields array the fields */
|
||||
preg_match('/^drop_(.+)_columns?_from_(.+)_table$/', $name, $matches);
|
||||
$columns = $matches[1];
|
||||
|
||||
echo "<?php\n";
|
||||
if (!empty($namespace)) {
|
||||
@ -20,7 +18,7 @@ if (!empty($namespace)) {
|
||||
use yii\db\Migration;
|
||||
|
||||
/**
|
||||
* Handles dropping <?= $columns ?> from table `<?= $table ?>`.
|
||||
* Handles dropping columns from table `<?= $table ?>`.
|
||||
<?= $this->render('_foreignTables', [
|
||||
'foreignKeys' => $foreignKeys,
|
||||
]) ?>
|
||||
|
||||
Reference in New Issue
Block a user