Skip to content

Commit c74e7e3

Browse files
authored
Make db:seed command prohibitable (#55238)
I think it is generally undesirable and potentially unsafe to execute seeders in production databases. This change allows users fine-grained control to prohibit `db:seed` from running.
1 parent 1d65895 commit c74e7e3

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Illuminate/Database/Console/Seeds/SeedCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Console\ConfirmableTrait;
7+
use Illuminate\Console\Prohibitable;
78
use Illuminate\Database\ConnectionResolverInterface as Resolver;
89
use Illuminate\Database\Eloquent\Model;
910
use Symfony\Component\Console\Attribute\AsCommand;
@@ -13,7 +14,7 @@
1314
#[AsCommand(name: 'db:seed')]
1415
class SeedCommand extends Command
1516
{
16-
use ConfirmableTrait;
17+
use ConfirmableTrait, Prohibitable;
1718

1819
/**
1920
* The console command name.
@@ -55,8 +56,9 @@ public function __construct(Resolver $resolver)
5556
*/
5657
public function handle()
5758
{
58-
if (! $this->confirmToProceed()) {
59-
return 1;
59+
if ($this->isProhibited() ||
60+
! $this->confirmToProceed()) {
61+
return Command::FAILURE;
6062
}
6163

6264
$this->components->info('Seeding database.');

tests/Database/SeedCommandTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Database;
44

5+
use Illuminate\Console\Command;
56
use Illuminate\Console\OutputStyle;
67
use Illuminate\Console\View\Components\Factory;
78
use Illuminate\Container\Container;
@@ -103,8 +104,39 @@ public function testWithoutModelEvents()
103104
$container->shouldHaveReceived('call')->with([$command, 'handle']);
104105
}
105106

107+
public function testProhibitable()
108+
{
109+
$input = new ArrayInput([]);
110+
$output = new NullOutput;
111+
$outputStyle = new OutputStyle($input, $output);
112+
113+
$resolver = m::mock(ConnectionResolverInterface::class);
114+
115+
$container = m::mock(Container::class);
116+
$container->shouldReceive('call');
117+
$container->shouldReceive('runningUnitTests')->andReturn('true');
118+
$container->shouldReceive('make')->with(OutputStyle::class, m::any())->andReturn(
119+
$outputStyle
120+
);
121+
$container->shouldReceive('make')->with(Factory::class, m::any())->andReturn(
122+
new Factory($outputStyle)
123+
);
124+
125+
$command = new SeedCommand($resolver);
126+
$command->setLaravel($container);
127+
128+
// call run to set up IO, then fire manually.
129+
$command->run($input, $output);
130+
131+
SeedCommand::prohibit();
132+
133+
Assert::assertSame(Command::FAILURE, $command->handle());
134+
}
135+
106136
protected function tearDown(): void
107137
{
138+
SeedCommand::prohibit(false);
139+
108140
Model::unsetEventDispatcher();
109141

110142
m::close();

0 commit comments

Comments
 (0)