Skip to content

Commit f3e8cc5

Browse files
author
Andrey Helldar
authored
Merge pull request #51 from TheDragonCode/2.x
Added `migrate:actions:fresh` command
2 parents 7b95858 + 719640a commit f3e8cc5

File tree

6 files changed

+119
-6
lines changed

6 files changed

+119
-6
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ re-migrate the last five migrations:
319319
php artisan migrate:actions:refresh --step=5
320320
```
321321

322+
### Drop All Actions & Migrate Actions
323+
324+
The `migrate:actions:fresh` command will drop all actions records from the actions table and then execute the migrate command:
325+
326+
```
327+
php artisan migrate:actions:fresh
328+
```
329+
322330
### Actions Status
323331

324332
The `migrate:actions:status` command displays the execution status of actions. In it you can see which actions were executed and which were not:
@@ -343,17 +351,17 @@ return new class extends Actionable
343351
{
344352
//
345353
}
346-
354+
347355
public function down(): void
348356
{
349357
//
350358
}
351-
359+
352360
public function success(): void
353361
{
354362
Log::info('success');
355363
}
356-
364+
357365
public function failed(): void
358366
{
359367
Log::info('failed');
@@ -378,17 +386,17 @@ return new class extends Actionable
378386
{
379387
throw new Exeption();
380388
}
381-
389+
382390
public function down(): void
383391
{
384392
throw new Exeption();
385393
}
386-
394+
387395
public function success(): void
388396
{
389397
Log::info('success');
390398
}
391-
399+
392400
public function failed(): void
393401
{
394402
Log::info('failed');

src/Console/Fresh.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelActions\Console;
6+
7+
use DragonCode\LaravelActions\Concerns\Optionable;
8+
use DragonCode\LaravelActions\Constants\Names;
9+
use Illuminate\Database\Console\Migrations\FreshCommand;
10+
use Illuminate\Support\Facades\Schema;
11+
12+
class Fresh extends FreshCommand
13+
{
14+
use Optionable;
15+
16+
/**
17+
* The console command name.
18+
*
19+
* @var string
20+
*/
21+
protected $name = Names::FRESH;
22+
23+
/**
24+
* The console command description.
25+
*
26+
* @var string
27+
*/
28+
protected $description = 'Drop and re-run all actions';
29+
30+
public function handle()
31+
{
32+
if (! $this->confirmToProceed()) {
33+
return 1;
34+
}
35+
36+
$database = $this->optionDatabase();
37+
38+
$this->dropTable();
39+
$this->migrate($database);
40+
41+
return 0;
42+
}
43+
44+
protected function dropTable(): void
45+
{
46+
Schema::dropIfExists($this->getTableName());
47+
}
48+
49+
protected function migrate(?string $database): void
50+
{
51+
$this->call(Names::MIGRATE,
52+
array_filter([
53+
'--database' => $database,
54+
'--force' => true,
55+
]));
56+
}
57+
58+
protected function getTableName(): string
59+
{
60+
return config('database.actions');
61+
}
62+
}

src/Constants/Command.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Command
1212

1313
public const REFRESH = 'command.migration.actions.refresh';
1414

15+
public const FRESH = 'command.migration.actions.fresh';
16+
1517
public const RESET = 'command.migration.actions.reset';
1618

1719
public const ROLLBACK = 'command.migration.actions.rollback';

src/Constants/Names.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class Names
66
{
7+
public const FRESH = 'migrate:actions:fresh';
8+
79
public const INSTALL = 'migrate:actions:install';
810

911
public const MAKE = 'make:migration:action';

src/ServiceProvider.php

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

33
namespace DragonCode\LaravelActions;
44

5+
use DragonCode\LaravelActions\Console\Fresh;
56
use DragonCode\LaravelActions\Console\Install;
67
use DragonCode\LaravelActions\Console\Make;
78
use DragonCode\LaravelActions\Console\Migrate;
@@ -21,6 +22,7 @@ class ServiceProvider extends BaseServiceProvider
2122
{
2223
protected $commands = [
2324
'Migrate' => Command::MIGRATE,
25+
'MigrateFresh' => Command::FRESH,
2426
'MigrateInstall' => Command::INSTALL,
2527
'MigrateMake' => Command::MAKE,
2628
'MigrateRefresh' => Command::REFRESH,
@@ -129,6 +131,13 @@ protected function registerMigrateRefreshCommand(): void
129131
});
130132
}
131133

134+
protected function registerMigrateFreshCommand(): void
135+
{
136+
$this->app->singleton(Command::FRESH, function () {
137+
return new Fresh();
138+
});
139+
}
140+
132141
protected function registerConfig(): void
133142
{
134143
$this->mergeConfigFrom(

tests/Commands/FreshTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Commands;
6+
7+
use Tests\TestCase;
8+
9+
class FreshTest extends TestCase
10+
{
11+
public function testFreshCommand()
12+
{
13+
$this->assertDatabaseDoesntTable($this->table);
14+
15+
$this->artisan('migrate:actions:install')->run();
16+
17+
$this->assertDatabaseHasTable($this->table);
18+
$this->assertDatabaseCount($this->table, 0);
19+
20+
$this->artisan('make:migration:action', ['name' => 'Fresh'])->run();
21+
$this->artisan('migrate:actions')->run();
22+
23+
$this->assertDatabaseCount($this->table, 1);
24+
25+
$this->artisan('migrate:actions:fresh')->run();
26+
27+
$this->assertDatabaseCount($this->table, 1);
28+
$this->assertDatabaseMigrationHas($this->table, 'fresh');
29+
}
30+
}

0 commit comments

Comments
 (0)