Skip to content
Permalink
Browse files

Improve support for `decimal` type in Postgres schema management

  • Loading branch information...
HavokInspiration committed Jan 1, 2017
1 parent 39319ab commit a0ce4c453c53ba9e9ee5b29e58f854fa48e516ea
Showing with 37 additions and 2 deletions.
  1. +8 −0 src/Database/Schema/PostgresSchema.php
  2. +29 −2 tests/TestCase/Database/Schema/PostgresSchemaTest.php
@@ -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)
@@ -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);
}
@@ -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
@@ -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
[
@@ -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 += [
@@ -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);
@@ -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,

0 comments on commit a0ce4c4

Please sign in to comment.
You can’t perform that action at this time.