Browse Source

Merge branch 'master' into 3.next

Mark Story 9 years ago
parent
commit
366fc3dee8

+ 1 - 1
.travis.yml

@@ -94,7 +94,7 @@ script:
   - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then export CODECOVERAGE=1; vendor/bin/phpunit --coverage-clover=clover.xml; fi
   - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
 
-  - if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi
+  - if [[ $PHPCS = 1 ]]; then composer cs-check; fi
   - if [[ $PHPCS = 3 ]]; then vendor/bin/phpcs -p -s --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi
   - if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:dev-master && vendor/bin/phpstan analyse -c phpstan.neon -l 0 src; fi
 

+ 4 - 3
composer.json

@@ -78,8 +78,9 @@
             "@cs-check",
             "@test"
         ],
-        "cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests",
-        "cs-fix": "phpcbf --colors --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests",
-        "test": "phpunit --colors=always"
+        "cs-check": "phpcs --colors -p ./src ./tests",
+        "cs-fix": "phpcbf --colors ./src ./tests",
+        "test": "phpunit",
+        "test-coverage": "phpunit --coverage-clover=clover.xml"
     }
 }

+ 8 - 0
src/Database/Schema/PostgresSchema.php

@@ -46,6 +46,8 @@ class PostgresSchema extends BaseSchema
             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 @@ class PostgresSchema extends BaseSchema
         if (!empty($row['has_serial'])) {
             $field['autoIncrement'] = true;
         }
+
         $field += [
             'default' => $this->_defaultValue($row['default']),
             'null' => $row['null'] === 'YES' ? true : false,
@@ -165,6 +168,11 @@ class PostgresSchema extends BaseSchema
             '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;
+        }
         $schema->addColumn($row['name'], $field);
     }
 

+ 29 - 2
tests/TestCase/Database/Schema/PostgresSchemaTest.php

@@ -76,6 +76,8 @@ published BOOLEAN DEFAULT false,
 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
@@ -140,11 +142,11 @@ SQL;
             // 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
             [
@@ -229,6 +231,8 @@ SQL;
             'default' => 'Default value',
             'comment' => 'Comment section',
             'char_length' => null,
+            'column_precision' => null,
+            'column_scale' => null,
             'collation_name' => 'ja_JP.utf8',
         ];
         $expected += [
@@ -238,6 +242,11 @@ SQL;
             '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);
 
@@ -368,6 +377,24 @@ SQL;
                 '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,