Browse Source

Fixes #17356: MSSQL Schema was not detecting string field size

tags/2.0.21
Sidney Lins 5 years ago committed by Alexander Makarov
parent
commit
fe3ebe2e56
  1. 1
      framework/CHANGELOG.md
  2. 4
      framework/db/Connection.php
  3. 10
      framework/db/mssql/Schema.php
  4. 9
      tests/framework/db/SchemaTest.php
  5. 41
      tests/framework/db/mssql/SchemaTest.php

1
framework/CHANGELOG.md

@ -9,6 +9,7 @@ Yii Framework 2 Change Log
- Enh #17345: Improved performance of `yii\db\Connection::quoteColumnName()` (brandonkelly)
- Enh #17348: Improved performance of `yii\db\Connection::quoteTableName()` (brandonkelly)
- Enh #17353: Added `sameSite` support for `yii\web\Cookie` and `yii\web\Session::cookieParams` (rhertogh)
- Bug #17356: MSSQL Schema was not detecting string field size (ricarnevale, sdlins)
- Bug #17341: Allowed callable objects to be set to `\yii\filters\AccessRule::$roleParams` (alexkart)
- Bug #17070: Striped invalid character from fallback file name in `Content-Disposition` header when using `\yii\web\Response::sendFile` (alexkart)
- Bug #16565: Added missing parts of the context message in `\yii\log\Target::collect` (alexkart)

4
framework/db/Connection.php

@ -706,7 +706,9 @@ class Connection extends Component
{
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($this->emulatePrepare !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
if ($this->driverName !== 'sqlsrv') {
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
}
}
if ($this->charset !== null && in_array($this->getDriverName(), ['pgsql', 'mysql', 'mysqli', 'cubrid'], true)) {
$this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset));

10
framework/db/mssql/Schema.php

@ -399,7 +399,15 @@ SQL;
SELECT
[t1].[column_name],
[t1].[is_nullable],
[t1].[data_type],
CASE WHEN [t1].[data_type] IN ('char','varchar','nchar','nvarchar','binary','varbinary') THEN
CASE WHEN [t1].[character_maximum_length] = NULL OR [t1].[character_maximum_length] = -1 THEN
[t1].[data_type]
ELSE
[t1].[data_type] + '(' + LTRIM(RTRIM(CONVERT(CHAR,[t1].[character_maximum_length]))) + ')'
END
ELSE
[t1].[data_type]
END AS 'data_type',
[t1].[column_default],
COLUMNPROPERTY(OBJECT_ID([t1].[table_schema] + '.' + [t1].[table_name]), [t1].[column_name], 'IsIdentity') AS is_identity,
(

9
tests/framework/db/SchemaTest.php

@ -54,8 +54,12 @@ abstract class SchemaTest extends DatabaseTestCase
{
$connection = $this->getConnection();
foreach ($pdoAttributes as $name => $value) {
if ($name === PDO::ATTR_EMULATE_PREPARES && $connection->driverName === 'sqlsrv') {
continue;
}
$connection->pdo->setAttribute($name, $value);
}
/* @var $schema Schema */
$schema = $connection->schema;
@ -78,6 +82,9 @@ abstract class SchemaTest extends DatabaseTestCase
{
$connection = $this->getConnection();
foreach ($pdoAttributes as $name => $value) {
if ($name === PDO::ATTR_EMULATE_PREPARES && $connection->driverName === 'sqlsrv') {
continue;
}
$connection->pdo->setAttribute($name, $value);
}
/* @var $schema Schema */
@ -568,7 +575,7 @@ abstract class SchemaTest extends DatabaseTestCase
'somecolUnique' => ['somecol'],
'someCol2Unique' => ['someCol2'],
], $uniqueIndexes);
// see https://github.com/yiisoft/yii2/issues/13814
$db->createCommand()->createIndex('another unique index', 'uniqueIndex', 'someCol2', true)->execute();

41
tests/framework/db/mssql/SchemaTest.php

@ -42,4 +42,45 @@ class SchemaTest extends \yiiunit\framework\db\SchemaTest
$result['4: default'][2] = [];
return $result;
}
public function testGetStringFieldsSize()
{
/* @var $db Connection */
$db = $this->getConnection();
/* @var $schema Schema */
$schema = $db->schema;
$columns = $schema->getTableSchema('type', false)->columns;
foreach ($columns as $name => $column) {
$type = $column->type;
$size = $column->size;
$dbType = $column->dbType;
if (strpos($name, 'char_') === 0) {
switch ($name) {
case 'char_col':
$expectedType = 'char';
$expectedSize = 100;
$expectedDbType = 'char(100)';
break;
case 'char_col2':
$expectedType = 'string';
$expectedSize = 100;
$expectedDbType = "varchar(100)";
break;
case 'char_col3':
$expectedType = 'text';
$expectedSize = null;
$expectedDbType = 'text';
break;
}
$this->assertEquals($expectedType, $type);
$this->assertEquals($expectedSize, $size);
$this->assertEquals($expectedDbType, $dbType);
}
}
}
}

Loading…
Cancel
Save