Skip to content

Commit 9c07718

Browse files
committed
Default column expressions do not work on SQL Server
1 parent 06d1d8c commit 9c07718

File tree

4 files changed

+85
-47
lines changed

4 files changed

+85
-47
lines changed

lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Doctrine\DBAL\Schema\Index;
1111
use Doctrine\DBAL\Schema\Table;
1212
use Doctrine\DBAL\Schema\TableDiff;
13-
use Doctrine\DBAL\Types;
1413
use InvalidArgumentException;
1514
use function array_merge;
1615
use function array_unique;
@@ -1602,36 +1601,6 @@ public function getBlobTypeDeclarationSQL(array $field)
16021601
return 'VARBINARY(MAX)';
16031602
}
16041603

1605-
/**
1606-
* {@inheritDoc}
1607-
*/
1608-
public function getDefaultValueDeclarationSQL($field)
1609-
{
1610-
if (! isset($field['default'])) {
1611-
return empty($field['notnull']) ? ' NULL' : '';
1612-
}
1613-
1614-
if (! isset($field['type'])) {
1615-
return " DEFAULT '" . $field['default'] . "'";
1616-
}
1617-
1618-
$type = $field['type'];
1619-
1620-
if ($type instanceof Types\PhpIntegerMappingType) {
1621-
return ' DEFAULT ' . $field['default'];
1622-
}
1623-
1624-
if ($type instanceof Types\PhpDateTimeMappingType && $field['default'] === $this->getCurrentTimestampSQL()) {
1625-
return ' DEFAULT ' . $this->getCurrentTimestampSQL();
1626-
}
1627-
1628-
if ($type instanceof Types\BooleanType) {
1629-
return " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
1630-
}
1631-
1632-
return ' DEFAULT ' . $this->quoteStringLiteral($field['default']);
1633-
}
1634-
16351604
/**
16361605
* {@inheritdoc}
16371606
*
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\DBAL\Functional\Platform;
6+
7+
use Doctrine\DBAL\FetchMode;
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
9+
use Doctrine\DBAL\Platforms\MySqlPlatform;
10+
use Doctrine\DBAL\Platforms\OraclePlatform;
11+
use Doctrine\DBAL\Schema\Table;
12+
use Doctrine\DBAL\Types\Types;
13+
use Doctrine\Tests\DbalFunctionalTestCase;
14+
use function sprintf;
15+
16+
class DefaultExpressionTest extends DbalFunctionalTestCase
17+
{
18+
/**
19+
* @dataProvider expressionProvider
20+
*/
21+
public function testDefaultExpression(string $type, callable $expression) : void
22+
{
23+
$platform = $this->connection->getDatabasePlatform();
24+
$defaultSql = $expression($platform, $this);
25+
26+
$table = new Table('default_expr_test');
27+
$table->addColumn('actual_value', $type);
28+
$table->addColumn('default_value', $type, ['default' => $defaultSql]);
29+
$this->connection->getSchemaManager()->dropAndCreateTable($table);
30+
31+
$query = sprintf(
32+
'INSERT INTO default_expr_test (actual_value) VALUES (%s)',
33+
$defaultSql
34+
);
35+
36+
$this->connection->exec($query);
37+
38+
$query = sprintf('SELECT default_value, actual_value FROM default_expr_test');
39+
[$actualValue, $defaultValue] = $this->connection->query($query)->fetch(FetchMode::NUMERIC);
40+
41+
self::assertEquals($actualValue, $defaultValue);
42+
}
43+
44+
/**
45+
* @return mixed[][]
46+
*/
47+
public static function expressionProvider() : iterable
48+
{
49+
yield 'current-date' => [
50+
Types::DATE_MUTABLE,
51+
static function (AbstractPlatform $platform) : string {
52+
if ($platform instanceof MySqlPlatform) {
53+
self::markTestSkipped('Not supported on MySQL');
54+
}
55+
56+
return $platform->getCurrentDateSQL();
57+
},
58+
];
59+
60+
yield 'current-time' => [
61+
Types::TIME_MUTABLE,
62+
static function (AbstractPlatform $platform) : string {
63+
if ($platform instanceof MySqlPlatform) {
64+
self::markTestSkipped('Not supported on MySQL');
65+
}
66+
67+
if ($platform instanceof OraclePlatform) {
68+
self::markTestSkipped('Not supported on Oracle');
69+
}
70+
71+
return $platform->getCurrentTimeSQL();
72+
},
73+
];
74+
75+
yield 'current-timestamp' => [
76+
Types::DATETIME_MUTABLE,
77+
static function (AbstractPlatform $platform) : string {
78+
return $platform->getCurrentTimestampSQL();
79+
},
80+
];
81+
}
82+
}

