From 837cd94768dc6d3de4f974c7ca5e691a8b61c18f Mon Sep 17 00:00:00 2001
From: Adam Andrzej Jaworski <mem@tkm.cc>
Date: Thu, 16 Jul 2020 19:31:44 +0200
Subject: [PATCH] Issue #3145881 by ergonlogic: User creation has changed in
 MySQL 8.0

---
 db/Provision/Service/db/mysql.php | 44 +++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 8 deletions(-)

diff --git a/db/Provision/Service/db/mysql.php b/db/Provision/Service/db/mysql.php
index 5f1a7cbf..3084af79 100644
--- a/db/Provision/Service/db/mysql.php
+++ b/db/Provision/Service/db/mysql.php
@@ -25,7 +25,7 @@ class Provision_Service_db_mysql extends Provision_Service_db_pdo {
   }
 
   function can_create_database() {
-    $test = drush_get_option('aegir_db_prefix', 'site_') . 'test';
+    $test = drush_get_option('aegir_db_prefix', 'site_') . 'tmp_test';
     $this->create_database($test);
 
     if ($this->database_exists($test)) {
@@ -44,24 +44,52 @@ class Provision_Service_db_mysql extends Provision_Service_db_pdo {
    *   TRUE if the check was successful.
    */
   function can_grant_privileges() {
-    $dbname   = drush_get_option('aegir_db_prefix', 'site_');
+    $dbname   = drush_get_option('aegir_db_prefix', 'site_') . 'tmp_test';
+    $this->create_database($dbname);
     $user     = $dbname . '_user';
     $password = $dbname . '_password';
     $host     = $dbname . '_host';
-    if ($status = $this->grant($dbname, $user, $password, $host)) {
-      $this->revoke($dbname, $user, $host);
-    }
+    $status = $this->grant($dbname, $user, $password, $host);
+    $this->revoke($dbname, $user, $host);
+    $this->drop_database($dbname);
     return $status;
   }
 
   function grant($name, $username, $password, $host = '') {
     $host = ($host) ? $host : '%';
+
     if ($host != "127.0.0.1") {
       $extra_host = "127.0.0.1";
-      $success_extra_host = $this->query("GRANT ALL PRIVILEGES ON `%s`.* TO `%s`@`%s` IDENTIFIED BY '%s'", $name, $username, $extra_host, $password);
+      $this->grant_privileges($name, $username, $password, $extra_host);
     }
-    // Issue: https://github.com/omega8cc/provision/issues/2
-    return $this->query("GRANT ALL PRIVILEGES ON `%s`.* TO `%s`@`%s` IDENTIFIED BY '%s'", $name, $username, $host, $password);
+
+    return $this->grant_privileges($name, $username, $password, $host);
+  }
+
+  function create_user($username, $host) {
+    $statement = "CREATE USER IF NOT EXISTS `%s`@`%s`";
+    return $this->query($statement, $username, $host);
+  }
+
+  function alter_user($username, $host, $password) {
+    $statement = "ALTER USER `%s`@`%s` IDENTIFIED BY '%s'";
+    return $this->query($statement, $username, $host, $password);
+  }
+
+  function grant_privileges($name, $username, $password, $host) {
+    $user_created = $this->create_user($username, $host);
+    $user_altered = $this->alter_user($username, $host, $password);
+    if (!$user_created) {
+      drush_log(dt("Failed to create db_user @name", array('@name' => $username)), 'error');
+      return $user_created;
+    }
+    if (!$user_altered) {
+      drush_log(dt("Failed to alter db_user @name", array('@name' => $username)), 'error');
+      return $user_altered;
+    }
+
+    $statement = "GRANT ALL PRIVILEGES ON `%s`.* TO `%s`@`%s`";
+    return $this->query($statement, $name, $username, $host);
   }
 
   function revoke($name, $username, $host = '') {
-- 
2.24.3 (Apple Git-128)

