From ee21c42011fd40b2065bb2acb868a427e2232d0a Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Thu, 11 Oct 2018 10:50:56 +0100 Subject: [PATCH] FIX: Re-instate missing SS_DATABASE_SUFFIX functionality (fixes #7966) --- src/Core/CoreKernel.php | 16 ++++++++++++---- src/ORM/DatabaseAdmin.php | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Core/CoreKernel.php b/src/Core/CoreKernel.php index a1ba6c4e7..ceb3ac352 100644 --- a/src/Core/CoreKernel.php +++ b/src/Core/CoreKernel.php @@ -223,7 +223,7 @@ class CoreKernel implements Kernel // Case 1: $databaseConfig global exists. Merge $database in as needed if (!empty($databaseConfig)) { if (!empty($database)) { - $databaseConfig['database'] = $this->getDatabasePrefix() . $database; + $databaseConfig['database'] = $this->getDatabasePrefix() . $database . $this->getDatabaseSuffix(); } // Only set it if its valid, otherwise ignore $databaseConfig entirely @@ -237,7 +237,7 @@ class CoreKernel implements Kernel // Case 2: $database merged into existing config if (!empty($database)) { $existing = DB::getConfig(); - $existing['database'] = $this->getDatabasePrefix() . $database; + $existing['database'] = $this->getDatabasePrefix() . $database . $this->getDatabaseSuffix(); DB::setConfig($existing); } @@ -374,6 +374,14 @@ class CoreKernel implements Kernel return Environment::getEnv('SS_DATABASE_PREFIX') ?: ''; } + /** + * @return string + */ + protected function getDatabaseSuffix() + { + return Environment::getEnv('SS_DATABASE_SUFFIX') ?: ''; + } + /** * Get name of database * @@ -385,7 +393,7 @@ class CoreKernel implements Kernel global $database; if (!empty($database)) { - return $this->getDatabasePrefix() . $database; + return $this->getDatabasePrefix() . $database . $this->getDatabaseSuffix(); } global $databaseConfig; @@ -398,7 +406,7 @@ class CoreKernel implements Kernel $database = Environment::getEnv('SS_DATABASE_NAME'); if ($database) { - return $this->getDatabasePrefix() . $database; + return $this->getDatabasePrefix() . $database . $this->getDatabaseSuffix(); } // Auto-detect name diff --git a/src/ORM/DatabaseAdmin.php b/src/ORM/DatabaseAdmin.php index 2898a72e8..0fcbf8681 100644 --- a/src/ORM/DatabaseAdmin.php +++ b/src/ORM/DatabaseAdmin.php @@ -11,6 +11,7 @@ use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Manifest\ClassLoader; use SilverStripe\Dev\DevelopmentAdmin; use SilverStripe\Dev\TestOnly; +use SilverStripe\ORM\Connect\DatabaseException; use SilverStripe\ORM\FieldType\DBClassName; use SilverStripe\Security\Permission; use SilverStripe\Security\Security; @@ -261,9 +262,25 @@ class DatabaseAdmin extends Controller } $database = $parameters['database']; - // Establish connection and create database in two steps + // Establish connection unset($parameters['database']); DB::connect($parameters); + + // Check to ensure that the re-instated SS_DATABASE_SUFFIX functionality won't unexpectedly + // rename the database. To be removed for SS5 + if ($suffix = Environment::getEnv('SS_DATABASE_SUFFIX')) { + $previousName = preg_replace("/{$suffix}$/", '', $database); + + if (!isset($_GET['force_suffix_rename']) && DB::get_conn()->databaseExists($previousName)) { + throw new DatabaseException( + "SS_DATABASE_SUFFIX was previously broken, but has now been fixed. This will result in your " + . "database being named \"{$database}\" instead of \"{$previousName}\" from now on. If this " + . "change is intentional, please visit dev/build?force_suffix_rename=1 to continue" + ); + } + } + + // Create database DB::create_database($database); }