Skip to content

Commit

Permalink
#670 -- Correctly handle default values for datetime and datetimetz c…
Browse files Browse the repository at this point in the history
…olumns.
  • Loading branch information
sarcher committed Jul 31, 2015
1 parent cc2d503 commit c9b6384
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ protected function _getPortableTableColumnDefinition($tableColumn)
case 'year':
$length = null;
break;
case 'timestamp':
case 'timestamptz':
if ($tableColumn['default'] == 'now()') {
$tableColumn['default'] = $this->_platform->getCurrentTimestampSQL();
}
break;
}

if ($tableColumn['default'] && preg_match("('([^']+)'::)", $tableColumn['default'], $match)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function testReturnsDatabaseNameWithoutDatabaseNameParameter()
$this->getDatabaseNameForConnectionWithoutDatabaseNameParameter(),
$this->driver->getDatabase($connection)
);

$connection->close();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,111 @@ public function testPartialIndexes()
);
}

/**
* Verify that an existing datetime column in the database
* that is set to default to the current time will be correctly
* transformed to "CURRENT_TIMESTAMP" by the schema manager,
* thereby resulting in no change.
*
* @group DBAL-983
*/
public function testDatetimeDefaultNow()
{
$table = new \Doctrine\DBAL\Schema\Table('test_datetime_default_now_table');

$table->addColumn('id', 'integer');
$table->addColumn('test_column', 'datetime', array('default' => 'CURRENT_TIMESTAMP'));

$this->_sm->createTable($table);

$databaseTable = $this->_sm->listTableDetails($table->getName());

$c = new \Doctrine\DBAL\Schema\Comparator();
$diff = $c->diffTable($table, $databaseTable);

$this->assertFalse($diff);
}

/**
* Verify that an existing datetimetz column in the database
* that is set to default to the current time will be correctly
* transformed to "CURRENT_TIMESTAMP" by the schema manager,
* thereby resulting in no change.
*
* @group DBAL-983
*/
public function testDatetimeTzDefaultNow()
{
$table = new \Doctrine\DBAL\Schema\Table('test_datetimetz_default_now_table');

$table->addColumn('id', 'integer');
$table->addColumn('test_column', 'datetimetz', array('default' => 'CURRENT_TIMESTAMP'));

$this->_sm->createTable($table);

$databaseTable = $this->_sm->listTableDetails($table->getName());

$c = new \Doctrine\DBAL\Schema\Comparator();
$diff = $c->diffTable($table, $databaseTable);

$this->assertFalse($diff);
}

/**
* Verify that an existing datetime column in the database
* that is set to default to a timestamp value will be correctly
* handled by the schema manager, resulting in no change.
*
* @group DBAL-983
*/
public function testDatetimeDefaultTimestamp()
{
$testTimestamp = '2014-08-29 18:01:01.370568';

$table = new \Doctrine\DBAL\Schema\Table('test_datetime_default_table');

$table->addColumn('id', 'integer');
$table->addColumn('test_column', 'datetime', array('default' => $testTimestamp));

$this->_sm->createTable($table);

$databaseTable = $this->_sm->listTableDetails($table->getName());

$c = new \Doctrine\DBAL\Schema\Comparator();
$diff = $c->diffTable($table, $databaseTable);

$this->assertFalse($diff);
}

/**
* Verify that an existing datetimetz column in the database
* that is set to default to a timestamp value will be correctly
* handled by the schema manager, resulting in no change.
*
* @group DBAL-983
*/
public function testDatetimeTzDefaultTimestamp()
{
// must determine server time zone for proper comparison
$serverTime = $this->_conn->fetchAssoc('SELECT CURRENT_TIMESTAMP AS timestamp_with_tz');
$timeZoneOffset = substr($serverTime['timestamp_with_tz'], -3);

$testTimestamp = '2014-08-29 18:01:01.370568' . $timeZoneOffset;

$table = new \Doctrine\DBAL\Schema\Table('test_datetimetz_default_table');

$table->addColumn('id', 'integer');
$table->addColumn('test_column', 'datetimetz', array('default' => $testTimestamp));

$this->_sm->createTable($table);

$databaseTable = $this->_sm->listTableDetails($table->getName());

$c = new \Doctrine\DBAL\Schema\Comparator();
$diff = $c->diffTable($table, $databaseTable);

$this->assertFalse($diff);
}
}

class MoneyType extends Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public function testNamedPrimaryKey()
$sql = $this->_platform->getAlterTableSQL($diff);

