Skip to content

Commit

Permalink
Prevent using unavailable databases (#334)
Browse files Browse the repository at this point in the history
* Prevent using unavailable databases

* wip

* wip

* wip

* wip

* wip

* Add missing extension label

* wip

* Update src/NewCommand.php

Co-authored-by: Jess Archer <jess@jessarcher.com>

* Update src/NewCommand.php

Co-authored-by: Jess Archer <jess@jessarcher.com>

* Update src/NewCommand.php

Co-authored-by: Jess Archer <jess@jessarcher.com>

* wip

* Update NewCommand.php

* Update NewCommand.php

---------

Co-authored-by: Jess Archer <jess@jessarcher.com>
Co-authored-by: Taylor Otwell <taylorotwell@gmail.com>
  • Loading branch information
3 people committed Apr 25, 2024
1 parent 54e99db commit b8587d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
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
40 changes: 29 additions & 11 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,29 +448,47 @@ 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,
));

if ($input->getOption('database') !== $defaultDatabase) {
$migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true);
if ($input->getOption('database') !== 'sqlite') {
$migrate = confirm(
label: 'Default database updated. Would you like to run the default database migrations?',
default: true
);
}
}

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

/**
* Get the available database options.
*
* @return array
*/
protected function databaseOptions(): array
{
return collect([
'sqlite' => ['SQLite', extension_loaded('pdo_sqlite')],
'mysql' => ['MySQL', extension_loaded('pdo_mysql')],
'mariadb' => ['MariaDB', extension_loaded('pdo_mysql')],
'pgsql' => ['PostgreSQL', extension_loaded('pdo_pgsql')],
'sqlsrv' => ['SQL Server', extension_loaded('pdo_sqlsrv')],
])
->sortBy(fn ($database) => $database[1] ? 0 : 1)
->map(fn ($database) => $database[0].($database[1] ? '' : ' (Missing PDO extension)'))
->all();
}

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

0 comments on commit b8587d0

Please sign in to comment.