Ignore validation errors for existing int columns with larger lengths
authorMatthias Schmidt <gravatronics@live.com>
Sun, 26 Jan 2020 11:02:20 +0000 (12:02 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 26 Jan 2020 11:02:20 +0000 (12:02 +0100)
Close #3139

wcfsetup/install/files/lib/system/database/table/column/AbstractIntDatabaseTableColumn.class.php

index f0235f3d5bc702c8b20eff63049e40155bf0faaa..fe4e9e711e827e6301fb55ae871ba391b85b74d6 100644 (file)
@@ -5,7 +5,7 @@ namespace wcf\system\database\table\column;
  * Abstract implementation of an integer database table column.
  * 
  * @author     Matthias Schmidt
- * @copyright  2001-2019 WoltLab GmbH
+ * @copyright  2001-2020 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System\Database\Table\Column
  * @since      5.2
@@ -20,4 +20,39 @@ abstract class AbstractIntDatabaseTableColumn extends AbstractDatabaseTableColum
        public function getMinimumLength() {
                return 1;
        }
+       
+       /**
+        * @inheritDoc
+        */
+       public static function createFromData($name, array $data) {
+               $length = $data['length'] ?? null;
+               
+               // Unset `length` so that `parent::createFromData()` does not validate the length
+               // which is done below.
+               $data['length'] = null;
+               
+               /** @var static $column */
+               $column = parent::createFromData($name, $data);
+               
+               if ($length) {
+                       try {
+                               $column->length($length);
+                       }
+                       catch (\InvalidArgumentException $e) {
+                               // Ignore exceptions due to the length being to large.
+                               // Such cases can happen when columns were created using the SQL PIP
+                               // where the length (which is just a display length for integer column
+                               // types) is not validated. To update tables with such columns,
+                               // exceptions related to the maximum length must be ignored.
+                               if ($length > $column->getMaximumLength()) {
+                                       $column->length = $length;
+                               }
+                               else {
+                                       throw $e;
+                               }
+                       }
+               }
+               
+               return $column;
+       }
 }