Skip to content

Commit

Permalink
Partially remove DynamicDatabaseConnection and define all database de…
Browse files Browse the repository at this point in the history
…faults in enum class
  • Loading branch information
PseudoResonance committed Jan 10, 2025
1 parent d22244c commit 754a28d
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 457 deletions.
266 changes: 72 additions & 194 deletions app/Console/Commands/Environment/DatabaseSettingsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@

namespace App\Console\Commands\Environment;

use App\Enums\DatabaseDriver;
use App\Traits\EnvironmentWriterTrait;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\DB;

class DatabaseSettingsCommand extends Command
{
use EnvironmentWriterTrait;

public const DATABASE_DRIVERS = [
'sqlite' => 'SQLite (recommended)',
'mariadb' => 'MariaDB',
'mysql' => 'MySQL',
'pgsql' => 'PostgreSQL',
];

protected $description = 'Configure database settings for the Panel.';

protected $signature = 'p:environment:database
{--driver= : The database driver backend to use.}
{--database= : The database to use.}
{--host= : The connection address for the MySQL/ MariaDB server.}
{--port= : The connection port for the MySQL/ MariaDB server.}
{--username= : Username to use when connecting to the MySQL/ MariaDB server.}
{--password= : Password to use for the MySQL/ MariaDB database.}';
{--host= : The connection address for the database server.}
{--port= : The connection port for the database server.}
{--username= : Username to use when connecting to the database server.}
{--password= : Password to use for the database server.}';

protected array $variables = [];

Expand All @@ -50,198 +45,81 @@ public function handle(): int
return 1;
}

$driverList = DatabaseDriver::getFriendlyNameArray(DatabaseDriver::Sqlite);
$selected = config('database.default', 'sqlite');
$this->variables['DB_CONNECTION'] = $this->option('driver') ?? $this->choice(
'Database Driver',
self::DATABASE_DRIVERS,
array_key_exists($selected, self::DATABASE_DRIVERS) ? $selected : null
$driverList,
array_key_exists($selected, $driverList) ? $selected : null
);

