Support empty SS_DATABASE_PREFIX

This commit is contained in:
Will Rossiter 2017-06-27 10:42:50 +12:00
parent cf758ddd4f
commit ecb5d85de0

View File

@ -209,17 +209,16 @@ class CoreKernel implements Kernel
global $databaseConfig; global $databaseConfig;
global $database; global $database;
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'SS_';
// Case 1: $databaseConfig global exists. Merge $database in as needed // Case 1: $databaseConfig global exists. Merge $database in as needed
if (!empty($databaseConfig)) { if (!empty($databaseConfig)) {
if (!empty($database)) { if (!empty($database)) {
$databaseConfig['database'] = $prefix . $database; $databaseConfig['database'] = $this->getDatabasePrefix() . $database;
} }
// Only set it if its valid, otherwise ignore $databaseConfig entirely // Only set it if its valid, otherwise ignore $databaseConfig entirely
if (!empty($databaseConfig['database'])) { if (!empty($databaseConfig['database'])) {
DB::setConfig($databaseConfig); DB::setConfig($databaseConfig);
return; return;
} }
} }
@ -227,7 +226,8 @@ class CoreKernel implements Kernel
// Case 2: $database merged into existing config // Case 2: $database merged into existing config
if (!empty($database)) { if (!empty($database)) {
$existing = DB::getConfig(); $existing = DB::getConfig();
$existing['database'] = $prefix . $database; $existing['database'] = $this->getDatabasePrefix() . $database;
DB::setConfig($existing); DB::setConfig($existing);
} }
} }
@ -356,6 +356,14 @@ class CoreKernel implements Kernel
return $databaseConfig; return $databaseConfig;
} }
/**
* @return string
*/
protected function getDatabasePrefix()
{
return getenv('SS_DATABASE_PREFIX');
}
/** /**
* Get name of database * Get name of database
* *
@ -363,26 +371,29 @@ class CoreKernel implements Kernel
*/ */
protected function getDatabaseName() protected function getDatabaseName()
{ {
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'SS_';
// Check globals // Check globals
global $database; global $database;
if (!empty($database)) { if (!empty($database)) {
return $prefix.$database; return $this->getDatabasePrefix() . $database;
} }
global $databaseConfig; global $databaseConfig;
if (!empty($databaseConfig['database'])) { if (!empty($databaseConfig['database'])) {
return $databaseConfig['database']; // Note: Already includes prefix return $databaseConfig['database']; // Note: Already includes prefix
} }
// Check environment // Check environment
$database = getenv('SS_DATABASE_NAME'); $database = getenv('SS_DATABASE_NAME');
if ($database) { if ($database) {
return $prefix.$database; return $this->getDatabasePrefix() . $database;
} }
// Auto-detect name // Auto-detect name
$chooseName = getenv('SS_DATABASE_CHOOSE_NAME'); $chooseName = getenv('SS_DATABASE_CHOOSE_NAME');
if ($chooseName) { if ($chooseName) {
// Find directory to build name from // Find directory to build name from
$loopCount = (int)$chooseName; $loopCount = (int)$chooseName;
@ -393,7 +404,13 @@ class CoreKernel implements Kernel
// Build name // Build name
$database = str_replace('.', '', basename($databaseDir)); $database = str_replace('.', '', basename($databaseDir));
return $prefix.$database; $prefix = $this->getDatabasePrefix();
if ($prefix === false) {
$prefix = 'SS_';
}
return $prefix . $database;
} }
// no DB name (may be optional for some connectors) // no DB name (may be optional for some connectors)