Browse Source

Improve SQLite handling of decimal columns to retain precision and length

Backport #13615 to master
Mark Story 6 years ago
parent
commit
9183d183de

+ 9 - 4
src/Database/Schema/SqliteSchema.php

@@ -61,9 +61,14 @@ class SqliteSchema extends BaseSchema
         }
 
         $col = strtolower($matches[2]);
-        $length = null;
+        $length = $precision = null;
         if (isset($matches[3])) {
-            $length = (int)$matches[3];
+            $length = $matches[3];
+            if (strpos($length, ',') !== false) {
+                [$length, $precision] = explode(',', $length);
+            }
+            $length = (int)$length;
+            $precision = (int)$precision;
         }
 
         if ($col === 'bigint') {
@@ -79,10 +84,10 @@ class SqliteSchema extends BaseSchema
             return ['type' => TableSchema::TYPE_INTEGER, 'length' => $length, 'unsigned' => $unsigned];
         }
         if (strpos($col, 'decimal') !== false) {
-            return ['type' => TableSchema::TYPE_DECIMAL, 'length' => null, 'unsigned' => $unsigned];
+            return ['type' => TableSchema::TYPE_DECIMAL, 'length' => $length, 'precision' => $precision, 'unsigned' => $unsigned];
         }
         if (in_array($col, ['float', 'real', 'double'])) {
-            return ['type' => TableSchema::TYPE_FLOAT, 'length' => null, 'unsigned' => $unsigned];
+            return ['type' => TableSchema::TYPE_FLOAT, 'length' => $length, 'precision' => $precision, 'unsigned' => $unsigned];
         }
 
         if (strpos($col, 'boolean') !== false) {

+ 6 - 6
tests/TestCase/Database/Schema/SqliteSchemaTest.php

@@ -125,27 +125,27 @@ class SqliteSchemaTest extends TestCase
             ],
             [
                 'FLOAT',
-                ['type' => 'float', 'length' => null, 'unsigned' => false]
+                ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false]
             ],
             [
                 'DOUBLE',
-                ['type' => 'float', 'length' => null, 'unsigned' => false]
+                ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false]
             ],
             [
                 'UNSIGNED DOUBLE',
-                ['type' => 'float', 'length' => null, 'unsigned' => true]
+                ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => true]
             ],
             [
                 'REAL',
-                ['type' => 'float', 'length' => null, 'unsigned' => false]
+                ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false]
             ],
             [
                 'DECIMAL(11,2)',
-                ['type' => 'decimal', 'length' => null, 'unsigned' => false]
+                ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => false]
             ],
             [
                 'UNSIGNED DECIMAL(11,2)',
-                ['type' => 'decimal', 'length' => null, 'unsigned' => true]
+                ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => true]
             ],
         ];
     }