tests/Doctrine/Tests/DBAL/Functional/Schema/SQLServerSchemaManagerTest.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,14 @@ public function testColumnCollation()
5454

5555
public function testDefaultConstraints()
5656
{
57-
$platform = $this->schemaManager->getDatabasePlatform();
58-
$table = new Table('sqlsrv_default_constraints');
57+
$table = new Table('sqlsrv_default_constraints');
5958
$table->addColumn('no_default', 'string');
6059
$table->addColumn('df_integer', 'integer', ['default' => 666]);
6160
$table->addColumn('df_string_1', 'string', ['default' => 'foobar']);
6261
$table->addColumn('df_string_2', 'string', ['default' => 'Doctrine rocks!!!']);
6362
$table->addColumn('df_string_3', 'string', ['default' => 'another default value']);
6463
$table->addColumn('df_string_4', 'string', ['default' => 'column to rename']);
6564
$table->addColumn('df_boolean', 'boolean', ['default' => true]);
66-
$table->addColumn('df_current_date', 'date', ['default' => $platform->getCurrentDateSQL()]);
67-
$table->addColumn('df_current_time', 'time', ['default' => $platform->getCurrentTimeSQL()]);
6865

6966
$this->schemaManager->createTable($table);
7067
$columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');
@@ -75,12 +72,10 @@ public function testDefaultConstraints()
7572
self::assertEquals('Doctrine rocks!!!', $columns['df_string_2']->getDefault());
7673
self::assertEquals('another default value', $columns['df_string_3']->getDefault());
7774
self::assertEquals(1, $columns['df_boolean']->getDefault());
78-
self::assertSame($platform->getCurrentDateSQL(), $columns['df_current_date']->getDefault());
79-
self::assertSame($platform->getCurrentTimeSQL(), $columns['df_current_time']->getDefault());
8075

8176
$diff = new TableDiff(
8277
'sqlsrv_default_constraints',
83-
[new Column('df_current_timestamp', Type::getType('datetime'), ['default' => 'CURRENT_TIMESTAMP'])],
78+
[],
8479
[
8580
'df_integer' => new ColumnDiff(
8681
'df_integer',
@@ -126,7 +121,6 @@ public function testDefaultConstraints()
126121
$columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');
127122

128123
self::assertNull($columns['no_default']->getDefault());
129-
self::assertEquals('CURRENT_TIMESTAMP', $columns['df_current_timestamp']->getDefault());
130124
self::assertEquals(0, $columns['df_integer']->getDefault());
131125
self::assertNull($columns['df_string_2']->getDefault());
132126
self::assertEquals('another default value', $columns['df_string_3']->getDefault());
@@ -140,12 +134,6 @@ public function testDefaultConstraints()
140134
'sqlsrv_default_constraints',
141135
[],
142136
[
143-
'df_current_timestamp' => new ColumnDiff(
144-
'df_current_timestamp',
145-
new Column('df_current_timestamp', Type::getType('datetime')),
146-
['default'],
147-
new Column('df_current_timestamp', Type::getType('datetime'), ['default' => 'CURRENT_TIMESTAMP'])
148-
),
149137
'df_integer' => new ColumnDiff(
150138
'df_integer',
151139
new Column('df_integer', Type::getType('integer'), ['default' => 666]),
@@ -163,7 +151,6 @@ public function testDefaultConstraints()
163151
$this->schemaManager->alterTable($diff);
164152
$columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');
165153

166-
self::assertNull($columns['df_current_timestamp']->getDefault());
167154
self::assertEquals(666, $columns['df_integer']->getDefault());
168155
}
169156

tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ public function testGetDefaultValueDeclarationSQLForDateType() : void
14961496
];
14971497

14981498
self::assertSame(
1499-
" DEFAULT '" . $currentDateSql . "'",
1499+
' DEFAULT CONVERT(date, GETDATE())',
15001500
$this->platform->getDefaultValueDeclarationSQL($field)
15011501
);
15021502
}

0 commit comments

Comments
 (0)