Skip to content

Commit 7c703eb

Browse files
committed
db:truncate command
1 parent 0341bc7 commit 7c703eb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Commands/DBTruncateCommand.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace LaracraftTech\LaravelUsefulAdditions\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Console\ConfirmableTrait;
7+
use Illuminate\Support\Facades\DB;
8+
use Illuminate\Support\Facades\Schema;
9+
10+
class DBTruncateCommand extends Command
11+
{
12+
use ConfirmableTrait;
13+
14+
public $signature = 'db:truncate';
15+
16+
public $description = 'Truncate all tables';
17+
18+
public function handle(): int
19+
{
20+
if (! $this->confirmToProceed()) {
21+
return 1;
22+
}
23+
24+
$this->components->info('Preparing database.');
25+
26+
collect(Schema::getAllTables())->each(function ($tableDefinition) {
27+
$table = current($tableDefinition);
28+
$this->components->task("Truncating table: $table", function () use ($table) {
29+
DB::table($table)->truncate();
30+
});
31+
});
32+
33+
$this->newLine();
34+
35+
return self::SUCCESS;
36+
}
37+
}

src/LaravelUsefulAdditionsServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LaracraftTech\LaravelUsefulAdditions;
44

5+
use LaracraftTech\LaravelUsefulAdditions\Commands\DBTruncateCommand;
56
use Spatie\LaravelPackageTools\Package;
67
use Spatie\LaravelPackageTools\PackageServiceProvider;
78

@@ -16,6 +17,7 @@ public function configurePackage(Package $package): void
1617
*/
1718
$package
1819
->name('laravel-useful-additions')
19-
->hasConfigFile();
20+
->hasConfigFile()
21+
->hasCommand(DBTruncateCommand::class);;
2022
}
2123
}

0 commit comments

Comments
 (0)