diff --git a/src/Database/Schema/PostgresSchema.php b/src/Database/Schema/PostgresSchema.php index 4a0c8325403..55ee8d0e8bd 100644 --- a/src/Database/Schema/PostgresSchema.php +++ b/src/Database/Schema/PostgresSchema.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,6 +160,7 @@ 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, @@ -165,6 +168,11 @@ public function convertColumnDescription(Table $table, $row) '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); } diff --git a/tests/TestCase/Database/Schema/PostgresSchemaTest.php b/tests/TestCase/Database/Schema/PostgresSchemaTest.php index f7ad569657c..61267adb195 100644 --- a/tests/TestCase/Database/Schema/PostgresSchemaTest.php +++ b/tests/TestCase/Database/Schema/PostgresSchemaTest.php @@ -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,