Support for backslashes in default values on PostgreSQL and SQLite

parent b83ba1a4
......@@ -384,11 +384,12 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
$length = null;
break;
case 'text':
case '_varchar':
case 'varchar':
$tableColumn['default'] = $this->fixDefaultValueQuotes($tableColumn['default']);
$fixed = false;
break;
case 'varchar':
case 'interval':
case '_varchar':
$fixed = false;
break;
case 'char':
......@@ -468,4 +469,23 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
return $defaultValue;
}
/**
* Un-escape a default value as given by PostgreSQL
*
* @param null|string $default
*
* @return null|string
*/
private function fixDefaultValueQuotes(?string $default): ?string
{
if ($default === null) {
return $default;
}
$default = str_replace("\\\\", "\\", $default);
$default = str_replace("''", "'", $default);
return $default;
}
}
......@@ -308,8 +308,9 @@ class SqliteSchemaManager extends AbstractSchemaManager
$default = null;
}
if ($default !== null) {
// SQLite returns strings wrapped in single quotes, so we need to strip them
$default = preg_replace("/^'(.*)'$/", '\1', $default);
// SQLite returns strings wrapped in single quotes and escaped, so we need to strip them
$default = preg_replace("/^'(.*)'$/s", '\1', $default);
$default = str_replace("''", "'", $default);
}
$notnull = (bool) $tableColumn['notnull'];
......
......@@ -1400,7 +1400,7 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
public function testEscapedDefaultValueMustBePreserved()
{
$value = "a\\0a\\'a\"a\na\ra\ta\\Za\\\\a" ;
$value = "a\\0b\\'c\"d\te\\Zf\\\\g";
$table = new Table('string_escaped_default_value');
$table->addColumn('def_string', 'string', array('default' => $value));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment