Skip to content

Commit

Permalink
Fixes UX on bad SQL credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Sep 13, 2021
1 parent 05d1e13 commit 83f6777
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
30 changes: 16 additions & 14 deletions modules/system/console/OctoberInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ protected function setupEncryptionKey()
*/
protected function setupDatabaseConfig()
{
$typeMap = [
'SQLite' => 'sqlite',
'MySQL' => 'mysql',
'Postgres' => 'pgsql',
'SQL Server' => 'sqlsrv',
];

$currentDriver = array_flip($typeMap)[$this->getEnvVar('DB_CONNECTION')] ?? 'SQLite';

$type = $this->choice(
// Database Engine
Lang::get('system::lang.installer.database_engine_label'),
Expand All @@ -186,16 +195,9 @@ protected function setupDatabaseConfig()
'Postgres',
'SQL Server'
],
'SQLite'
$currentDriver
);

$typeMap = [
'SQLite' => 'sqlite',
'MySQL' => 'mysql',
'Postgres' => 'pgsql',
'SQL Server' => 'sqlsrv',
];

$driver = $typeMap[$type] ?? 'sqlite';

$this->setEnvVar('DB_CONNECTION', $driver);
Expand Down Expand Up @@ -244,7 +246,7 @@ protected function setupDatabase($driver)
$host = $this->ask(
// Database Host
Lang::get('system::lang.installer.database_host_label'),
env('DB_HOST', 'localhost')
$this->getEnvVar('DB_HOST', 'localhost')
);
$this->setEnvVar('DB_HOST', $host);
Config::set("database.connections.{$driver}.host", $host);
Expand All @@ -254,7 +256,7 @@ protected function setupDatabase($driver)
$port = $this->ask(
// Database Port
Lang::get('system::lang.installer.database_port_label'),
env('DB_PORT', false)
$this->getEnvVar('DB_PORT', false)
) ?: '';
$this->setEnvVar('DB_PORT', $port);
Config::set("database.connections.{$driver}.port", $port);
Expand All @@ -264,7 +266,7 @@ protected function setupDatabase($driver)
$database = $this->ask(
// Database Name
Lang::get('system::lang.installer.database_name_label'),
env('DB_DATABASE', 'octobercms')
$this->getEnvVar('DB_DATABASE', 'octobercms')
);
$this->setEnvVar('DB_DATABASE', $database);
Config::set("database.connections.{$driver}.database", $database);
Expand All @@ -274,7 +276,7 @@ protected function setupDatabase($driver)
$username = $this->ask(
// Database Login
Lang::get('system::lang.installer.database_login_label'),
env('DB_USERNAME', 'root')
$this->getEnvVar('DB_USERNAME', 'root')
);
$this->setEnvVar('DB_USERNAME', $username);
Config::set("database.connections.{$driver}.username", $username);
Expand All @@ -284,7 +286,7 @@ protected function setupDatabase($driver)
$password = $this->ask(
// Database Password
Lang::get('system::lang.installer.database_pass_label'),
env('DB_PASSWORD', false)
$this->getEnvVar('DB_PASSWORD', false)
) ?: '';
$this->setEnvVar('DB_PASSWORD', $password);
Config::set("database.connections.{$driver}.password", $password);
Expand All @@ -298,7 +300,7 @@ protected function setupDatabaseSqlite()
// For file-based storage, enter a path relative to the application root directory.
$this->comment(Lang::get('system::lang.installer.database_path_comment'));

$defaultDb = env('DB_DATABASE', 'storage/database.sqlite');
$defaultDb = $this->getEnvVar('DB_DATABASE', 'storage/database.sqlite');
if ($defaultDb === 'database') {
$defaultDb = 'storage/database.sqlite';
}
Expand Down
6 changes: 3 additions & 3 deletions modules/system/traits/SetupHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function setEnvVars(array $vars)
protected function setEnvVar($key, $value)
{
$path = base_path('.env');
$old = env($key);
$old = $this->getEnvVar($key);

if (is_bool(env($key))) {
$old = env($key) ? 'true' : 'false';
Expand All @@ -123,9 +123,9 @@ protected function setEnvVar($key, $value)
* getEnvVar specifically from installer specified values. This is needed since
* the writing to the environment file may not update the values from env()
*/
protected function getEnvVar(string $key): string
protected function getEnvVar(string $key, $default = null): string
{
return $this->userConfig[$key] ?? env($key);
return $this->userConfig[$key] ?? env($key, $default);
}

/**
Expand Down

0 comments on commit 83f6777

Please sign in to comment.