Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent using unavailable databases #334

Merged
merged 14 commits into from
Apr 25, 2024
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, fileinfo
extensions: dom, curl, libxml, mbstring, zip, fileinfo, sqlsrv, pdo, pdo_sqlsrv
ini-values: error_reporting=E_ALL, memory_limit=512M
tools: composer:v2
coverage: none
Expand Down
61 changes: 51 additions & 10 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,29 +448,70 @@ protected function installJetstream(string $directory, InputInterface $input, Ou
*/
protected function promptForDatabaseOptions(string $directory, InputInterface $input)
{
$defaultDatabase = 'sqlite';
$defaultDatabase = collect(
$databaseOptions = $this->databaseOptions()
)->keys()->first();

if (! $input->getOption('database') && $input->isInteractive()) {
$input->setOption('database', select(
label: 'Which database will your application use?',
options: [
'mysql' => 'MySQL',
'mariadb' => 'MariaDB',
'pgsql' => 'PostgreSQL',
'sqlite' => 'SQLite',
'sqlsrv' => 'SQL Server',
],
default: $defaultDatabase
options: $databaseOptions,
default: $defaultDatabase,
validate: fn (string $engine) => ! $this->isDatabaseEngineAvailable($engine)
? 'You selected an engine with a missing PDO extension. Please select an available option.'
: null
driesvints marked this conversation as resolved.
Show resolved Hide resolved
));

if ($input->getOption('database') !== $defaultDatabase) {
driesvints marked this conversation as resolved.
Show resolved Hide resolved
$migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true);
$migrate = confirm(
label: 'Default database updated. Would you like to run the default database migrations?',
default: true
);
}
}

return [$input->getOption('database') ?? $defaultDatabase, $migrate ?? false];
}

/**
* Return the database options.
*
* @return array
*/
public function databaseOptions(): array
{
return collect([
// Sequence is that of the default selected database option.
'sqlite' => 'SQLite',
'mysql' => 'MySQL',
'mariadb' => 'MariaDB',
'pgsql' => 'PostgreSQL',
'sqlsrv' => 'SQL Server',
])->map(function ($label, $engine) {
return $this->isDatabaseEngineAvailable($engine)
? $label
: "$label (Missing PDO extension)";
})->all();
}

/**
* Determine if the given database engine is available.
*
* @param string $engine
* @return bool
*/
public function isDatabaseEngineAvailable(string $engine): bool
{
return match ($engine) {
'mysql',
'mariadb' => extension_loaded('pdo_mysql'),
'pgsql' => extension_loaded('pdo_pgsql'),
'sqlite' => extension_loaded('pdo_sqlite'),
'sqlsrv' => extension_loaded('pdo_sqlsrv'),
default => false,
};
}
driesvints marked this conversation as resolved.
Show resolved Hide resolved

/**
* Determine the stack for Breeze.
*
Expand Down