Skip to content

Added withOperation method to migrations #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Data/Casts/PathCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@
namespace DragonCode\LaravelDeployOperations\Data\Casts;

use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper;
use Illuminate\Support\Str;
use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Support\DataProperty;

use function app;
use function realpath;

class PathCast implements Cast
{
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): string
{
$path = $this->config()->basePath((string) $value);

if ($properties['realpath'] ?? false) {
return $value ?: $this->config()->basePath();
return $value ?: $path;
}

return $this->config()->basePath($value);
return $this->filename($path) ?: $path;
}

protected function filename(string $path): false|string
{
return realpath(Str::finish($path, '.php'));
}

protected function config(): ConfigHelper
Expand Down
40 changes: 40 additions & 0 deletions src/Listeners/Listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelDeployOperations\Listeners;

use DragonCode\LaravelDeployOperations\Console\OperationsCommand;
use DragonCode\LaravelDeployOperations\Console\RollbackCommand;
use DragonCode\LaravelDeployOperations\Constants\Options;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Artisan;

use function array_merge;

abstract class Listener
{
protected function withOperation(Migration $migration): ?string
{
if (method_exists($migration, 'withOperation')) {
return $migration->withOperation();
}

return null;
}

protected function run(string $method, string $operation): void
{
match ($method) {
'up' => $this->call(OperationsCommand::class, $operation),
'down' => $this->call(RollbackCommand::class, $operation, ['--force' => true]),
};
}

protected function call(string $command, string $filename, array $parameters = []): void
{
Artisan::call($command, array_merge([
'--' . Options::Path => $filename,
], $parameters));
}
}
17 changes: 17 additions & 0 deletions src/Listeners/MigrationEndedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelDeployOperations\Listeners;

use Illuminate\Database\Events\MigrationEnded;

class MigrationEndedListener extends Listener
{
public function handle(MigrationEnded $event): void
{
if ($operation = $this->withOperation($event->migration)) {
$this->run($event->method, $operation);
}
}
}
10 changes: 10 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace DragonCode\LaravelDeployOperations;

use DragonCode\LaravelDeployOperations\Concerns\HasAbout;
use DragonCode\LaravelDeployOperations\Listeners\MigrationEndedListener;
use Illuminate\Database\Events\MigrationEnded;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
Expand All @@ -21,6 +24,8 @@ public function boot(): void
$this->registerCommands();
$this->registerMigrations();
}

$this->registerEvents();
}

public function register(): void
Expand All @@ -42,6 +47,11 @@ protected function registerCommands(): void
]);
}

protected function registerEvents(): void
{
Event::listen(MigrationEnded::class, MigrationEndedListener::class);
}

protected function registerMigrations(): void
{
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
Expand Down
6 changes: 6 additions & 0 deletions src/Services/MigratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Illuminate\Support\Facades\DB;
use Throwable;

use function file_exists;
use function is_file;
use function method_exists;
use function realpath;

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

protected function resolvePath(string $filename, string $path): string
{
if (file_exists($path) && is_file($path)) {
return $path;
}

$withExtension = Str::finish($filename, '.php');

if ($this->file->exists($withExtension) && $this->file->isFile($withExtension)) {
Expand Down
44 changes: 44 additions & 0 deletions tests/Commands/OperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DragonCode\LaravelDeployOperations\Constants\Names;
use DragonCode\LaravelDeployOperations\Jobs\OperationJob;
use Exception;
use Illuminate\Database\Console\Migrations\RollbackCommand;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -746,4 +747,47 @@ public function testSync()
$this->assertDatabaseOperationHas($this->table, 'foo_bar');
$this->assertDatabaseOperationDoesntLike($this->table, 'every_time');
}

public function testViaMigrationMethod()
{
$this->copyViaMigrations();

$table = 'test';

$this->artisan(Names::Install)->assertExitCode(0);

$this->assertDatabaseCount($table, 0);
$this->assertDatabaseCount($this->table, 0);
$this->assertDatabaseOperationDoesntLike($this->table, 'custom');
$this->assertDatabaseOperationDoesntLike($this->table, 'invoke');
$this->assertDatabaseOperationDoesntLike($this->table, 'up_down');
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
$this->assertDatabaseOperationDoesntLike($table, 'invoke', column: 'value');
$this->assertDatabaseOperationDoesntLike($table, 'up_down', column: 'value');

$this->loadMigrationsFrom(__DIR__ . '/../fixtures/migrations_with_operations');

$this->assertDatabaseCount($table, 2);
$this->assertDatabaseCount($this->table, 2);
$this->assertDatabaseOperationDoesntLike($this->table, 'custom');
$this->assertDatabaseOperationHas($this->table, 'invoke');
$this->assertDatabaseOperationHas($this->table, 'up_down');
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
$this->assertDatabaseOperationHas($table, 'invoke', column: 'value');
$this->assertDatabaseOperationHas($table, 'up_down', column: 'value');

$this->artisan(RollbackCommand::class, [
'--path' => __DIR__ . '/../fixtures/migrations_with_operations',
'--realpath' => true,
])->assertSuccessful();

$this->assertDatabaseCount($table, 1);
$this->assertDatabaseCount($this->table, 0);
$this->assertDatabaseOperationDoesntLike($this->table, 'custom');
$this->assertDatabaseOperationDoesntLike($this->table, 'invoke');
$this->assertDatabaseOperationDoesntLike($this->table, 'up_down');
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
$this->assertDatabaseOperationHas($table, 'invoke', column: 'value');
$this->assertDatabaseOperationDoesntLike($table, 'up_down', column: 'value');
}
}
8 changes: 8 additions & 0 deletions tests/Concerns/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ protected function copyDI(): void
);
}

protected function copyViaMigrations(): void
{
File::copyDirectory(
__DIR__ . '/../fixtures/app/via_migrations',
$this->targetDirectory()
);
}

protected function copySuccessFailureMethod(): void
{
File::copy(
Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/app/via_migrations/2025_03_31_213407_custom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelDeployOperations\Operation;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\Some;

return new class extends Operation {
public function __invoke(Some $some): void
{
$this->table()->insert([
'value' => $some->get('custom'),
]);
}

protected function table(): Builder
{
return DB::table('test');
}
};
22 changes: 22 additions & 0 deletions tests/fixtures/app/via_migrations/2025_03_31_234251_invoke.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelDeployOperations\Operation;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\Some;

return new class extends Operation {
public function __invoke(Some $some): void
{
$this->table()->insert([
'value' => $some->get('invoke'),
]);
}

protected function table(): Builder
{
return DB::table('test');
}
};
29 changes: 29 additions & 0 deletions tests/fixtures/app/via_migrations/2025_03_31_234312_up_down.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use DragonCode\LaravelDeployOperations\Operation;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\Some;

return new class extends Operation {
public function up(Some $some): void
{
$this->table()->insert([
'value' => $some->get('up_down'),
]);
}

public function down(Some $some): void
{
$this->table()
->where('value', $some->get('up_down'))
->delete();
}

protected function table(): Builder
{
return DB::table('test');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
public function up(): void {}

public function down(): void {}

public function withOperation(): string
{
return '2025_03_31_234251_invoke';
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
public function up(): void {}

public function down(): void {}

public function withOperation(): string
{
return '2025_03_31_234312_up_down';
}
};
Loading