From b8587d009bad1565bf5b1751e7572e621b509adb Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 25 Apr 2024 16:46:15 +0200 Subject: [PATCH] Prevent using unavailable databases (#334) * Prevent using unavailable databases * wip * wip * wip * wip * wip * Add missing extension label * wip * Update src/NewCommand.php Co-authored-by: Jess Archer * Update src/NewCommand.php Co-authored-by: Jess Archer * Update src/NewCommand.php Co-authored-by: Jess Archer * wip * Update NewCommand.php * Update NewCommand.php --------- Co-authored-by: Jess Archer Co-authored-by: Taylor Otwell --- .github/workflows/tests.yml | 2 +- src/NewCommand.php | 40 +++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b204a66..34f86d0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/src/NewCommand.php b/src/NewCommand.php index b1acaae..497f5c2 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -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. *