Skip to content

Commit

Permalink
Fixes yiisoft#3564: Fixed the bug that primary key columns should not…
Browse files Browse the repository at this point in the history
… take default values from schema
  • Loading branch information
qiangxue committed May 23, 2014
1 parent 6e7713e commit f5dbd9a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions framework/db/cubrid/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ protected function loadColumnSchema($info)

$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' ||
Expand Down
2 changes: 1 addition & 1 deletion framework/db/mssql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ protected function loadColumnSchema($info)
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']);
}

Expand Down
2 changes: 1 addition & 1 deletion framework/db/mysql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected function loadColumnSchema($info)

$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']);
}

Expand Down
10 changes: 6 additions & 4 deletions framework/db/oci/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,12 @@ protected function createColumn($column)
$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;
Expand Down
6 changes: 2 additions & 4 deletions framework/db/pgsql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,12 @@ protected function findColumns($table)
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];
}
Expand Down
12 changes: 7 additions & 5 deletions framework/db/sqlite/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,13 @@ protected function loadColumnSchema($info)
}
$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;
Expand Down

0 comments on commit f5dbd9a

Please sign in to comment.