From d742576a0c20966470b5dc4616c22a3838d76faf Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 21 Nov 2018 18:13:38 +1300 Subject: [PATCH] FIX Implement method to update values that have been invalidated by a new enum definition --- code/PostgreSQLSchemaManager.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/code/PostgreSQLSchemaManager.php b/code/PostgreSQLSchemaManager.php index 37f3563..a8ea31f 100644 --- a/code/PostgreSQLSchemaManager.php +++ b/code/PostgreSQLSchemaManager.php @@ -4,6 +4,7 @@ namespace SilverStripe\PostgreSQL; use SilverStripe\Dev\Deprecation; use SilverStripe\ORM\Connect\DBSchemaManager; +use SilverStripe\ORM\Connect\Query; use SilverStripe\ORM\DB; /** @@ -1471,4 +1472,25 @@ class PostgreSQLSchemaManager extends DBSchemaManager user_error("PostGreSQL does not support multi-enum", E_USER_ERROR); return "int"; } + + /** + * Update existing enum value that have been invalidated + * @param string $specValue Updated specification for the column. + * @param string $spec_orig Original specification for the column. + * @param string $fieldValue Current specification for the column. + * @param string $table Name of the table. + * @param string $field Name of the field. + * @return Query|null + */ + protected function resetInvalidatedEnumValue($specValue, $spec_orig, $fieldValue, $table, $field) + { + $enumRegex = "/^varchar\(255\) default (.+?) check \(\"$field\" in \((.*?), null\)\)$/"; + + if (preg_match($enumRegex, $specValue, $matches)) { + $default = $matches[1]; + $inValues = $matches[2]; + $query = "UPDATE \"$table\" SET \"$field\"=$default WHERE NOT \"$field\" IN ($inValues) AND \"$field\" IS NOT NULL"; + return $this->query($query); + } + } }