From f5dbd9a084c857ce64ed9df32f4f35b4e5896d04 Mon Sep 17 00:00:00 2001
From: Qiang Xue <qiang.xue@gmail.com>
Date: Fri, 23 May 2014 10:35:20 -0400
Subject: [PATCH] Fixes #3564: Fixed the bug that primary key columns should not take default values from schema

---
 framework/CHANGELOG.md         |  1 +
 framework/db/cubrid/Schema.php |  4 ++++
 framework/db/mssql/Schema.php  |  2 +-
 framework/db/mysql/Schema.php  |  2 +-
 framework/db/oci/Schema.php    | 10 ++++++----
 framework/db/pgsql/Schema.php  |  6 ++----
 framework/db/sqlite/Schema.php | 12 +++++++-----
 7 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md
index aa43ec8..c795b97 100644
--- a/framework/CHANGELOG.md
+++ b/framework/CHANGELOG.md
@@ -30,6 +30,7 @@ Yii Framework 2 Change Log
 - Bug #3458: Fixed the bug that the image rendered by `CaptchaAction` was using a wrong content type (MDMunir, qiangxue)
 - Bug #3522: Fixed BaseFileHelper::normalizePath to allow a (.) for the current path. (skotos)
 - Bug #3548: Fixed the bug that X-Rate-Limit-Remaining header is always zero when using RateLimiter (qiangxue)
+- Bug #3564: Fixed the bug that primary key columns should not take default values from schema (qiangxue)
 - Bug #3567: Fixed the bug that smallint was treated as string for PostgreSQL (qiangxue)
 - Bug: Fixed inconsistent return of `\yii\console\Application::runAction()` (samdark)
 - Enh #2264: `CookieCollection::has()` will return false for expired or removed cookies (qiangxue)
diff --git a/framework/db/cubrid/Schema.php b/framework/db/cubrid/Schema.php
index 1d517fc..0daf792 100644
--- a/framework/db/cubrid/Schema.php
+++ b/framework/db/cubrid/Schema.php
@@ -227,6 +227,10 @@ class Schema extends \yii\db\Schema
 
         $column->phpType = $this->getColumnPhpType($column);
 
+        if ($column->isPrimaryKey) {
+            return $column;
+        }
+
         if ($column->type === 'timestamp' && $info['Default'] === 'CURRENT_TIMESTAMP' ||
             $column->type === 'datetime' && $info['Default'] === 'SYS_DATETIME' ||
             $column->type === 'date' && $info['Default'] === 'SYS_DATE' ||
diff --git a/framework/db/mssql/Schema.php b/framework/db/mssql/Schema.php
index 419b260..7f26181 100644
--- a/framework/db/mssql/Schema.php
+++ b/framework/db/mssql/Schema.php
@@ -221,7 +221,7 @@ class Schema extends \yii\db\Schema
         if ($info['column_default'] == '(NULL)') {
             $info['column_default'] = null;
         }
-        if ($column->type !== 'timestamp' || $info['column_default'] !== 'CURRENT_TIMESTAMP') {
+        if (!$column->isPrimaryKey && ($column->type !== 'timestamp' || $info['column_default'] !== 'CURRENT_TIMESTAMP')) {
             $column->defaultValue = $column->typecast($info['column_default']);
         }
 
diff --git a/framework/db/mysql/Schema.php b/framework/db/mysql/Schema.php
index d01d513..63c0892 100644
--- a/framework/db/mysql/Schema.php
+++ b/framework/db/mysql/Schema.php
@@ -168,7 +168,7 @@ class Schema extends \yii\db\Schema
 
         $column->phpType = $this->getColumnPhpType($column);
 
-        if ($column->type !== 'timestamp' || $info['Default'] !== 'CURRENT_TIMESTAMP') {
+        if (!$column->isPrimaryKey && ($column->type !== 'timestamp' || $info['Default'] !== 'CURRENT_TIMESTAMP')) {
             $column->defaultValue = $column->typecast($info['Default']);
         }
 
diff --git a/framework/db/oci/Schema.php b/framework/db/oci/Schema.php
index e6fed70..fafe2b8 100644
--- a/framework/db/oci/Schema.php
+++ b/framework/db/oci/Schema.php
@@ -170,10 +170,12 @@ EOD;
         $this->extractColumnType($c, $column['DATA_TYPE']);
         $this->extractColumnSize($c, $column['DATA_TYPE']);
 
-        if (stripos($column['DATA_DEFAULT'], 'timestamp') !== false) {
-            $c->defaultValue = null;
-        } else {
-            $c->defaultValue = $c->typecast($column['DATA_DEFAULT']);
+        if (!$column->isPrimaryKey) {
+            if (stripos($column['DATA_DEFAULT'], 'timestamp') !== false) {
+                $c->defaultValue = null;
+            } else {
+                $c->defaultValue = $c->typecast($column['DATA_DEFAULT']);
+            }
         }
 
         return $c;
diff --git a/framework/db/pgsql/Schema.php b/framework/db/pgsql/Schema.php
index d264f1c..ca17526 100644
--- a/framework/db/pgsql/Schema.php
+++ b/framework/db/pgsql/Schema.php
@@ -367,14 +367,12 @@ SQL;
         foreach ($columns as $column) {
             $column = $this->loadColumnSchema($column);
             $table->columns[$column->name] = $column;
-            if ($column->isPrimaryKey === true) {
+            if ($column->isPrimaryKey) {
                 $table->primaryKey[] = $column->name;
                 if ($table->sequenceName === null && preg_match("/nextval\\('\"?\\w+\"?\.?\"?\\w+\"?'(::regclass)?\\)/", $column->defaultValue) === 1) {
                     $table->sequenceName = preg_replace(['/nextval/', '/::/', '/regclass/', '/\'\)/', '/\(\'/'], '', $column->defaultValue);
                 }
-            }
-
-            if ($column->defaultValue) {
+            } elseif ($column->defaultValue) {
                 if (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches) || preg_match("/^(.*?)::/", $column->defaultValue, $matches)) {
                     $column->defaultValue = $matches[1];
                 }
diff --git a/framework/db/sqlite/Schema.php b/framework/db/sqlite/Schema.php
index 5a29f2f..bcc83b5 100644
--- a/framework/db/sqlite/Schema.php
+++ b/framework/db/sqlite/Schema.php
@@ -238,11 +238,13 @@ class Schema extends \yii\db\Schema
         }
         $column->phpType = $this->getColumnPhpType($column);
 
-        $value = trim($info['dflt_value'], "'\"");
-        if ($column->type === 'string') {
-            $column->defaultValue = $value;
-        } else {
-            $column->defaultValue = $column->typecast(strcasecmp($value, 'null') ? $value : null);
+        if (!$column->isPrimaryKey) {
+            $value = trim($info['dflt_value'], "'\"");
+            if ($column->type === 'string') {
+                $column->defaultValue = $value;
+            } else {
+                $column->defaultValue = $column->typecast(strcasecmp($value, 'null') ? $value : null);
+            }
         }
 
         return $column;
--
libgit2 0.27.1