Commit d8442038 authored by Marco Pivetta's avatar Marco Pivetta Committed by GitHub

Merge pull request #2614 from deeky666/DBAL-2427

[DBAL-2427] Fix negative default value introspection on PostgreSQL 9.4
parents 3f8c7467 5eb1a93b
...@@ -316,7 +316,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -316,7 +316,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
$autoincrement = true; $autoincrement = true;
} }
if (preg_match("/^'(.*)'::.*$/", $tableColumn['default'], $matches)) { if (preg_match("/^['(](.*)[')]::.*$/", $tableColumn['default'], $matches)) {
$tableColumn['default'] = $matches[1]; $tableColumn['default'] = $matches[1];
} }
...@@ -354,15 +354,18 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -354,15 +354,18 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
switch ($dbType) { switch ($dbType) {
case 'smallint': case 'smallint':
case 'int2': case 'int2':
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
$length = null; $length = null;
break; break;
case 'int': case 'int':
case 'int4': case 'int4':
case 'integer': case 'integer':
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
$length = null; $length = null;
break; break;
case 'bigint': case 'bigint':
case 'int8': case 'int8':
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
$length = null; $length = null;
break; break;
case 'bool': case 'bool':
...@@ -398,6 +401,8 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -398,6 +401,8 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
case 'decimal': case 'decimal':
case 'money': case 'money':
case 'numeric': case 'numeric':
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) { if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['complete_type'], $match)) {
$precision = $match[1]; $precision = $match[1];
$scale = $match[2]; $scale = $match[2];
...@@ -445,4 +450,20 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -445,4 +450,20 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
return $column; return $column;
} }
/**
* PostgreSQL 9.4 puts parentheses around negative numeric default values that need to be stripped eventually.
*
* @param mixed $defaultValue
*
* @return mixed
*/
private function fixVersion94NegativeNumericDefaultValue($defaultValue)
{
if (strpos($defaultValue, '(') === 0) {
return trim($defaultValue, '()');
}
return $defaultValue;
}
} }
...@@ -383,6 +383,31 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -383,6 +383,31 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertEquals('json_array', $columns['foo']->getType()->getName()); $this->assertEquals('json_array', $columns['foo']->getType()->getName());
$this->assertEquals(true, $columns['foo']->getPlatformOption('jsonb')); $this->assertEquals(true, $columns['foo']->getPlatformOption('jsonb'));
} }
/**
* @group DBAL-2427
*/
public function testListNegativeColumnDefaultValue()
{
$table = new Schema\Table('test_default_negative');
$table->addColumn('col_smallint', 'smallint', array('default' => -1));
$table->addColumn('col_integer', 'integer', array('default' => -1));
$table->addColumn('col_bigint', 'bigint', array('default' => -1));
$table->addColumn('col_float', 'float', array('default' => -1.1));
$table->addColumn('col_decimal', 'decimal', array('default' => -1.1));
$table->addColumn('col_string', 'string', array('default' => '(-1)'));
$this->_sm->dropAndCreateTable($table);
$columns = $this->_sm->listTableColumns('test_default_negative');
$this->assertEquals(-1, $columns['col_smallint']->getDefault());
$this->assertEquals(-1, $columns['col_integer']->getDefault());
$this->assertEquals(-1, $columns['col_bigint']->getDefault());
$this->assertEquals(-1.1, $columns['col_float']->getDefault());
$this->assertEquals(-1.1, $columns['col_decimal']->getDefault());
$this->assertEquals('(-1)', $columns['col_string']->getDefault());
}
} }
class MoneyType extends Type class MoneyType extends Type
......
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