Browse Source

fix mixed case column names for unique indexes in postgres

fixes #10613
tags/2.0.9
Carsten Brandt 8 years ago
parent
commit
7fb447506d
  1. 1
      framework/CHANGELOG.md
  2. 8
      framework/db/pgsql/Schema.php
  3. 36
      tests/framework/db/SchemaTest.php

1
framework/CHANGELOG.md

@ -56,6 +56,7 @@ Yii Framework 2 Change Log
- Bug #11878: Fixed i18n gettext fallback language message loading (stevekr)
- Enh #11212: Added headers to PO file in `yii\i18n\GettextPoFile::save()` (stevekr)
- Bug #6347: `inverseOf()` not working for dynamic relational queries (laszlovl)
- Bug #10613: Fixed PostgreSQL Schema to return correct column names for unique indexes that have mixed case column names (cebe)
2.0.8 April 28, 2016

8
framework/db/pgsql/Schema.php

@ -370,7 +370,13 @@ SQL;
$rows = $this->getUniqueIndexInformation($table);
foreach ($rows as $row) {
$uniqueIndexes[$row['indexname']][] = $row['columnname'];
$column = $row['columnname'];
if (!empty($column) && $column[0] === '"') {
// postgres will quote names that are not lowercase-only
// https://github.com/yiisoft/yii2/issues/10613
$column = substr($column, 1, -1);
}
$uniqueIndexes[$row['indexname']][] = $column;
}
return $uniqueIndexes;

36
tests/framework/db/SchemaTest.php

@ -377,4 +377,40 @@ abstract class SchemaTest extends DatabaseTestCase
$this->assertSame('', $columnSchema->dbTypecast(''));
}
public function testFindUniqueIndexes()
{
$db = $this->getConnection();
try {
$db->createCommand()->dropTable('uniqueIndex')->execute();
} catch(\Exception $e) {
}
$db->createCommand()->createTable('uniqueIndex', [
'somecol' => 'string',
'someCol2' => 'string',
])->execute();
/* @var $schema Schema */
$schema = $db->schema;
$uniqueIndexes = $schema->findUniqueIndexes($schema->getTableSchema('uniqueIndex', true));
$this->assertEquals([], $uniqueIndexes);
$db->createCommand()->createIndex('somecolUnique', 'uniqueIndex', 'somecol', true)->execute();
$uniqueIndexes = $schema->findUniqueIndexes($schema->getTableSchema('uniqueIndex', true));
$this->assertEquals([
'somecolUnique' => ['somecol'],
], $uniqueIndexes);
// create another column with upper case letter that fails postgres
// see https://github.com/yiisoft/yii2/issues/10613
$db->createCommand()->createIndex('someCol2Unique', 'uniqueIndex', 'someCol2', true)->execute();
$uniqueIndexes = $schema->findUniqueIndexes($schema->getTableSchema('uniqueIndex', true));
$this->assertEquals([
'somecolUnique' => ['somecol'],
'someCol2Unique' => ['someCol2'],
], $uniqueIndexes);
}
}

Loading…
Cancel
Save