diff --git a/db/Provision/Service/db/mysql.php b/db/Provision/Service/db/mysql.php index 5f1a7cbf..0ed6c456 100644 --- a/db/Provision/Service/db/mysql.php +++ b/db/Provision/Service/db/mysql.php @@ -44,24 +44,43 @@ 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_') . '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, '127.0.0.1'); } - // 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, $password) { + $statement = "CREATE USER IF NOT EXISTS `%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, $password); + if (!$user_created) { + drush_log('Failed to create database user.', 'error'); + return $user_created; + } + + $statement = "GRANT ALL PRIVILEGES ON `%s`.* TO `%s`@`%s`"; + return $this->query($statement, $name, $username, $host); } function revoke($name, $username, $host = '') { diff --git a/db/Provision/Service/db/pdo.php b/db/Provision/Service/db/pdo.php index 023feac6..455872d8 100644 --- a/db/Provision/Service/db/pdo.php +++ b/db/Provision/Service/db/pdo.php @@ -54,10 +54,15 @@ class Provision_Service_db_pdo extends Provision_Service_db { catch (PDOException $e) { drush_log($e->getMessage(), 'warning'); return FALSE; - } + } - return $result; + if (!$result) { + $error = []; + list($error['@sql_error'], $error['@driver_error'], $error['@error_message']) = $this->conn->errorInfo(); + drush_log(dt('Database query returned: @sql_error[@driver_error]: @error_message', $error), 'notice'); + } + return $result; } function query_callback($match, $init = FALSE) {