Skip to content

Commit bbc0987

Browse files
Merge pull request #200 from TheDragonCode/7.x
Added `withOperation` method to migrations
2 parents 68436fe + fea899e commit bbc0987

File tree

12 files changed

+241
-2
lines changed

12 files changed

+241
-2
lines changed

src/Data/Casts/PathCast.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,30 @@
55
namespace DragonCode\LaravelDeployOperations\Data\Casts;
66

77
use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper;
8+
use Illuminate\Support\Str;
89
use Spatie\LaravelData\Casts\Cast;
910
use Spatie\LaravelData\Support\Creation\CreationContext;
1011
use Spatie\LaravelData\Support\DataProperty;
1112

1213
use function app;
14+
use function realpath;
1315

1416
class PathCast implements Cast
1517
{
1618
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): string
1719
{
20+
$path = $this->config()->basePath((string) $value);
21+
1822
if ($properties['realpath'] ?? false) {
19-
return $value ?: $this->config()->basePath();
23+
return $value ?: $path;
2024
}
2125

22-
return $this->config()->basePath($value);
26+
return $this->filename($path) ?: $path;
27+
}
28+
29+
protected function filename(string $path): false|string
30+
{
31+
return realpath(Str::finish($path, '.php'));
2332
}
2433

2534
protected function config(): ConfigHelper

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');

src/Services/MigratorService.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Illuminate\Support\Facades\DB;
2020
use Throwable;
2121

22+
use function file_exists;
23+
use function is_file;
2224
use function method_exists;
2325
use function realpath;
2426

@@ -148,6 +150,10 @@ protected function disallowBefore(Operation $operation, OptionsData $options): b
148150

149151
protected function resolvePath(string $filename, string $path): string
150152
{
153+
if (file_exists($path) && is_file($path)) {
154+
return $path;
155+
}
156+
151157
$withExtension = Str::finish($filename, '.php');
152158

153159
if ($this->file->exists($withExtension) && $this->file->isFile($withExtension)) {

tests/Commands/OperationsTest.php

Lines changed: 44 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\RollbackCommand;
1011
use Illuminate\Support\Facades\DB;
1112
use Illuminate\Support\Facades\Queue;
1213
use Illuminate\Support\Str;
@@ -746,4 +747,47 @@ 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->copyViaMigrations();
754+
755+
$table = 'test';
756+
757+
$this->artisan(Names::Install)->assertExitCode(0);
758+
759+
$this->assertDatabaseCount($table, 0);
760+
$this->assertDatabaseCount($this->table, 0);
761+
$this->assertDatabaseOperationDoesntLike($this->table, 'custom');
762+
$this->assertDatabaseOperationDoesntLike($this->table, 'invoke');
763+
$this->assertDatabaseOperationDoesntLike($this->table, 'up_down');
764+
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
765+
$this->assertDatabaseOperationDoesntLike($table, 'invoke', column: 'value');
766+
$this->assertDatabaseOperationDoesntLike($table, 'up_down', column: 'value');
767+
768+
$this->loadMigrationsFrom(__DIR__ . '/../fixtures/migrations_with_operations');
769+
770+
$this->assertDatabaseCount($table, 2);
771+
$this->assertDatabaseCount($this->table, 2);
772+
$this->assertDatabaseOperationDoesntLike($this->table, 'custom');
773+
$this->assertDatabaseOperationHas($this->table, 'invoke');
774+
$this->assertDatabaseOperationHas($this->table, 'up_down');
775+
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
776+
$this->assertDatabaseOperationHas($table, 'invoke', column: 'value');
777+
$this->assertDatabaseOperationHas($table, 'up_down', column: 'value');
778+
779+
$this->artisan(RollbackCommand::class, [
780+
'--path' => __DIR__ . '/../fixtures/migrations_with_operations',
781+
'--realpath' => true,
782+
])->assertSuccessful();
783+
784+
$this->assertDatabaseCount($table, 1);
785+
$this->assertDatabaseCount($this->table, 0);
786+
$this->assertDatabaseOperationDoesntLike($this->table, 'custom');
787+
$this->assertDatabaseOperationDoesntLike($this->table, 'invoke');
788+
$this->assertDatabaseOperationDoesntLike($this->table, 'up_down');
789+
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
790+
$this->assertDatabaseOperationHas($table, 'invoke', column: 'value');
791+
$this->assertDatabaseOperationDoesntLike($table, 'up_down', column: 'value');
792+
}
749793
}

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
public function down(): void {}
11+
12+
public function withOperation(): string
13+
{
14+
return '2025_03_31_234251_invoke';
15+
}
16+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
public function down(): void {}
11+
12+
public function withOperation(): string
13+
{
14+
return '2025_03_31_234312_up_down';
15+
}
16+
};

0 commit comments

Comments
 (0)