Skip to content

Commit d437824

Browse files
Merge pull request #66 from TheDragonCode/2.x
Added split launch option
2 parents a2176df + 80c8f6d commit d437824

12 files changed

+419
-32
lines changed

README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
[![Github Workflow Status][badge_build]][link_build]
99
[![License][badge_license]][link_license]
1010

11-
> Actions are like version control for your migration process, allowing your team to modify and share the application's actionable schema. If you have ever had to tell a teammate to manually perform any action on a producton server, you've come across an issue that actions solves.
11+
> Actions are like version control for your migration process, allowing your team to modify and share the application's actionable schema. If you have ever had to tell a teammate
12+
> to manually perform any action on a producton server, you've come across an issue that actions solves.
1213
1314
## Installation
1415

@@ -23,7 +24,7 @@ Or manually update `require` block of `composer.json` and run `composer update`.
2324
```json
2425
{
2526
"require": {
26-
"dragon-code/laravel-migration-actions": "^2.6"
27+
"dragon-code/laravel-migration-actions": "^2.9"
2728
}
2829
}
2930
```
@@ -253,6 +254,57 @@ return new class extends Actionable
253254

254255
By default, no actions will be excluded. The same happens if you specify `null` or `[]` value.
255256

257+
#### Split Launch Option
258+
259+
Sometimes it becomes necessary to launch actions separately, for example, to notify about the successful deployment of a project.
260+
261+
There is a `before` option for this when calling actions:
262+
263+
```bash
264+
php artisan migrate:actions --before
265+
```
266+
267+
When calling the `migrate:actions` command with the `before` parameter, the script will execute only those actions within which the value of the `before` parameter is `true`.
268+
269+
For backwards compatibility, the `before` parameter is set to `true` by default, but actions will only be executed if the option is explicitly passed.
270+
271+
```php
272+
use DragonCode\LaravelActions\Support\Actionable;
273+
274+
return new class extends Actionable
275+
{
276+
protected $before = false;
277+
278+
public function up(): void
279+
{
280+
// your code
281+
}
282+
};
283+
```
284+
285+
For example, you need to call actions when deploying an application. Some actions should be run after the migrations are deployed, and others after the application is fully
286+
launched.
287+
288+
To run, you need to pass the `before` parameter. For example, when using [`deployer`](https://github.com/deployphp/deployer) it would look like this:
289+
290+
```php
291+
task('deploy', [
292+
// ...
293+
'artisan:migrate',
294+
'artisan:migrate:actions --before', // here
295+
'deploy:publish',
296+
'php-fpm:reload',
297+
'artisan:queue:restart',
298+
'artisan:migrate:actions', // here
299+
]);
300+
```
301+
302+
Thus, when `migrate:actions` is called, all actions whose `before` parameter is `true` will be executed, and after that, the remaining tasks will be executed.
303+
304+
> Note:
305+
> If you call the `migrate:actions` command without the `before` parameter, then all tasks will be executed regardless of the value of the `$before` attribute inside the action
306+
> class.
307+
256308
#### Database Transactions
257309

258310
In some cases, it becomes necessary to undo previously performed actions in the database. For example, when code execution throws an error. To do this, the code must be wrapped in

src/Concerns/Optionable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
/** @mixin \Illuminate\Console\Command */
88
trait Optionable
99
{
10+
protected function optionBefore(): bool
11+
{
12+
return $this->input->getOption('before');
13+
}
14+
1015
protected function optionDatabase(): ?string
1116
{
1217
return $this->input->getOption('database');

src/Console/Migrate.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ class Migrate extends BaseCommand
2020
* @var string
2121
*/
2222
protected $signature = Names::MIGRATE
23-
. ' {--database= : The database connection to use}'
24-
. ' {--force : Force the operation to run when in production}'
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}';
23+
. ' {--database= : The database connection to use}'
24+
. ' {--force : Force the operation to run when in production}'
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}'
28+
. ' {--before : Run actions marked as before}';
2829

2930
/**
3031
* The console command description.
@@ -49,7 +50,8 @@ public function handle()
4950

5051
$this->migrator->setOutput($this->output)
5152
->run($this->getMigrationPaths(), [
52-
'step' => $this->optionStep(),
53+
'step' => $this->optionStep(),
54+
'before' => $this->optionBefore(),
5355
]);
5456
});
5557

src/Support/Actionable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ abstract class Actionable extends Migration implements Contract
5151
*/
5252
protected $except_environment;
5353

54+
/**
55+
* Defines a possible "pre-launch" of the action.
56+
*
57+
* @var bool
58+
*/
59+
protected $before = true;
60+
5461
/**
5562
* Reverse the actions.
5663
*/
@@ -121,6 +128,16 @@ public function allow(): bool
121128
return true;
122129
}
123130

131+
/**
132+
* Defines a possible "pre-launch" of the action.
133+
*
134+
* @return bool
135+
*/
136+
public function hasBefore(): bool
137+
{
138+
return $this->before;
139+
}
140+
124141
/**
125142
* Method to be called when the job completes successfully.
126143
*

src/Support/Migrator.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Migrator extends BaseMigrator
1414
use Infoable;
1515
use Anonymous;
1616

17+
protected $is_before = false;
18+
1719
public function usingConnection($name, callable $callback)
1820
{
1921
$prev = $this->resolver->getDefaultConnection();
@@ -25,6 +27,13 @@ public function usingConnection($name, callable $callback)
2527
});
2628
}
2729

30+
public function runPending(array $migrations, array $options = [])
31+
{
32+
$this->is_before = $options['before'] ?? false;
33+
34+
return parent::runPending($migrations, $options);
35+
}
36+
2837
/**
2938
* Run "up" a migration instance.
3039
*
@@ -45,7 +54,7 @@ protected function runUp($file, $batch, $pretend)
4554
$name = $this->getMigrationName($file);
4655
} else {
4756
$migration = $this->resolve(
48-
$name = $this->getMigrationName($file)
57+
$name = $this->getMigrationName($file)
4958
);
5059
}
5160

@@ -55,6 +64,12 @@ protected function runUp($file, $batch, $pretend)
5564
return;
5665
}
5766

67+
if ($this->disallowBefore($migration)) {
68+
$this->note("<info>Migrate:</info> {$name} was omitted because the 'before' parameter is enabled.");
69+
70+
return;
71+
}
72+
5873
if ($pretend) {
5974
$this->pretendToRun($migration, 'up');
6075

@@ -196,6 +211,18 @@ protected function transactionAttempts(ActionableContract $migration): int
196211
return (int) abs($value);
197212
}
198213

214+
/**
215+
* Defines a possible "pre-launch" of the action.
216+
*
217+
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
218+
*
219+
* @return bool
220+
*/
221+
protected function disallowBefore(ActionableContract $migration): bool
222+
{
223+
return $this->is_before && ! $migration->hasBefore();
224+
}
225+
199226
/**
200227
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
201228
* @param callable $handle

0 commit comments

Comments
 (0)