$this->assertEquals(array(
"ALTER TABLE mytable DROP PRIMARY KEY",
"ALTER TABLE mytable DROP PRIMARY KEY",
"ALTER TABLE mytable ADD PRIMARY KEY (foo)",
), $sql);
}
Expand Down Expand Up @@ -630,6 +630,16 @@ protected function getQuotedAlterTableChangeColumnLengthSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getGenerateAlterDefaultSql()
{
return array(
"ALTER TABLE test_table CHANGE test_column test_column VARCHAR(255) DEFAULT 'some_value' NOT NULL"
);
}

/**
* @group DBAL-423
*/
Expand Down
44 changes: 44 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,50 @@ public function testQuoteStringLiteral()
);
}

/**
* Verify that setting a string column to have a default value
* does generate the correct ALTER statement.
*
* @group DBAL-983
*/
public function testCompareDefaultString()
{
$oldColumn = new Column('test_column', Type::getType('string'), array(
'default' => null,
'length' => 255
));

$newColumn = new Column('test_column', Type::getType('string'), array(
'default' => 'some_value',
'length' => 255
));

$columnDiff = new ColumnDiff(
'test_column',
$newColumn,
array(
'default'
),
$oldColumn
);

$tableDiff = new TableDiff('test_table');
$tableDiff->fromTable = new Table('test_table');
$tableDiff->fromTable->addColumn('test_column', 'string');
$tableDiff->changedColumns[] = $columnDiff;

$sql = $this->_platform->getAlterTableSQL($tableDiff);

$this->assertEquals($this->getGenerateAlterDefaultSql(), $sql);
}

/**
* Return ALTER statement to set a string default.
*
* @return string
*/
abstract protected function getGenerateAlterDefaultSql();

/**
* @group DBAL-423
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,14 @@ public function testGetNullCommentOnColumnSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getGenerateAlterDefaultSql()
{
return array("ALTER TABLE test_table ALTER test_column SET DEFAULT 'some_value'");
}

/**
* @group DBAL-423
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,17 @@ protected function getQuotedAlterTableRenameIndexInSchemaSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getGenerateAlterDefaultSql()
{
return array(
"ALTER TABLE test_table ALTER COLUMN test_column NVARCHAR(255) NOT NULL",
"ALTER TABLE test_table ADD CONSTRAINT DF_FDF7294D_EA8A8050 DEFAULT 'some_value' FOR test_column"
);
}

/**
* @dataProvider getGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL
* @group DBAL-830
Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/DB2PlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,17 @@ protected function getQuotedAlterTableRenameIndexInSchemaSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getGenerateAlterDefaultSql()
{
return array(
"ALTER TABLE test_table ALTER COLUMN test_column SET DEFAULT 'some_value'",
"CALL SYSPROC.ADMIN_CMD ('REORG TABLE test_table')"
);
}

/**
* @group DBAL-423
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,16 @@ protected function getQuotedAlterTableRenameIndexInSchemaSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getGenerateAlterDefaultSql()
{
return array(
"ALTER TABLE test_table MODIFY (test_column VARCHAR2(255) DEFAULT 'some_value')"
);
}

/**
* @group DBAL-423
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,16 @@ protected function getQuotedAlterTableRenameIndexInSchemaSQL()
);
}

/**
* {@inheritdoc}
*/
protected function getGenerateAlterDefaultSql()
{
return array(
"ALTER TABLE test_table ALTER test_column VARCHAR(255) DEFAULT 'some_value' NOT NULL"
);
}

/**
* @group DBAL-423
*/
Expand Down
16 changes: 15 additions & 1 deletion tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ protected function getQuotedAlterTableRenameColumnSQL()
'INSERT INTO mytable (unquoted, "where", "foo", reserved_keyword, "from", "bar", quoted, "and", "baz") SELECT unquoted1, unquoted2, unquoted3, "create", "table", "select", "quoted1", "quoted2", "quoted3" FROM __temp__mytable',
'DROP TABLE __temp__mytable',
);
}
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -565,6 +565,20 @@ public function testQuotesAlterTableRenameIndexInSchema()
);
}

/**
* {@inheritdoc}
*/
protected function getGenerateAlterDefaultSql()
{
return array(
"CREATE TEMPORARY TABLE __temp__test_table AS SELECT test_column FROM test_table",
"DROP TABLE test_table",
"CREATE TABLE test_table (test_column VARCHAR(255) DEFAULT 'some_value' NOT NULL)",
"INSERT INTO test_table (test_column) SELECT test_column FROM __temp__test_table",
"DROP TABLE __temp__test_table"
);
}

/**
* @group DBAL-423
*/
Expand Down

0 comments on commit c9b6384

Please sign in to comment.