Commit 4f95fcd9 by Carsten Brandt

added unit tests for schema detection

fixed some issues with schema detection
parent 1cef60db
......@@ -99,6 +99,8 @@ class ColumnSchema extends Object
return (integer) $value;
case 'boolean':
return (boolean) $value;
case 'double':
return (double) $value;
}
return $value;
......
......@@ -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'
......
......@@ -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;
......
......@@ -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);
}
}
......
<?php
/* @var $this \yii\web\View */
/* @var $exception \Exception */
/* @var $handler \yii\web\ErrorHandler */
?>
......
......@@ -108,18 +108,20 @@ CREATE TABLE null_values (
);
CREATE TABLE `type` (
`int_col` int(11) NOT NULL,
`int_col2` int(11) DEFAULT '1',
`int_col` integer NOT NULL,
`int_col2` integer DEFAULT '1',
`char_col` char(100) NOT NULL,
`char_col2` varchar(100) DEFAULT 'something',
`char_col3` text,
`enum_col` enum('a', 'B'),
`float_col` double(4,3) NOT NULL,
`float_col2` double DEFAULT '1.23',
`blob_col` blob,
`numeric_col` decimal(5,2) DEFAULT '33.22',
`time` timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
`bool_col` tinyint(1) NOT NULL,
`bool_col2` tinyint(1) DEFAULT '1'
`bool_col2` tinyint(1) DEFAULT '1',
`ts_default` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `profile` (description) VALUES ('profile customer 1');
......
......@@ -99,7 +99,8 @@ CREATE TABLE "type" (
numeric_col decimal(5,2) DEFAULT '33.22',
time timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
bool_col smallint NOT NULL,
bool_col2 smallint DEFAULT '1'
bool_col2 smallint DEFAULT '1',
ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO "profile" (description) VALUES ('profile customer 1');
......
......@@ -103,7 +103,8 @@ CREATE TABLE "type" (
numeric_col decimal(5,2) DEFAULT '33.22',
time timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
bool_col tinyint(1) NOT NULL,
bool_col2 tinyint(1) DEFAULT '1'
bool_col2 tinyint(1) DEFAULT '1',
ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO "profile" (description) VALUES ('profile customer 1');
......
......@@ -79,46 +79,46 @@ class ConnectionTest extends DatabaseTestCase
$this->assertEquals('(column)', $connection->quoteColumnName('(column)'));
}
public function testTransaction()
{
$connection = $this->getConnection(false);
$this->assertNull($connection->transaction);
$transaction = $connection->beginTransaction();
$this->assertNotNull($connection->transaction);
$this->assertTrue($transaction->isActive);
public function testTransaction()
{
$connection = $this->getConnection(false);
$this->assertNull($connection->transaction);
$transaction = $connection->beginTransaction();
$this->assertNotNull($connection->transaction);
$this->assertTrue($transaction->isActive);
$connection->createCommand()->insert('profile', ['description' => 'test transaction'])->execute();
$connection->createCommand()->insert('profile', ['description' => 'test transaction'])->execute();
$transaction->rollBack();
$this->assertFalse($transaction->isActive);
$this->assertNull($connection->transaction);
$transaction->rollBack();
$this->assertFalse($transaction->isActive);
$this->assertNull($connection->transaction);
$this->assertEquals(0, $connection->createCommand("SELECT COUNT(*) FROM profile WHERE description = 'test transaction';")->queryScalar());
$this->assertEquals(0, $connection->createCommand("SELECT COUNT(*) FROM profile WHERE description = 'test transaction';")->queryScalar());
$transaction = $connection->beginTransaction();
$connection->createCommand()->insert('profile', ['description' => 'test transaction'])->execute();
$transaction->commit();
$this->assertFalse($transaction->isActive);
$this->assertNull($connection->transaction);
$transaction = $connection->beginTransaction();
$connection->createCommand()->insert('profile', ['description' => 'test transaction'])->execute();
$transaction->commit();
$this->assertFalse($transaction->isActive);
$this->assertNull($connection->transaction);
$this->assertEquals(1, $connection->createCommand("SELECT COUNT(*) FROM profile WHERE description = 'test transaction';")->queryScalar());
}
$this->assertEquals(1, $connection->createCommand("SELECT COUNT(*) FROM profile WHERE description = 'test transaction';")->queryScalar());
}
public function testTransactionIsolation()
{
$connection = $this->getConnection(true);
public function testTransactionIsolation()
{
$connection = $this->getConnection(true);
$transaction = $connection->beginTransaction(Transaction::READ_UNCOMMITTED);
$transaction->commit();
$transaction = $connection->beginTransaction(Transaction::READ_UNCOMMITTED);
$transaction->commit();
$transaction = $connection->beginTransaction(Transaction::READ_COMMITTED);
$transaction->commit();
$transaction = $connection->beginTransaction(Transaction::READ_COMMITTED);
$transaction->commit();
$transaction = $connection->beginTransaction(Transaction::REPEATABLE_READ);
$transaction->commit();
$transaction = $connection->beginTransaction(Transaction::REPEATABLE_READ);
$transaction->commit();
$transaction = $connection->beginTransaction(Transaction::SERIALIZABLE);
$transaction->commit();
}
$transaction = $connection->beginTransaction(Transaction::SERIALIZABLE);
$transaction->commit();
}
}
......@@ -3,6 +3,7 @@
namespace yiiunit\framework\db;
use yii\caching\FileCache;
use yii\db\Expression;
use yii\db\Schema;
/**
......@@ -90,4 +91,210 @@ class SchemaTest extends DatabaseTestCase
}
fclose($fp);
}
public function getExpectedColumns()
{
return [
'int_col' => [
'type' => 'integer',
'dbType' => 'int(11)',
'phpType' => 'integer',
'allowNull' => false,
'autoIncrement' => false,
'enumValues' => null,
'size' => 11,
'precision' => 11,
'scale' => null,
'defaultValue' => null,
],
'int_col2' => [
'type' => 'integer',
'dbType' => 'int(11)',
'phpType' => 'integer',
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => 11,
'precision' => 11,
'scale' => null,
'defaultValue' => 1,
],
'char_col' => [
'type' => 'string',
'dbType' => 'char(100)',
'phpType' => 'string',
'allowNull' => false,
'autoIncrement' => false,
'enumValues' => null,
'size' => 100,
'precision' => 100,
'scale' => null,
'defaultValue' => null,
],
'char_col2' => [
'type' => 'string',
'dbType' => 'varchar(100)',
'phpType' => 'string',
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => 100,
'precision' => 100,
'scale' => null,
'defaultValue' => 'something',
],
'char_col3' => [
'type' => 'text',
'dbType' => 'text',
'phpType' => 'string',
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => null,
],
'enum_col' => [
'type' => 'string',
'dbType' => "enum('a','B')",
'phpType' => 'string',
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => ['a', 'B'],
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => null,
],
'float_col' => [
'type' => 'float',
'dbType' => 'double(4,3)',
'phpType' => 'double',
'allowNull' => false,
'autoIncrement' => false,
'enumValues' => null,
'size' => 4,
'precision' => 4,
'scale' => 3,
'defaultValue' => null,
],
'float_col2' => [
'type' => 'float',
'dbType' => 'double',
'phpType' => 'double',
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => 1.23,
],
'blob_col' => [
'type' => 'string',
'dbType' => 'blob',
'phpType' => 'string',
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => null,
],
'numeric_col' => [
'type' => 'decimal',
'dbType' => 'decimal(5,2)',
'phpType' => 'string',
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => 5,
'precision' => 5,
'scale' => 2,
'defaultValue' => '33.22',
],
'time' => [
'type' => 'timestamp',
'dbType' => 'timestamp',
'phpType' => 'string',
'allowNull' => false,
'autoIncrement' => false,
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => '2002-01-01 00:00:00',
],
'bool_col' => [
'type' => 'smallint',
'dbType' => 'tinyint(1)',
'phpType' => 'integer',
'allowNull' => false,
'autoIncrement' => false,
'enumValues' => null,
'size' => 1,
'precision' => 1,
'scale' => null,
'defaultValue' => null,
],
'bool_col2' => [
'type' => 'smallint',
'dbType' => 'tinyint(1)',
'phpType' => 'integer',
'allowNull' => true,
'autoIncrement' => false,
'enumValues' => null,
'size' => 1,
'precision' => 1,
'scale' => null,
'defaultValue' => 1,
],
'ts_default' => [
'type' => 'timestamp',
'dbType' => 'timestamp',
'phpType' => 'string',
'allowNull' => false,
'autoIncrement' => false,
'enumValues' => null,
'size' => null,
'precision' => null,
'scale' => null,
'defaultValue' => new Expression('CURRENT_TIMESTAMP'),
],
];
}
public function testColumnSchema()
{
$columns = $this->getExpectedColumns();
$table = $this->getConnection(false)->schema->getTableSchema('type', true);
$expectedColNames = array_keys($columns);
sort($expectedColNames);
$colNames = $table->columnNames;
sort($colNames);
$this->assertEquals($expectedColNames, $colNames);
foreach($table->columns as $name => $column) {
$expected = $columns[$name];
$this->assertSame($expected['dbType'], $column->dbType, "dbType of colum $name does not match. type is $column->type, dbType is $column->dbType.");
$this->assertSame($expected['phpType'], $column->phpType, "phpType of colum $name does not match. type is $column->type, dbType is $column->dbType.");
$this->assertSame($expected['type'], $column->type, "type of colum $name does not match.");
$this->assertSame($expected['allowNull'], $column->allowNull, "allowNull of colum $name does not match.");
$this->assertSame($expected['autoIncrement'], $column->autoIncrement, "autoIncrement of colum $name does not match.");
$this->assertSame($expected['enumValues'], $column->enumValues, "enumValues of colum $name does not match.");
$this->assertSame($expected['size'], $column->size, "size of colum $name does not match.");
$this->assertSame($expected['precision'], $column->precision, "precision of colum $name does not match.");
$this->assertSame($expected['scale'], $column->scale, "scale of colum $name does not match.");
if (is_object($expected['defaultValue'])) {
$this->assertTrue(is_object($column->defaultValue), "defaultValue of colum $name is expected to be an object but it is not.");
$this->assertEquals((string) $expected['defaultValue'], (string) $column->defaultValue, "defaultValue of colum $name does not match.");
} else {
$this->assertSame($expected['defaultValue'], $column->defaultValue, "defaultValue of colum $name does not match.");
}
}
}
}
<?php
namespace yiiunit\framework\db\cubrid;
use yii\db\Expression;
use yiiunit\framework\db\SchemaTest;
/**
......@@ -33,4 +34,37 @@ class CubridSchemaTest extends SchemaTest
}
fclose($fp);
}
public function getExpectedColumns()
{
$columns = parent::getExpectedColumns();
$columns['int_col']['dbType'] = 'integer';
$columns['int_col']['size'] = null;
$columns['int_col']['precision'] = null;
$columns['int_col2']['dbType'] = 'integer';
$columns['int_col2']['size'] = null;
$columns['int_col2']['precision'] = null;
$columns['char_col3']['type'] = 'string';
$columns['char_col3']['dbType'] = 'varchar(1073741823)';
$columns['char_col3']['size'] = 1073741823;
$columns['char_col3']['precision'] = 1073741823;
$columns['enum_col']['dbType'] = "enum('a', 'B')";
$columns['float_col']['dbType'] = 'double';
$columns['float_col']['size'] = null;
$columns['float_col']['precision'] = null;
$columns['float_col']['scale'] = null;
$columns['numeric_col']['dbType'] = 'numeric(5,2)';
$columns['blob_col']['phpType'] = 'resource';
$columns['blob_col']['type'] = 'binary';
$columns['bool_col']['dbType'] = 'short';
$columns['bool_col']['size'] = null;
$columns['bool_col']['precision'] = null;
$columns['bool_col2']['dbType'] = 'short';
$columns['bool_col2']['size'] = null;
$columns['bool_col2']['precision'] = null;
$columns['time']['defaultValue'] = '12:00:00 AM 01/01/2002';
$columns['ts_default']['defaultValue'] = new Expression('SYS_TIMESTAMP');
return $columns;
}
}
......@@ -50,28 +50,28 @@ class PostgreSQLConnectionTest extends ConnectionTest
$this->assertEquals('(column)', $connection->quoteColumnName('(column)'));
}
public function testTransactionIsolation()
{
$connection = $this->getConnection(true);
public function testTransactionIsolation()
{
$connection = $this->getConnection(true);
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::READ_UNCOMMITTED);
$transaction->commit();
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::READ_UNCOMMITTED);
$transaction->commit();
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::READ_COMMITTED);
$transaction->commit();
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::READ_COMMITTED);
$transaction->commit();
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::REPEATABLE_READ);
$transaction->commit();
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::REPEATABLE_READ);
$transaction->commit();
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::SERIALIZABLE);
$transaction->commit();
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::SERIALIZABLE);
$transaction->commit();
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::SERIALIZABLE . ' READ ONLY DEFERRABLE');
$transaction->commit();
}
$transaction = $connection->beginTransaction();
$transaction->setIsolationLevel(Transaction::SERIALIZABLE . ' READ ONLY DEFERRABLE');
$transaction->commit();
}
}
<?php
namespace yiiunit\framework\db\pgsql;
use yii\db\pgsql\Schema;
use yiiunit\framework\db\QueryTest;
use yiiunit\framework\db\SchemaTest;
/**
* @group db
* @group pgsql
*/
class PostgreSQLQueryTest extends QueryTest
{
public $driverName = 'pgsql';
}
<?php
namespace yiiunit\framework\db\pgsql;
use yii\db\pgsql\Schema;
use yiiunit\framework\db\SchemaTest;
/**
* @group db
* @group pgsql
*/
class PostgreSQLSchemaTest extends SchemaTest
{
public $driverName = 'pgsql';
public function getExpectedColumns()
{
$columns = parent::getExpectedColumns();
unset($columns['enum_col']);
$columns['int_col']['dbType'] = 'integer';
$columns['int_col']['size'] = null;
$columns['int_col']['precision'] = null;
$columns['int_col2']['dbType'] = 'integer';
$columns['int_col2']['size'] = null;
$columns['int_col2']['precision'] = null;
$columns['bool_col']['type'] = 'boolean';
$columns['bool_col']['phpType'] = 'boolean';
$columns['bool_col2']['type'] = 'boolean';
$columns['bool_col2']['phpType'] = 'boolean';
$columns['bool_col2']['defaultValue'] = true;
return $columns;
}
}
......@@ -47,14 +47,14 @@ class SqliteConnectionTest extends ConnectionTest
$this->assertEquals('(column)', $connection->quoteColumnName('(column)'));
}
public function testTransactionIsolation()
{
$connection = $this->getConnection(true);
public function testTransactionIsolation()
{
$connection = $this->getConnection(true);
$transaction = $connection->beginTransaction(Transaction::READ_UNCOMMITTED);
$transaction->rollBack();
$transaction = $connection->beginTransaction(Transaction::READ_UNCOMMITTED);
$transaction->rollBack();
$transaction = $connection->beginTransaction(Transaction::SERIALIZABLE);
$transaction->rollBack();
}
$transaction = $connection->beginTransaction(Transaction::SERIALIZABLE);
$transaction->rollBack();
}
}
......@@ -10,4 +10,23 @@ use yiiunit\framework\db\SchemaTest;
class SqliteSchemaTest extends SchemaTest
{
protected $driverName = 'sqlite';
public function getExpectedColumns()
{
$columns = parent::getExpectedColumns();
unset($columns['enum_col']);
$columns['int_col']['dbType'] = 'integer';
$columns['int_col']['size'] = null;
$columns['int_col']['precision'] = null;
$columns['int_col2']['dbType'] = 'integer';
$columns['int_col2']['size'] = null;
$columns['int_col2']['precision'] = null;
$columns['bool_col']['type'] = 'boolean';
$columns['bool_col']['phpType'] = 'boolean';
$columns['bool_col2']['type'] = 'boolean';
$columns['bool_col2']['phpType'] = 'boolean';
$columns['bool_col2']['defaultValue'] = true;
return $columns;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment