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 1 commit
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
Prev Previous commit
Fixed a call of operations
  • Loading branch information
andrey-helldar committed Apr 1, 2025
commit fea899e80c4ba495bd87b2d55450cfb8a4ccac3c
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
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
20 changes: 16 additions & 4 deletions tests/Commands/OperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use DragonCode\LaravelDeployOperations\Constants\Names;
use DragonCode\LaravelDeployOperations\Jobs\OperationJob;
use Exception;
use Illuminate\Database\Console\Migrations\MigrateCommand;
use Illuminate\Database\Console\Migrations\RollbackCommand;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -750,8 +750,6 @@ public function testSync()

public function testViaMigrationMethod()
{
$this->loadMigrationsFrom(__DIR__ . '/../fixtures/migrations_with_operations');

$this->copyViaMigrations();

$table = 'test';
Expand All @@ -767,7 +765,7 @@ public function testViaMigrationMethod()
$this->assertDatabaseOperationDoesntLike($table, 'invoke', column: 'value');
$this->assertDatabaseOperationDoesntLike($table, 'up_down', column: 'value');

$this->artisan(MigrateCommand::class)->assertExitCode(0);
$this->loadMigrationsFrom(__DIR__ . '/../fixtures/migrations_with_operations');

$this->assertDatabaseCount($table, 2);
$this->assertDatabaseCount($this->table, 2);
Expand All @@ -777,5 +775,19 @@ public function testViaMigrationMethod()
$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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
use Illuminate\Database\Migrations\Migration;

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

public function down(): void {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
use Illuminate\Database\Migrations\Migration;

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

public function down(): void {}

Expand Down
Loading