if ($this->variables['DB_CONNECTION'] === 'mysql') {
$this->output->note(__('commands.database_settings.DB_HOST_note'));
$this->variables['DB_HOST'] = $this->option('host') ?? $this->ask(
'Database Host',
config('database.connections.mysql.host', '127.0.0.1')
);

$this->variables['DB_PORT'] = $this->option('port') ?? $this->ask(
'Database Port',
config('database.connections.mysql.port', 3306)
);

$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(
'Database Name',
config('database.connections.mysql.database', 'panel')
);

$this->output->note(__('commands.database_settings.DB_USERNAME_note'));
$this->variables['DB_USERNAME'] = $this->option('username') ?? $this->ask(
'Database Username',
config('database.connections.mysql.username', 'pelican')
);

$askForMySQLPassword = true;
if (!empty(config('database.connections.mysql.password')) && $this->input->isInteractive()) {
$this->variables['DB_PASSWORD'] = config('database.connections.mysql.password');
$askForMySQLPassword = $this->confirm(__('commands.database_settings.DB_PASSWORD_note'));
}

if ($askForMySQLPassword) {
$this->variables['DB_PASSWORD'] = $this->option('password') ?? $this->secret('Database Password');
}

try {
// Test connection
config()->set('database.connections._panel_command_test', [
'driver' => 'mysql',
'host' => $this->variables['DB_HOST'],
'port' => $this->variables['DB_PORT'],
'database' => $this->variables['DB_DATABASE'],
'username' => $this->variables['DB_USERNAME'],
'password' => $this->variables['DB_PASSWORD'],
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'strict' => true,
]);

$this->database->connection('_panel_command_test')->getPdo();
} catch (\PDOException $exception) {
$this->output->error(sprintf('Unable to connect to the MySQL server using the provided credentials. The error returned was "%s".', $exception->getMessage()));
$this->output->error(__('commands.database_settings.DB_error_2'));

if ($this->confirm(__('commands.database_settings.go_back'))) {
$this->database->disconnect('_panel_command_test');

return $this->handle();
$driver = DatabaseDriver::from($this->variables['DB_CONNECTION']);

switch ($driver) {
case DatabaseDriver::Sqlite:
$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(
'Database Path',
env('DB_DATABASE', 'database.sqlite')
);
break;
case DatabaseDriver::Mariadb:
case DatabaseDriver::Mysql:
case DatabaseDriver::Postgresql:
$this->output->note(__('commands.database_settings.DB_HOST_note'));
$this->variables['DB_HOST'] = $this->option('host') ?? $this->ask(
'Database Host',
$driver->getDefaultOption('host', true)
);

$this->variables['DB_PORT'] = $this->option('port') ?? $this->ask(
'Database Port',
$driver->getDefaultOption('port', true)
);

$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(
'Database Name',
$driver->getDefaultOption('database', true)
);

$this->output->note(__('commands.database_settings.DB_USERNAME_note'));
$this->variables['DB_USERNAME'] = $this->option('username') ?? $this->ask(
'Database Username',
$driver->getDefaultOption('username', true)
);

$askForPassword = true;
if (!empty($driver->getDefaultOption('password', true)) && $this->input->isInteractive()) {
$this->variables['DB_PASSWORD'] = $driver->getDefaultOption('password', true);
$askForPassword = $this->confirm(__('commands.database_settings.DB_PASSWORD_note'));
}

return 1;
}
} elseif ($this->variables['DB_CONNECTION'] === 'mariadb') {
$this->output->note(__('commands.database_settings.DB_HOST_note'));
$this->variables['DB_HOST'] = $this->option('host') ?? $this->ask(
'Database Host',
config('database.connections.mariadb.host', '127.0.0.1')
);

$this->variables['DB_PORT'] = $this->option('port') ?? $this->ask(
'Database Port',
config('database.connections.mariadb.port', 3306)
);

$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(
'Database Name',
config('database.connections.mariadb.database', 'panel')
);

$this->output->note(__('commands.database_settings.DB_USERNAME_note'));
$this->variables['DB_USERNAME'] = $this->option('username') ?? $this->ask(
'Database Username',
config('database.connections.mariadb.username', 'pelican')
);

$askForMariaDBPassword = true;
if (!empty(config('database.connections.mariadb.password')) && $this->input->isInteractive()) {
$this->variables['DB_PASSWORD'] = config('database.connections.mariadb.password');
$askForMariaDBPassword = $this->confirm(__('commands.database_settings.DB_PASSWORD_note'));
}

if ($askForMariaDBPassword) {
$this->variables['DB_PASSWORD'] = $this->option('password') ?? $this->secret('Database Password');
}

try {
// Test connection
config()->set('database.connections._panel_command_test', [
'driver' => 'mariadb',
'host' => $this->variables['DB_HOST'],
'port' => $this->variables['DB_PORT'],
'database' => $this->variables['DB_DATABASE'],
'username' => $this->variables['DB_USERNAME'],
'password' => $this->variables['DB_PASSWORD'],
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'strict' => true,
]);

$this->database->connection('_panel_command_test')->getPdo();
} catch (\PDOException $exception) {
$this->output->error(sprintf('Unable to connect to the MariaDB server using the provided credentials. The error returned was "%s".', $exception->getMessage()));
$this->output->error(__('commands.database_settings.DB_error_2'));

if ($this->confirm(__('commands.database_settings.go_back'))) {
$this->database->disconnect('_panel_command_test');

return $this->handle();
if ($askForPassword) {
$this->variables['DB_PASSWORD'] = $this->option('password') ?? $this->secret('Database Password');
}

return 1;
}
} elseif ($this->variables['DB_CONNECTION'] === 'sqlite') {
$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(
'Database Path',
env('DB_DATABASE', 'database.sqlite')
);
} elseif ($this->variables['DB_CONNECTION'] === 'pgsql') {
$this->output->note(__('commands.database_settings.DB_HOST_note'));
$this->variables['DB_HOST'] = $this->option('host') ?? $this->ask(
'Database Host',
config('database.connections.pgsql.host', '127.0.0.1')
);

$this->variables['DB_PORT'] = $this->option('port') ?? $this->ask(
'Database Port',
config('database.connections.pgsql.port', 5432)
);

$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(
'Database Name',
config('database.connections.pgsql.database', 'panel')
);

$this->output->note(__('commands.database_settings.DB_USERNAME_note'));
$this->variables['DB_USERNAME'] = $this->option('username') ?? $this->ask(
'Database Username',
config('database.connections.pgsql.username', 'pelican')
);

$askForPGSQLPassword = true;
if (!empty(config('database.connections.pgsql.password')) && $this->input->isInteractive()) {
$this->variables['DB_PASSWORD'] = config('database.connections.pgsql.password');
$askForPGSQLPassword = $this->confirm(__('commands.database_settings.DB_PASSWORD_note'));
}

if ($askForPGSQLPassword) {
$this->variables['DB_PASSWORD'] = $this->option('password') ?? $this->secret('Database Password');
}

try {
// Test connection
config()->set('database.connections._panel_command_test', [
'driver' => 'pgsql',
'host' => $this->variables['DB_HOST'],
'port' => $this->variables['DB_PORT'],
'database' => $this->variables['DB_DATABASE'],
'username' => $this->variables['DB_USERNAME'],
'password' => $this->variables['DB_PASSWORD'],
'charset' => 'UTF8',
'collation' => 'en_US.UTF-8',
'strict' => true,
]);

$this->database->connection('_panel_command_test')->getPdo();
} catch (\PDOException $exception) {
$this->output->error(sprintf('Unable to connect to the PostgreSQL server using the provided credentials. The error returned was "%s".', $exception->getMessage()));
$this->output->error(__('commands.database_settings.DB_error_2'));

if ($this->confirm(__('commands.database_settings.go_back'))) {
$this->database->disconnect('_panel_command_test');

return $this->handle();
try {
// Test connection
DB::build([
'driver' => $driver->value,
'host' => $this->variables['DB_HOST'],
'port' => $this->variables['DB_PORT'],
'database' => $this->variables['DB_DATABASE'],
'username' => $this->variables['DB_USERNAME'],
'password' => $this->variables['DB_PASSWORD'],
'charset' => $driver->getDefaultOption('charset', true),
'collation' => $driver->getDefaultOption('collation', true),
'strict' => true,
])->beginTransaction();
} catch (\PDOException $exception) {
$this->output->error(sprintf('Unable to connect to the %s server using the provided credentials. The error returned was "%s".', $driver->getFriendlyName(), $exception->getMessage()));
$this->output->error(__('commands.database_settings.DB_error_2'));

if ($this->confirm(__('commands.database_settings.go_back'))) {
return $this->handle();
}

return 1;
}

return 1;
}
break;
}

$this->writeToEnvironment($this->variables);
Expand Down
Loading

0 comments on commit 754a28d

Please sign in to comment.