Skip to content

Commit

Permalink
[7.x] package:create-sqlite-db and package:drop-sqlite-db improve…
Browse files Browse the repository at this point in the history
…ments (#252)

* Add `--database` option to both create and drop commands.
* Add `--all` option to drop all SQLite databases within `database`
  directory.

Solves #251

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Nov 6, 2024
1 parent f105640 commit 0f4eda6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/Foundation/Console/CreateSqliteDbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;

use function Orchestra\Testbench\join_paths;
Expand All @@ -16,7 +17,9 @@ class CreateSqliteDbCommand extends Command
*
* @var string
*/
protected $signature = 'package:create-sqlite-db {--force : Overwrite the database file}';
protected $signature = 'package:create-sqlite-db
{--database=database.sqlite : Set the database name}
{--force : Overwrite the database file}';

/**
* Execute the console command.
Expand All @@ -38,7 +41,7 @@ public function handle(Filesystem $filesystem)
? join_paths($databasePath, 'database.sqlite.example')
: (string) realpath(join_paths(__DIR__, 'stubs', 'database.sqlite.example'));

$to = join_paths($databasePath, 'database.sqlite');
$to = join_paths($databasePath, $this->databaseName());

(new Actions\GeneratesFile(
filesystem: $filesystem,
Expand All @@ -49,4 +52,19 @@ public function handle(Filesystem $filesystem)

return Command::SUCCESS;
}

/**
* Resolve the database name.
*
* @return string
*/
protected function databaseName(): string
{
if (empty($database = $this->option('database'))) {
$database = 'database';
}

/** @var string $database */
return \sprintf('%s.sqlite', Str::before((string) $database, '.sqlite'));
}
}
27 changes: 25 additions & 2 deletions src/Foundation/Console/DropSqliteDbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;

use function Orchestra\Testbench\join_paths;
Expand All @@ -16,7 +17,9 @@ class DropSqliteDbCommand extends Command
*
* @var string
*/
protected $signature = 'package:drop-sqlite-db';
protected $signature = 'package:drop-sqlite-db
{--database=database.sqlite : Set the database name}
{--all : Delete all SQLite databases}';

/**
* Execute the console command.
Expand All @@ -33,8 +36,28 @@ public function handle(Filesystem $filesystem)
filesystem: $filesystem,
components: $this->components,
workingPath: $workingPath,
))->handle([join_paths($databasePath, 'database.sqlite')]);
))->handle(
match ($this->option('all')) {
true => [...$filesystem->glob(join_paths($databasePath, '*.sqlite'))],
default => [join_paths($databasePath, $this->databaseName())],
}
);

return Command::SUCCESS;
}

/**
* Resolve the database name.
*
* @return string
*/
protected function databaseName(): string
{
if (empty($database = $this->option('database'))) {
$database = 'database';
}

/** @var string $database */
return \sprintf('%s.sqlite', Str::before((string) $database, '.sqlite'));
}
}

0 comments on commit 0f4eda6

Please sign in to comment.