Skip to content

Commit

Permalink
Default column expressions do not work on SQL Server
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed May 22, 2019
1 parent 06d1d8c commit 48c38b0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 47 deletions.
31 changes: 0 additions & 31 deletions lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types;
use InvalidArgumentException;
use function array_merge;
use function array_unique;
Expand Down Expand Up @@ -1602,36 +1601,6 @@ public function getBlobTypeDeclarationSQL(array $field)
return 'VARBINARY(MAX)';
}

/**
* {@inheritDoc}
*/
public function getDefaultValueDeclarationSQL($field)
{
if (! isset($field['default'])) {
return empty($field['notnull']) ? ' NULL' : '';
}

if (! isset($field['type'])) {
return " DEFAULT '" . $field['default'] . "'";
}

$type = $field['type'];

if ($type instanceof Types\PhpIntegerMappingType) {
return ' DEFAULT ' . $field['default'];
}

if ($type instanceof Types\PhpDateTimeMappingType && $field['default'] === $this->getCurrentTimestampSQL()) {
return ' DEFAULT ' . $this->getCurrentTimestampSQL();
}

if ($type instanceof Types\BooleanType) {
return " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
}

return ' DEFAULT ' . $this->quoteStringLiteral($field['default']);
}

/**
* {@inheritdoc}
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\DBAL\Functional\Platform;

use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Types;
use Doctrine\Tests\DbalFunctionalTestCase;
use function sprintf;

class DefaultExpressionTest extends DbalFunctionalTestCase
{
public function testCurrentDate() : void
{
$platform = $this->connection->getDatabasePlatform();

if ($platform instanceof MySqlPlatform) {
self::markTestSkipped('Not supported on MySQL');
}

$this->assertDefaultExpression(Types::DATE_MUTABLE, static function (AbstractPlatform $platform) : string {
return $platform->getCurrentDateSQL();
});
}

public function testCurrentTime() : void
{
$platform = $this->connection->getDatabasePlatform();

if ($platform instanceof MySqlPlatform) {
self::markTestSkipped('Not supported on MySQL');
}

if ($platform instanceof OraclePlatform) {
self::markTestSkipped('Not supported on Oracle');
}

$this->assertDefaultExpression(Types::TIME_MUTABLE, static function (AbstractPlatform $platform) : string {
return $platform->getCurrentTimeSQL();
});
}

public function testCurrentTimestamp() : void
{
$this->assertDefaultExpression(Types::DATETIME_MUTABLE, static function (AbstractPlatform $platform) : string {
return $platform->getCurrentTimestampSQL();
});
}

private function assertDefaultExpression(string $type, callable $expression) : void
{
$platform = $this->connection->getDatabasePlatform();
$defaultSql = $expression($platform, $this);

$table = new Table('default_expr_test');
$table->addColumn('actual_value', $type);
$table->addColumn('default_value', $type, ['default' => $defaultSql]);
$this->connection->getSchemaManager()->dropAndCreateTable($table);

$this->connection->exec(
sprintf(
'INSERT INTO default_expr_test (actual_value) VALUES (%s)',
$defaultSql
)
);

[$actualValue, $defaultValue] = $this->connection->query(
'SELECT default_value, actual_value FROM default_expr_test'
)->fetch(FetchMode::NUMERIC);

self::assertEquals($actualValue, $defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,14 @@ public function testColumnCollation()

public function testDefaultConstraints()
{
$platform = $this->schemaManager->getDatabasePlatform();
$table = new Table('sqlsrv_default_constraints');
$table = new Table('sqlsrv_default_constraints');
$table->addColumn('no_default', 'string');
$table->addColumn('df_integer', 'integer', ['default' => 666]);
$table->addColumn('df_string_1', 'string', ['default' => 'foobar']);
$table->addColumn('df_string_2', 'string', ['default' => 'Doctrine rocks!!!']);
$table->addColumn('df_string_3', 'string', ['default' => 'another default value']);
$table->addColumn('df_string_4', 'string', ['default' => 'column to rename']);
$table->addColumn('df_boolean', 'boolean', ['default' => true]);
$table->addColumn('df_current_date', 'date', ['default' => $platform->getCurrentDateSQL()]);
$table->addColumn('df_current_time', 'time', ['default' => $platform->getCurrentTimeSQL()]);

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

$diff = new TableDiff(
'sqlsrv_default_constraints',
[new Column('df_current_timestamp', Type::getType('datetime'), ['default' => 'CURRENT_TIMESTAMP'])],
[],
[
'df_integer' => new ColumnDiff(
'df_integer',
Expand Down Expand Up @@ -126,7 +121,6 @@ public function testDefaultConstraints()
$columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');

self::assertNull($columns['no_default']->getDefault());
self::assertEquals('CURRENT_TIMESTAMP', $columns['df_current_timestamp']->getDefault());
self::assertEquals(0, $columns['df_integer']->getDefault());
self::assertNull($columns['df_string_2']->getDefault());
self::assertEquals('another default value', $columns['df_string_3']->getDefault());
Expand All @@ -140,12 +134,6 @@ public function testDefaultConstraints()
'sqlsrv_default_constraints',
[],
[
'df_current_timestamp' => new ColumnDiff(
'df_current_timestamp',
new Column('df_current_timestamp', Type::getType('datetime')),
['default'],
new Column('df_current_timestamp', Type::getType('datetime'), ['default' => 'CURRENT_TIMESTAMP'])
),
'df_integer' => new ColumnDiff(
'df_integer',
new Column('df_integer', Type::getType('integer'), ['default' => 666]),
Expand All @@ -163,7 +151,6 @@ public function testDefaultConstraints()
$this->schemaManager->alterTable($diff);
$columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ public function testGetDefaultValueDeclarationSQLForDateType() : void
];

self::assertSame(
" DEFAULT '" . $currentDateSql . "'",
' DEFAULT CONVERT(date, GETDATE())',
$this->platform->getDefaultValueDeclarationSQL($field)
);
}
Expand Down

0 comments on commit 48c38b0

Please sign in to comment.