Skip to content

Commit b11ae23

Browse files
Added withOperation method to migration
1 parent 58a68da commit b11ae23

File tree

10 files changed

+218
-0
lines changed

10 files changed

+218
-0
lines changed

src/Listeners/Listener.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelDeployOperations\Listeners;
6+
7+
use DragonCode\LaravelDeployOperations\Console\OperationsCommand;
8+
use DragonCode\LaravelDeployOperations\Console\RollbackCommand;
9+
use DragonCode\LaravelDeployOperations\Constants\Options;
10+
use Illuminate\Database\Migrations\Migration;
11+
use Illuminate\Support\Facades\Artisan;
12+
13+
use function array_merge;
14+
15+
abstract class Listener
16+
{
17+
protected function withOperation(Migration $migration): ?string
18+
{
19+
if (method_exists($migration, 'withOperation')) {
20+
return $migration->withOperation();
21+
}
22+
23+
return null;
24+
}
25+
26+
protected function run(string $method, string $operation): void
27+
{
28+
match ($method) {
29+
'up' => $this->call(OperationsCommand::class, $operation),
30+
'down' => $this->call(RollbackCommand::class, $operation, ['--force' => true]),
31+
};
32+
}
33+
34+
protected function call(string $command, string $filename, array $parameters = []): void
35+
{
36+
Artisan::call($command, array_merge([
37+
'--' . Options::Path => $filename,
38+
], $parameters));
39+
}
40+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelDeployOperations\Listeners;
6+
7+
use Illuminate\Database\Events\MigrationEnded;
8+
9+
class MigrationEndedListener extends Listener
10+
{
11+
public function handle(MigrationEnded $event): void
12+
{
13+
if ($operation = $this->withOperation($event->migration)) {
14+
$this->run($event->method, $operation);
15+
}
16+
}
17+
}

src/ServiceProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace DragonCode\LaravelDeployOperations;
66

77
use DragonCode\LaravelDeployOperations\Concerns\HasAbout;
8+
use DragonCode\LaravelDeployOperations\Listeners\MigrationEndedListener;
9+
use Illuminate\Database\Events\MigrationEnded;
10+
use Illuminate\Support\Facades\Event;
811
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
912

1013
class ServiceProvider extends BaseServiceProvider
@@ -21,6 +24,8 @@ public function boot(): void
2124
$this->registerCommands();
2225
$this->registerMigrations();
2326
}
27+
28+
$this->registerEvents();
2429
}
2530

2631
public function register(): void
@@ -42,6 +47,11 @@ protected function registerCommands(): void
4247
]);
4348
}
4449

50+
protected function registerEvents(): void
51+
{
52+
Event::listen(MigrationEnded::class, MigrationEndedListener::class);
53+
}
54+
4555
protected function registerMigrations(): void
4656
{
4757
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');

tests/Commands/OperationsTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DragonCode\LaravelDeployOperations\Constants\Names;
88
use DragonCode\LaravelDeployOperations\Jobs\OperationJob;
99
use Exception;
10+
use Illuminate\Database\Console\Migrations\MigrateCommand;
1011
use Illuminate\Support\Facades\DB;
1112
use Illuminate\Support\Facades\Queue;
1213
use Illuminate\Support\Str;
@@ -746,4 +747,35 @@ public function testSync()
746747
$this->assertDatabaseOperationHas($this->table, 'foo_bar');
747748
$this->assertDatabaseOperationDoesntLike($this->table, 'every_time');
748749
}
750+
751+
public function testViaMigrationMethod()
752+
{
753+
$this->loadMigrationsFrom(__DIR__ . '/../fixtures/migrations_with_operations');
754+
755+
$this->copyViaMigrations();
756+
757+
$table = 'test';
758+
759+
$this->artisan(Names::Install)->assertExitCode(0);
760+
761+
$this->assertDatabaseCount($table, 0);
762+
$this->assertDatabaseCount($this->table, 0);
763+
$this->assertDatabaseOperationDoesntLike($this->table, 'custom');
764+
$this->assertDatabaseOperationDoesntLike($this->table, 'invoke');
765+
$this->assertDatabaseOperationDoesntLike($this->table, 'up_down');
766+
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
767+
$this->assertDatabaseOperationDoesntLike($table, 'invoke', column: 'value');
768+
$this->assertDatabaseOperationDoesntLike($table, 'up_down', column: 'value');
769+
770+
$this->artisan(MigrateCommand::class)->assertExitCode(0);
771+
772+
$this->assertDatabaseCount($table, 2);
773+
$this->assertDatabaseCount($this->table, 2);
774+
$this->assertDatabaseOperationDoesntLike($this->table, 'custom');
775+
$this->assertDatabaseOperationHas($this->table, 'invoke');
776+
$this->assertDatabaseOperationHas($this->table, 'up_down');
777+
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
778+
$this->assertDatabaseOperationHas($table, 'invoke', column: 'value');
779+
$this->assertDatabaseOperationHas($table, 'up_down', column: 'value');
780+
}
749781
}

tests/Concerns/Files.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ protected function copyDI(): void
5050
);
5151
}
5252

53+
protected function copyViaMigrations(): void
54+
{
55+
File::copyDirectory(
56+
__DIR__ . '/../fixtures/app/via_migrations',
57+
$this->targetDirectory()
58+
);
59+
}
60+
5361
protected function copySuccessFailureMethod(): void
5462
{
5563
File::copy(
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelDeployOperations\Operation;
6+
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Support\Facades\DB;
8+
use Tests\Concerns\Some;
9+
10+
return new class extends Operation {
11+
public function __invoke(Some $some): void
12+
{
13+
$this->table()->insert([
14+
'value' => $some->get('custom'),
15+
]);
16+
}
17+
18+
protected function table(): Builder
19+
{
20+
return DB::table('test');
21+
}
22+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelDeployOperations\Operation;
6+
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Support\Facades\DB;
8+
use Tests\Concerns\Some;
9+
10+
return new class extends Operation {
11+
public function __invoke(Some $some): void
12+
{
13+
$this->table()->insert([
14+
'value' => $some->get('invoke'),
15+
]);
16+
}
17+
18+
protected function table(): Builder
19+
{
20+
return DB::table('test');
21+
}
22+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DragonCode\LaravelDeployOperations\Operation;
6+
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Support\Facades\DB;
8+
use Tests\Concerns\Some;
9+
10+
return new class extends Operation {
11+
public function up(Some $some): void
12+
{
13+
$this->table()->insert([
14+
'value' => $some->get('up_down'),
15+
]);
16+
}
17+
18+
public function down(Some $some): void
19+
{
20+
$this->table()
21+
->where('value', $some->get('up_down'))
22+
->delete();
23+
}
24+
25+
protected function table(): Builder
26+
{
27+
return DB::table('test');
28+
}
29+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
return new class extends Migration {
8+
public function up(): void
9+
{
10+
//
11+
}
12+
13+
public function down(): void {}
14+
15+
public function withOperation(): string
16+
{
17+
return '2025_03_31_234251_invoke';
18+
}
19+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
return new class extends Migration {
8+
public function up(): void
9+
{
10+
//
11+
}
12+
13+
public function down(): void {}
14+
15+
public function withOperation(): string
16+
{
17+
return '2025_03_31_234312_up_down';
18+
}
19+
};

0 commit comments

Comments
 (0)