Browse Source

Fixes #16394: Fixed issues in `migrate/create` when specifying default values with colons and adding multiple columns

tags/2.0.22
Alexander Kartavenko 5 years ago committed by Alexander Makarov
parent
commit
0fcd495583
  1. 1
      framework/CHANGELOG.md
  2. 30
      framework/console/controllers/MigrateController.php
  3. 2
      framework/views/addColumnMigration.php
  4. 37
      tests/data/console/migrate_create/add_two_columns_test.php
  5. 39
      tests/data/console/migrate_create/create_field_with_colon_default_values.php
  6. 12
      tests/framework/console/controllers/MigrateControllerTest.php

1
framework/CHANGELOG.md

@ -5,6 +5,7 @@ Yii Framework 2 Change Log
------------------------
- Enh #17382: Added `\yii\validators\DateValidator::$strictDateFormat` to enable strict validation (alexkart)
- Bug #16394: Fixed issues in `migrate/create` when specifying default values with colons and adding multiple columns (alexkart)
2.0.21 June 18, 2019

30
framework/console/controllers/MigrateController.php

@ -488,7 +488,7 @@ class MigrateController extends BaseMigrateController
$foreignKeys = [];
foreach ($this->fields as $index => $field) {
$chunks = preg_split('/\s?:\s?/', $field, null);
$chunks = $this->splitFieldIntoChunks($field);
$property = array_shift($chunks);
foreach ($chunks as $i => &$chunk) {
@ -524,6 +524,34 @@ class MigrateController extends BaseMigrateController
}
/**
* Splits field into chunks
*
* @param string $field
* @return array|array[]|false|string[]
*/
protected function splitFieldIntoChunks($field)
{
$hasDoubleQuotes = false;
preg_match_all('/defaultValue\(.*?:.*?\)/', $field, $matches);
if (isset($matches[0][0])) {
$hasDoubleQuotes = true;
$originalDefaultValue = $matches[0][0];
$defaultValue = str_replace(':', '{{colon}}', $originalDefaultValue);
$field = str_replace($originalDefaultValue, $defaultValue, $field);
}
$chunks = preg_split('/\s?:\s?/', $field);
if (is_array($chunks) && $hasDoubleQuotes) {
foreach ($chunks as $key => $chunk) {
$chunks[$key] = str_replace($defaultValue, $originalDefaultValue, $chunk);
}
}
return $chunks;
}
/**
* Adds default primary key to fields list if there's no primary key specified.
* @param array $fields parsed fields
* @since 2.0.7

2
framework/views/addColumnMigration.php

@ -10,7 +10,7 @@
/* @var $fields array the fields */
preg_match('/^add_(.+)_columns?_to_(.+)_table$/', $name, $matches);
$columns = $matches[1];
$columns = str_replace('_column_', ', ', $matches[1]);
echo "<?php\n";
if (!empty($namespace)) {

37
tests/data/console/migrate_create/add_two_columns_test.php

@ -0,0 +1,37 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
return <<<CODE
<?php
use yii\db\Migration;
/**
* Handles adding field_1, field_2 to table `{{%{table}}}`.
*/
class {$class} extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
\$this->addColumn('{{%{table}}}', 'field_1', \$this->string(10)->notNull());
\$this->addColumn('{{%{table}}}', 'field_2', \$this->text()->notNull());
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
\$this->dropColumn('{{%{table}}}', 'field_1');
\$this->dropColumn('{{%{table}}}', 'field_2');
}
}
CODE;

39
tests/data/console/migrate_create/create_field_with_colon_default_values.php

@ -0,0 +1,39 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
return <<<CODE
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%test}}`.
*/
class {$class} extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
\$this->createTable('{{%test}}', [
'id' => \$this->primaryKey(),
'field_1' => \$this->dateTime()->notNull()->defaultValue('0000-00-00 00:00:00'),
'field_2' => \$this->string()->defaultValue('default:value'),
]);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
\$this->dropTable('{{%test}}');
}
}
CODE;

12
tests/framework/console/controllers/MigrateControllerTest.php

@ -142,6 +142,11 @@ class MigrateControllerTest extends TestCase
body:text:notNull:defaultValue(",test"),
test:custom(11,2,"s"):notNull',
]);
$this->assertCommandCreatedFile('create_field_with_colon_default_values', 'create_test_table', 'test', [
'fields' => 'field_1:dateTime:notNull:defaultValue(\'0000-00-00 00:00:00\'),
field_2:string:defaultValue(\'default:value\')',
]);
}
public function testUpdatingLongNamedMigration()
@ -171,7 +176,7 @@ class MigrateControllerTest extends TestCase
public function testCreateLongNamedMigration()
{
$this->setOutputCallback(function($output) {
$this->setOutputCallback(function ($output) {
return null;
});
@ -237,6 +242,11 @@ class MigrateControllerTest extends TestCase
order_id:integer:foreignKey(user_order):notNull,
created_at:dateTime:notNull',
]);
$this->assertCommandCreatedFile('add_two_columns_test', 'add_field_1_column_field_2_column_to_' . $table . '_table', $table, [
'fields' => 'field_1:string(10):notNull,
field_2:text:notNull',
]);
}
}

Loading…
Cancel
Save