Skip to content

Commit

Permalink
Automatic escaping of default values
Browse files Browse the repository at this point in the history
It is now possible to use `\` in non-escaped default values for a column,
and it will be automatically escaped for platforms that need it.

Previously this lead to a confusion when diffing actual and expected
schema leading to perpetual out of sync schema.

Before:

```php
/**
 * @Orm\Column(options={"default" = "Foo\\Bar"}))
 */
private $name;

```

After:

```php
/**
 * @Orm\Column(options={"default" = "Foo\Bar"}))
 */
private $name;

```

And the result in database will be a default value exactly as written in
the annotation, that is `Foo\Bar`.
  • Loading branch information
PowerKiKi committed Sep 22, 2017
1 parent a0cc581 commit 44e1e17
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -2277,7 +2277,7 @@ public function getDefaultValueDeclarationSQL($field)
$default = empty($field['notnull']) ? ' DEFAULT NULL' : '';

if (isset($field['default'])) {
$default = " DEFAULT '".$field['default']."'";
$default = " DEFAULT ".$this->quoteStringLiteral($field['default']);
if (isset($field['type'])) {
$type = $field['type'];
if ($type instanceof Types\PhpIntegerMappingType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,4 +907,15 @@ public function testListTableForeignKeysSQLEvaluatesDatabase()
self::assertContains('bar', $sql);
self::assertNotContains('DATABASE()', $sql);
}

public function testGetDefaultValueDeclarationSQLEscaped()
{
// string must be escaped
$field = array(
'type' => 'string',
'default' => 'Foo\\Bar'
);

self::assertEquals(" DEFAULT 'Foo\\\\Bar'", $this->_platform->getDefaultValueDeclarationSQL($field));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -919,4 +919,15 @@ public function testQuotesDatabaseNameInCloseActiveDatabaseConnectionsSQL()
true
);
}

public function testGetDefaultValueDeclarationSQLEscaped()
{
// string must be escaped
$field = array(
'type' => 'string',
'default' => 'Foo\\Bar'
);

self::assertEquals(" DEFAULT 'Foo\\\\Bar'", $this->_platform->getDefaultValueDeclarationSQL($field));
}
}
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,4 +874,15 @@ public function testQuotesDatabaseNameInListTableColumnsSQL()
{
self::assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\"), '', true);
}

public function testGetDefaultValueDeclarationSQLEscaped()
{
// string must be escaped
$field = array(
'type' => 'string',
'default' => 'Foo\\Bar'
);

self::assertEquals(" DEFAULT 'Foo\\\\Bar'", $this->_platform->getDefaultValueDeclarationSQL($field));
}
}

0 comments on commit 44e1e17

Please sign in to comment.