Merge pull request #8470 from kinglozzer/suf-fixing-the-suffix

FIX: Re-instate missing SS_DATABASE_SUFFIX functionality (fixes #7966)
This commit is contained in:
Robbie Averill 2018-10-12 23:11:54 +02:00 committed by GitHub
commit 516267ed46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View File

@ -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

View File

@ -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);
}