From e810a9928c2d0771ba8f44934ed6295bbed418b3 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 20 Jun 2016 17:40:58 +1200 Subject: [PATCH] API Add optimistic_connect to SS_Database Fixes #4765 --- model/connect/Database.php | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/model/connect/Database.php b/model/connect/Database.php index cfdd6281e..45cc36985 100644 --- a/model/connect/Database.php +++ b/model/connect/Database.php @@ -16,6 +16,15 @@ abstract class SS_Database { */ protected $connector = null; + /** + * In cases where your environment does not have 'SHOW DATABASES' permission, + * you can set this to true. Then selectDatabase() will always connect without + * doing databaseExists() check. + * + * @var bool + */ + private static $optimistic_connect = false; + /** * Get the current connector * @@ -630,18 +639,23 @@ abstract class SS_Database { * @return boolean Flag indicating success */ public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR) { - if (!$this->schemaManager->databaseExists($name)) { - // Check DB creation permisson - if (!$create) { - if ($errorLevel !== false) { - user_error("Attempted to connect to non-existing database \"$name\"", $errorLevel); - } - // Unselect database - $this->connector->unloadDatabase(); - return false; - } - $this->schemaManager->createDatabase($name); + // In case our live environment is locked down, we can bypass a SHOW DATABASE check + $canConnect = Config::inst()->get(get_class($this), 'optimistic_connect') + || $this->schemaManager->databaseExists($name); + if($canConnect) { + return $this->connector->selectDatabase($name); } + + // Check DB creation permisson + if (!$create) { + if ($errorLevel !== false) { + user_error("Attempted to connect to non-existing database \"$name\"", $errorLevel); + } + // Unselect database + $this->connector->unloadDatabase(); + return false; + } + $this->schemaManager->createDatabase($name); return $this->connector->selectDatabase($name); }