Skip to content

Commit

Permalink
Improve support for decimal type in Postgres schema management
Browse files Browse the repository at this point in the history
  • Loading branch information
HavokInspiration committed Jan 1, 2017
1 parent 39319ab commit a0ce4c4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Database/Schema/PostgresSchema.php
Expand Up @@ -46,6 +46,8 @@ public function describeColumnSql($tableName, $config)
c.collation_name,
d.description as comment,
ordinal_position,
c.numeric_precision as column_precision,
c.numeric_scale as column_scale,
pg_get_serial_sequence(attr.attrelid::regclass::text, attr.attname) IS NOT NULL AS has_serial
FROM information_schema.columns c
INNER JOIN pg_catalog.pg_namespace ns ON (ns.nspname = table_schema)
Expand Down Expand Up @@ -158,13 +160,19 @@ public function convertColumnDescription(Table $table, $row)
if (!empty($row['has_serial'])) {
$field['autoIncrement'] = true;
}

$field += [
'default' => $this->_defaultValue($row['default']),
'null' => $row['null'] === 'YES' ? true : false,
'collate' => $row['collation_name'],
'comment' => $row['comment']
];
$field['length'] = $row['char_length'] ?: $field['length'];

if ($field['type'] === 'numeric' || $field['type'] === 'decimal') {
$field['length'] = $row['column_precision'];
$field['precision'] = $row['column_scale'] ? $row['column_scale'] : null;
}
$table->addColumn($row['name'], $field);
}

Expand Down
31 changes: 29 additions & 2 deletions tests/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -75,6 +75,8 @@ protected function _createTables($connection)
views SMALLINT DEFAULT 0,
readingtime TIME,
data JSONB,
average_note DECIMAL(4,2),
average_income NUMERIC(10,2),
created TIMESTAMP,
CONSTRAINT "content_idx" UNIQUE ("title", "body"),
CONSTRAINT "author_idx" FOREIGN KEY ("author_id") REFERENCES "schema_authors" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
Expand Down Expand Up @@ -139,11 +141,11 @@ public static function convertColumnProvider()
// Decimal
[
'NUMERIC',
['type' => 'decimal', 'length' => null]
['type' => 'decimal', 'length' => null, 'precision' => null]
],
[
'DECIMAL(10,2)',
['type' => 'decimal', 'length' => null]
['type' => 'decimal', 'length' => 10, 'precision' => 2]
],
// String
[
Expand Down Expand Up @@ -228,6 +230,8 @@ public function testConvertColumn($type, $expected)
'default' => 'Default value',
'comment' => 'Comment section',
'char_length' => null,
'column_precision' => null,
'column_scale' => null,
'collation_name' => 'ja_JP.utf8',
];
$expected += [
Expand All @@ -237,6 +241,11 @@ public function testConvertColumn($type, $expected)
'collate' => 'ja_JP.utf8',
];

if ($field['type'] === 'NUMERIC' || strpos($field['type'], 'DECIMAL') !== false) {
$field['column_precision'] = $expected['length'];
$field['column_scale'] = $expected['precision'];
}

$driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')->getMock();
$dialect = new PostgresSchema($driver);

Expand Down Expand Up @@ -367,6 +376,24 @@ public function testDescribeTable()
'precision' => null,
'comment' => null,
],
'average_note' => [
'type' => 'decimal',
'null' => true,
'default' => null,
'length' => 4,
'precision' => 2,
'unsigned' => null,
'comment' => null,
],
'average_income' => [
'type' => 'decimal',
'null' => true,
'default' => null,
'length' => 10,
'precision' => 2,
'unsigned' => null,
'comment' => null,
],
'created' => [
'type' => 'timestamp',
'null' => true,
Expand Down

0 comments on commit a0ce4c4

Please sign in to comment.