added unit tests for schema detection

fixed some issues with schema detection
This commit is contained in:
Carsten Brandt
2014-06-25 03:09:28 +02:00
parent 1cef60db17
commit 4f95fcd91f
17 changed files with 521 additions and 195 deletions

View File

@@ -99,6 +99,8 @@ class ColumnSchema extends Object
return (integer) $value;
case 'boolean':
return (boolean) $value;
case 'double':
return (double) $value;
}
return $value;

View File

@@ -200,18 +200,19 @@ class Schema extends \yii\db\Schema
$column->isPrimaryKey = false; // primary key will be set by loadTableSchema() later
$column->autoIncrement = stripos($info['Extra'], 'auto_increment') !== false;
$column->dbType = strtolower($info['Type']);
$column->dbType = $info['Type'];
$column->unsigned = strpos($column->dbType, 'unsigned') !== false;
$column->type = self::TYPE_STRING;
if (preg_match('/^([\w ]+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) {
$type = $matches[1];
if (preg_match('/^([\w ]+)(?:\(([^\)]+)\))?$/', $column->dbType, $matches)) {
$type = strtolower($matches[1]);
$column->dbType = $type . (isset($matches[2]) ? "({$matches[2]})" : '');
if (isset($this->typeMap[$type])) {
$column->type = $this->typeMap[$type];
}
if (!empty($matches[2])) {
if ($type === 'enum') {
$values = explode(',', $matches[2]);
$values = preg_split('/\s*,\s*/', $matches[2]);
foreach ($values as $i => $value) {
$values[$i] = trim($value, "'");
}
@@ -232,7 +233,7 @@ class Schema extends \yii\db\Schema
return $column;
}
if ($column->type === 'timestamp' && $info['Default'] === 'CURRENT_TIMESTAMP' ||
if ($column->type === 'timestamp' && $info['Default'] === 'SYS_TIMESTAMP' ||
$column->type === 'datetime' && $info['Default'] === 'SYS_DATETIME' ||
$column->type === 'date' && $info['Default'] === 'SYS_DATE' ||
$column->type === 'time' && $info['Default'] === 'SYS_TIME'

View File

@@ -7,6 +7,7 @@
namespace yii\db\mysql;
use yii\db\Expression;
use yii\db\TableSchema;
use yii\db\ColumnSchema;
@@ -132,11 +133,11 @@ class Schema extends \yii\db\Schema
$column->comment = $info['Comment'];
$column->dbType = $info['Type'];
$column->unsigned = strpos($column->dbType, 'unsigned') !== false;
$column->unsigned = stripos($column->dbType, 'unsigned') !== false;
$column->type = self::TYPE_STRING;
if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) {
$type = $matches[1];
$type = strtolower($matches[1]);
if (isset($this->typeMap[$type])) {
$column->type = $this->typeMap[$type];
}
@@ -168,8 +169,12 @@ class Schema extends \yii\db\Schema
$column->phpType = $this->getColumnPhpType($column);
if (!$column->isPrimaryKey && ($column->type !== 'timestamp' || $info['Default'] !== 'CURRENT_TIMESTAMP')) {
$column->defaultValue = $column->typecast($info['Default']);
if (!$column->isPrimaryKey) {
if ($column->type === 'timestamp' && $info['Default'] === 'CURRENT_TIMESTAMP') {
$column->defaultValue = new Expression('CURRENT_TIMESTAMP');
} else {
$column->defaultValue = $column->typecast($info['Default']);
}
}
return $column;

View File

@@ -8,6 +8,7 @@
namespace yii\db\sqlite;
use yii\base\NotSupportedException;
use yii\db\Expression;
use yii\db\TableSchema;
use yii\db\ColumnSchema;
use yii\db\Transaction;
@@ -211,7 +212,7 @@ class Schema extends \yii\db\Schema
$column->allowNull = !$info['notnull'];
$column->isPrimaryKey = $info['pk'] != 0;
$column->dbType = $info['type'];
$column->dbType = strtolower($info['type']);
$column->unsigned = strpos($column->dbType, 'unsigned') !== false;
$column->type = self::TYPE_STRING;
@@ -241,11 +242,13 @@ class Schema extends \yii\db\Schema
$column->phpType = $this->getColumnPhpType($column);
if (!$column->isPrimaryKey) {
$value = trim($info['dflt_value'], "'\"");
if ($column->type === 'string') {
$column->defaultValue = $value;
if ($info['dflt_value'] === 'null' || $info['dflt_value'] === '' || $info['dflt_value'] === null) {
$column->defaultValue = null;
} elseif ($column->type === 'timestamp' && $info['dflt_value'] === 'CURRENT_TIMESTAMP') {
$column->defaultValue = new Expression('CURRENT_TIMESTAMP');
} else {
$column->defaultValue = $column->typecast(strcasecmp($value, 'null') ? $value : null);
$value = trim($info['dflt_value'], "'\"");
$column->defaultValue = $column->typecast($value);
}
}

View File

@@ -1,4 +1,5 @@
<?php
/* @var $this \yii\web\View */
/* @var $exception \Exception */
/* @var $handler \yii\web\ErrorHandler */
?>