Skip to content

Commit 4912fc2

Browse files
committed
Fix autoIncrement reflection in SQLServer
SQLServer was not correctly reflecting the identity-ness of a column. We cannot get this information from INFORMATION_SCHEMA so we'll use other SQLServer specific catalog views.
1 parent 8c69ebf commit 4912fc2

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/Database/Schema/SqlserverSchema.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,20 @@ public function listTablesSql($config)
4242
*/
4343
public function describeColumnSql($tableName, $config)
4444
{
45-
$sql = "SELECT DISTINCT TABLE_SCHEMA AS [schema], COLUMN_NAME AS [name], DATA_TYPE AS [type],
46-
IS_NULLABLE AS [null], COLUMN_DEFAULT AS [default],
47-
CHARACTER_MAXIMUM_LENGTH AS [char_length],
48-
NUMERIC_PRECISION AS [precision],
49-
NUMERIC_SCALE AS [scale],
50-
'' AS [comment], ORDINAL_POSITION AS [ordinal_position]
51-
FROM INFORMATION_SCHEMA.COLUMNS
52-
WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?
53-
ORDER BY ordinal_position";
45+
$sql = "SELECT DISTINCT
46+
AC.name AS [name],
47+
TY.name AS [type],
48+
AC.max_length AS [char_length],
49+
AC.precision AS [precision],
50+
AC.scale AS [scale],
51+
AC.is_identity AS [autoincrement],
52+
AC.is_nullable AS [null],
53+
OBJECT_DEFINITION(AC.default_object_id) AS [default]
54+
FROM sys.[tables] T
55+
INNER JOIN sys.[schemas] S ON S.[schema_id] = T.[schema_id]
56+
INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
57+
INNER JOIN sys.[types] TY ON TY.[user_type_id] = AC.[user_type_id]
58+
WHERE T.[name] = ? AND S.[name] = ?";
5459

5560
$schema = empty($config['schema']) ? static::DEFAULT_SCHEMA_NAME : $config['schema'];
5661
return [$sql, [$tableName, $schema]];
@@ -146,13 +151,15 @@ public function convertColumnDescription(Table $table, $row)
146151
if (!empty($row['default'])) {
147152
$row['default'] = trim($row['default'], '()');
148153
}
149-
154+
if (!empty($row['autoincrement'])) {
155+
$field['autoIncrement'] = true;
156+
}
150157
if ($field['type'] === 'boolean') {
151158
$row['default'] = (int)$row['default'];
152159
}
153160

154161
$field += [
155-
'null' => $row['null'] === 'YES' ? true : false,
162+
'null' => $row['null'] === '1' ? true : false,
156163
'default' => $row['default'],
157164
];
158165
$table->addColumn($row['name'], $field);

tests/TestCase/Database/Schema/SqlserverSchemaTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public function testConvertColumn($type, $length, $precision, $scale, $expected)
243243
$field = [
244244
'name' => 'field',
245245
'type' => $type,
246-
'null' => 'YES',
246+
'null' => '1',
247247
'default' => 'Default value',
248248
'char_length' => $length,
249249
'precision' => $precision,
@@ -364,6 +364,33 @@ public function testDescribeTable()
364364
}
365365
}
366366

367+
/**
368+
* Test describing a table with postgres and composite keys
369+
*
370+
* @return void
371+
*/
372+
public function testDescribeTableCompositeKey()
373+
{
374+
$this->_needsConnection();
375+
$connection = ConnectionManager::get('test');
376+
$sql = <<<SQL
377+
CREATE TABLE schema_composite (
378+
[id] INTEGER IDENTITY(1, 1),
379+
[site_id] INTEGER NOT NULL,
380+
[name] VARCHAR(255),
381+
PRIMARY KEY([id], [site_id])
382+
);
383+
SQL;
384+
$connection->execute($sql);
385+
$schema = new SchemaCollection($connection);
386+
$result = $schema->describe('schema_composite');
387+
$connection->execute('DROP TABLE schema_composite');
388+
389+
$this->assertEquals(['id', 'site_id'], $result->primaryKey());
390+
$this->assertNull($result->column('site_id')['autoIncrement'], 'site_id should not be autoincrement');
391+
$this->assertTrue($result->column('id')['autoIncrement'], 'id should be autoincrement');
392+
}
393+
367394
/**
368395
* Test that describe accepts tablenames containing `schema.table`.
369396
*

0 commit comments

Comments
 (0)