Skip to content

Commit 6c166c7

Browse files
author
Andrey Helldar
authored
Merge pull request #21 from TheDragonCode/2.x
Add the ability to execute a specific file
2 parents 73d70e0 + d7a80ba commit 6c166c7

File tree

5 files changed

+128
-8
lines changed

5 files changed

+128
-8
lines changed

src/Console/Migrate.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class Migrate extends BaseCommand
2222
protected $signature = Names::MIGRATE
2323
. ' {--database= : The database connection to use}'
2424
. ' {--force : Force the operation to run when in production}'
25-
. ' {--step : Force the actions to be run so they can be rolled back individually}';
25+
. ' {--step : Force the actions to be run so they can be rolled back individually}'
26+
. ' {--path=* : The path(s) to the migrations files to be executed}'
27+
. ' {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}';
2628

2729
/**
2830
* The console command description.
@@ -47,8 +49,7 @@ public function handle()
4749

4850
$this->migrator->setOutput($this->output)
4951
->run($this->getMigrationPaths(), [
50-
'pretend' => null,
51-
'step' => $this->optionStep(),
52+
'step' => $this->optionStep(),
5253
]);
5354
});
5455

@@ -58,12 +59,23 @@ public function handle()
5859
/**
5960
* Prepare the action database for running.
6061
*/
61-
protected function prepareDatabase()
62+
protected function prepareDatabase(): void
6263
{
6364
if (! $this->migrator->repositoryExists()) {
6465
$this->call(Names::INSTALL, array_filter([
6566
'--database' => $this->optionDatabase(),
6667
]));
6768
}
6869
}
70+
71+
protected function getMigrationPaths(): array
72+
{
73+
if ($paths = $this->optionPath()) {
74+
return $paths;
75+
}
76+
77+
return array_merge(
78+
$this->migrator->paths(), [$this->getMigrationPath()]
79+
);
80+
}
6981
}

src/ServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ServiceProvider extends BaseServiceProvider
2929
'MigrateStatus' => Command::STATUS,
3030
];
3131

32-
public function register()
32+
public function register(): void
3333
{
3434
$this->registerConfig();
3535
$this->registerRepository();
@@ -38,7 +38,7 @@ public function register()
3838
$this->registerCommands($this->commands);
3939
}
4040

41-
public function provides()
41+
public function provides(): array
4242
{
4343
return array_merge([
4444
Action::MIGRATOR,

src/Traits/Optionable.php

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

33
namespace DragonCode\LaravelActions\Traits;
44

5+
use Illuminate\Support\Str;
6+
57
/** @mixin \Illuminate\Console\Command */
68
trait Optionable
79
{
@@ -14,4 +16,25 @@ protected function optionStep(int $default = null): ?int
1416
{
1517
return $this->input->getOption('step') ?: $default;
1618
}
19+
20+
protected function optionPath(): ?array
21+
{
22+
if ($path = $this->option('path')) {
23+
return collect($path)->map(function ($path) {
24+
if ($this->usingRealPath()) {
25+
return $path;
26+
}
27+
28+
$filename = $this->getMigrationPath() . DIRECTORY_SEPARATOR . $path;
29+
30+
if (is_dir($filename) || file_exists($filename)) {
31+
return $filename;
32+
}
33+
34+
return Str::finish($filename, '.php');
35+
})->all();
36+
}
37+
38+
return null;
39+
}
1740
}

tests/Commands/MigrateTest.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public function testFailedTransaction()
8888

8989
try {
9090
$this->artisan('migrate:actions')->run();
91-
}
92-
catch (Exception $e) {
91+
} catch (Exception $e) {
9392
$this->assertSame(Exception::class, get_class($e));
9493
$this->assertSame('Random message', $e->getMessage());
9594
}
@@ -204,6 +203,66 @@ public function testAllow()
204203
$this->artisan('migrate:actions')->run();
205204
}
206205

206+
public function testPathAsFileWithExtension()
207+
{
208+
$this->copyFiles();
209+
210+
$table = 'test';
211+
212+
$path = 'sub_path/2021_12_15_205804_baz.php';
213+
214+
$this->artisan('migrate:actions:install')->run();
215+
216+
$this->assertDatabaseCount($table, 0);
217+
$this->assertDatabaseCount($this->table, 0);
218+
$this->assertDatabaseMigrationDoesntLike($this->table, 'baz');
219+
$this->artisan('migrate:actions', ['--path' => $path])->run();
220+
221+
$this->assertDatabaseCount($table, 1);
222+
$this->assertDatabaseCount($this->table, 1);
223+
$this->assertDatabaseMigrationHas($this->table, 'baz');
224+
}
225+
226+
public function testPathAsFileWithoutExtension()
227+
{
228+
$this->copyFiles();
229+
230+
$table = 'test';
231+
232+
$path = 'sub_path/2021_12_15_205804_baz';
233+
234+
$this->artisan('migrate:actions:install')->run();
235+
236+
$this->assertDatabaseCount($table, 0);
237+
$this->assertDatabaseCount($this->table, 0);
238+
$this->assertDatabaseMigrationDoesntLike($this->table, 'baz');
239+
$this->artisan('migrate:actions', ['--path' => $path])->run();
240+
241+
$this->assertDatabaseCount($table, 1);
242+
$this->assertDatabaseCount($this->table, 1);
243+
$this->assertDatabaseMigrationHas($this->table, 'baz');
244+
}
245+
246+
public function testPathAsDirectory()
247+
{
248+
$this->copyFiles();
249+
250+
$table = 'test';
251+
252+
$path = 'sub_path';
253+
254+
$this->artisan('migrate:actions:install')->run();
255+
256+
$this->assertDatabaseCount($table, 0);
257+
$this->assertDatabaseCount($this->table, 0);
258+
$this->assertDatabaseMigrationDoesntLike($this->table, 'baz');
259+
$this->artisan('migrate:actions', ['--path' => $path])->run();
260+
261+
$this->assertDatabaseCount($table, 1);
262+
$this->assertDatabaseCount($this->table, 1);
263+
$this->assertDatabaseMigrationHas($this->table, 'baz');
264+
}
265+
207266
public function testMigrationNotFound()
208267
{
209268
$this->assertDatabaseDoesntTable($this->table);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use DragonCode\LaravelActions\Support\Actionable;
4+
use Illuminate\Database\Query\Builder;
5+
use Illuminate\Support\Facades\DB;
6+
use Ramsey\Uuid\Uuid;
7+
8+
class Baz extends Actionable
9+
{
10+
public function up(): void
11+
{
12+
$this->table()->insert([
13+
'value' => Uuid::uuid4(),
14+
]);
15+
}
16+
17+
public function down(): void
18+
{
19+
$this->table()->truncate();
20+
}
21+
22+
protected function table(): Builder
23+
{
24+
return DB::table('test');
25+
}
26+
}

0 commit comments

Comments
 (0)