ソースを参照

Fix non-set precision bug in MysqlSchema::columnSql

Fix #13698
bancer 6 年 前
コミット
37fbab8aa7

+ 1 - 0
.gitignore

@@ -7,6 +7,7 @@
 /phpunit.xml
 /phpcs.xml
 /vendor
+/vendors
 /composer.phar
 *.mo
 debug.log

+ 8 - 4
src/Database/Schema/MysqlSchema.php

@@ -389,10 +389,14 @@ class MysqlSchema extends BaseSchema
         }
 
         $hasPrecision = [TableSchema::TYPE_FLOAT, TableSchema::TYPE_DECIMAL];
-        if (in_array($data['type'], $hasPrecision, true) &&
-            (isset($data['length']) || isset($data['precision']))
-        ) {
-            $out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
+        if (in_array($data['type'], $hasPrecision, true)) {
+            if (isset($data['length'])) {
+                if (isset($data['precision'])) {
+                    $out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
+                } else {
+                    $out .= '(' . (int)$data['length'] . ')';
+                }
+            }
         }
 
         $hasUnsigned = [

+ 23 - 1
tests/TestCase/Database/Schema/MysqlSchemaTest.php

@@ -670,7 +670,7 @@ SQL;
             [
                 'value',
                 ['type' => 'decimal', 'length' => 11, 'unsigned' => true],
-                '`value` DECIMAL(11,0) UNSIGNED'
+                '`value` DECIMAL(11) UNSIGNED'
             ],
             [
                 'value',
@@ -781,6 +781,28 @@ SQL;
     }
 
     /**
+     * Test generating column definitions
+     *
+     * @return void
+     */
+    public function testColumnSqlFloat()
+    {
+        $expected = '`latitude` FLOAT(53) UNSIGNED';
+        $driver = $this->_getMockedDriver();
+        $schema = new MysqlSchema($driver);
+        $name = 'latitude';
+        $data = [
+            'type' => 'float',
+            'length' => 53,
+            'null' => true,
+            'default' => null,
+            'unsigned' => true,
+        ];
+        $table = (new TableSchema('articles'))->addColumn($name, $data);
+        $this->assertEquals($expected, $schema->columnSql($table, $name));
+    }
+
+    /**
      * Provide data for testing constraintSql
      *
      * @return array