Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MigrateController::addDefaultPrimaryKey() skip if 'id' already set #18845

Merged
merged 9 commits into from
Sep 3, 2021
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Yii Framework 2 Change Log
- Enh #18762: Added `yii\helpers\Json::$keepObjectType` and `yii\web\JsonResponseFormatter::$keepObjectType` in order to avoid changing zero-indexed objects to array in `yii\helpers\Json::encode()` (zebraf1)
- Enh #18783: Add support for URI namespaced tags in `XmlResponseFormatter` (WinterSilence, samdark)
- Enh #18783: Add `XmlResponseFormatter::$objectTagToLowercase` option to lowercase object tags (WinterSilence, samdark)
- Bug #18845: Fix duplicating `id` in `MigrateController::addDefaultPrimaryKey()` (WinterSilence, samdark)
- Bug #17119: Fix `yii\caching\Cache::multiSet()` to use `yii\caching\Cache::$defaultDuration` when no duration is passed (OscarBarrett)


Expand Down
2 changes: 1 addition & 1 deletion framework/console/controllers/MigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ protected function splitFieldIntoChunks($field)
protected function addDefaultPrimaryKey(&$fields)
{
foreach ($fields as $field) {
if (false !== strripos($field['decorators'], 'primarykey()')) {
if ($field['property'] === 'id' || false !== strripos($field['decorators'], 'primarykey()')) {
return;
}
}
Expand Down
37 changes: 37 additions & 0 deletions tests/data/console/migrate_create/create_id_field_not_as_pk.php
Original file line number Diff line number Diff line change
@@ -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

{$namespace}use yii\db\Migration;

/**
* Handles the creation of table `{{%{table}}}`.
*/
class {$class} extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
\$this->createTable('{{%{table}}}', [
'id' => \$this->integer(11)->notNull(),
]);
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
\$this->dropTable('{{%{table}}}');
}
}

CODE;
4 changes: 4 additions & 0 deletions tests/framework/console/controllers/MigrateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ public function generateMigrationDataProvider()
price:money(11,2):notNull,
parenthesis_in_comment:string(255):notNull:comment(\'Name of set (RU)\')',
],
'create_id_field_not_as_pk' => [
'fields' => 'id:integer(11):notNull',
],
'create_fields_with_col_method_after_default_value' => [
'fields' => 'id:primaryKey,
title:string(10):notNull:unique:defaultValue("test"):after("id"),
Expand Down Expand Up @@ -229,6 +232,7 @@ public function generateMigrationDataProvider()

['create_fields', 'create_test_table', 'test', $params['create_fields']],
['create_fields', 'create_TEST_table', 'TEST', $params['create_fields']],
['create_id_field_not_as_pk', 'create_test_table', 'test', $params['create_id_field_not_as_pk']],
['create_title_pk', 'create_test_table', 'test', $params['create_title_pk']],
['create_title_pk', 'create_TEST_table', 'TEST', $params['create_title_pk']],
['create_unsigned_pk', 'create_test_table', 'test', $params['create_unsigned_pk']],
Expand Down