From f1a6b43e9c5c6face5370ff759f681a5b2391422 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 13 Jun 2019 11:50:35 -0400
Subject: [PATCH] Improve MySQL 8 compatibility. (#1381)

- Resolves VUFIND-1325.
---
 build.xml                                     |  5 ++++-
 .../VuFind/Controller/InstallController.php   |  6 +++--
 .../VuFind/Controller/Plugin/DbUpgrade.php    | 22 ++++++++++++++++++-
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/build.xml b/build.xml
index 1d3805b9d30..609d8048a06 100644
--- a/build.xml
+++ b/build.xml
@@ -284,7 +284,9 @@
         <!-- build database -->
         <exec command="mysqladmin -f -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} drop ${vufinddb}" />
         <exec command="mysqladmin -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} create ${vufinddb}" checkreturn="true" />
-        <exec command="mysql -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} -e &quot;GRANT SELECT,INSERT,UPDATE,DELETE ON ${vufinddb}.* TO '${vufinddbuser}'@'${mysqlhost}' IDENTIFIED BY '${vufinddbpass}' WITH GRANT OPTION&quot;" checkreturn="true" />
+        <exec command="mysql -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} -e &quot;DROP USER '${vufinddbuser}'@'${mysqlhost}'&quot;" />
+        <exec command="mysql -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} -e &quot;CREATE USER '${vufinddbuser}'@'${mysqlhost}' IDENTIFIED BY '${vufinddbpass}'&quot;" checkreturn="true" />
+        <exec command="mysql -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} -e &quot;GRANT SELECT,INSERT,UPDATE,DELETE ON ${vufinddb}.* TO '${vufinddbuser}'@'${mysqlhost}' WITH GRANT OPTION&quot;" checkreturn="true" />
         <exec command="mysql -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} -e &quot;FLUSH PRIVILEGES&quot;" checkreturn="true" />
         <exec command="mysql -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} -D ${vufinddb} &lt; ${srcdir}/module/VuFind/sql/mysql.sql" checkreturn="true" />
 
@@ -352,6 +354,7 @@
         <exec command="sudo su -c &quot;psql -c \&quot;DROP USER ${vufinddbuser};\&quot;&quot; ${pgsqlrootuser}" checkreturn="true" />
       </then>
       <else>
+        <exec command="mysql -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} -e &quot;DROP USER '${vufinddbuser}'@'${mysqlhost}'&quot;" />
         <exec command="mysqladmin -f -h ${mysqlhost} -u ${mysqlrootuser} ${mysqlpwswitch}${mysqlrootpass} drop ${vufinddb}" />
       </else>
     </if>
diff --git a/module/VuFind/src/VuFind/Controller/InstallController.php b/module/VuFind/src/VuFind/Controller/InstallController.php
index 2408da17897..8269e32c7f6 100644
--- a/module/VuFind/src/VuFind/Controller/InstallController.php
+++ b/module/VuFind/src/VuFind/Controller/InstallController.php
@@ -454,11 +454,13 @@ class InstallController extends AbstractBase
             return [$create, $escape, $cuser, $grant];
         }
         // Default: MySQL:
+        $user = "CREATE USER '{$view->dbuser}'@'{$view->vufindhost}'"
+            . "IDENTIFIED BY {$escapedPass}";
         $grant = "GRANT SELECT,INSERT,UPDATE,DELETE ON "
             . $view->dbname
             . ".* TO '{$view->dbuser}'@'{$view->vufindhost}' "
-            . "IDENTIFIED BY {$escapedPass} WITH GRANT OPTION";
-        return [$create, $grant, 'FLUSH PRIVILEGES'];
+            . "WITH GRANT OPTION";
+        return [$create, $user, $grant, 'FLUSH PRIVILEGES'];
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Controller/Plugin/DbUpgrade.php b/module/VuFind/src/VuFind/Controller/Plugin/DbUpgrade.php
index b00bd2737e1..34610c99911 100644
--- a/module/VuFind/src/VuFind/Controller/Plugin/DbUpgrade.php
+++ b/module/VuFind/src/VuFind/Controller/Plugin/DbUpgrade.php
@@ -627,6 +627,26 @@ class DbUpgrade extends AbstractPlugin
         return $missing;
     }
 
+    /**
+     * Normalize constraint values.
+     *
+     * @param array $constraints Constraints to normalize
+     *
+     * @return array
+     */
+    protected function normalizeConstraints($constraints)
+    {
+        foreach (['deleteRule', 'updateRule'] as $key) {
+            // NO ACTION and RESTRICT are equivalent in MySQL, but different
+            // versions return different values. Here we normalize them to RESTRICT
+            // for simplicity/consistency.
+            if ($constraints[$key] == 'NO ACTION') {
+                $constraints[$key] = 'RESTRICT';
+            }
+        }
+        return $constraints;
+    }
+
     /**
      * Compare expected vs. actual constraint actions and return an array of SQL
      * clauses required to create the modified constraints.
@@ -646,7 +666,7 @@ class DbUpgrade extends AbstractPlugin
                         "Could not find constraint '$name' in actual constraints"
                     );
                 }
-                $actualConstr = $actual[$type][$name];
+                $actualConstr = $this->normalizeConstraints($actual[$type][$name]);
                 if ($constraint['deleteRule'] !== $actualConstr['deleteRule']
                     || $constraint['updateRule'] !== $actualConstr['updateRule']
                 ) {
-- 
GitLab