Skip to content

Added split launch option #66

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
Aug 17, 2022
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
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[![Github Workflow Status][badge_build]][link_build]
[![License][badge_license]][link_license]

> 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.
> 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.

## Installation

Expand All @@ -23,7 +24,7 @@ Or manually update `require` block of `composer.json` and run `composer update`.
```json
{
"require": {
"dragon-code/laravel-migration-actions": "^2.6"
"dragon-code/laravel-migration-actions": "^2.9"
}
}
```
Expand Down Expand Up @@ -253,6 +254,57 @@ return new class extends Actionable

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

#### Split Launch Option

Sometimes it becomes necessary to launch actions separately, for example, to notify about the successful deployment of a project.

There is a `before` option for this when calling actions:

```bash
php artisan migrate:actions --before
```

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`.

For backwards compatibility, the `before` parameter is set to `true` by default, but actions will only be executed if the option is explicitly passed.

```php
use DragonCode\LaravelActions\Support\Actionable;

return new class extends Actionable
{
protected $before = false;

public function up(): void
{
// your code
}
};
```

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
launched.

To run, you need to pass the `before` parameter. For example, when using [`deployer`](https://github.com/deployphp/deployer) it would look like this:

```php
task('deploy', [
// ...
'artisan:migrate',
'artisan:migrate:actions --before', // here
'deploy:publish',
'php-fpm:reload',
'artisan:queue:restart',
'artisan:migrate:actions', // here
]);
```

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.

> Note:
> 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
> class.

#### Database Transactions

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
Expand Down
5 changes: 5 additions & 0 deletions src/Concerns/Optionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
/** @mixin \Illuminate\Console\Command */
trait Optionable
{
protected function optionBefore(): bool
{
return $this->input->getOption('before');
}

protected function optionDatabase(): ?string
{
return $this->input->getOption('database');
Expand Down
14 changes: 8 additions & 6 deletions src/Console/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ class Migrate extends BaseCommand
* @var string
*/
protected $signature = Names::MIGRATE
. ' {--database= : The database connection to use}'
. ' {--force : Force the operation to run when in production}'
. ' {--step : Force the actions to be run so they can be rolled back individually}'
. ' {--path=* : The path(s) to the migrations files to be executed}'
. ' {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}';
. ' {--database= : The database connection to use}'
. ' {--force : Force the operation to run when in production}'
. ' {--step : Force the actions to be run so they can be rolled back individually}'
. ' {--path=* : The path(s) to the migrations files to be executed}'
. ' {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}'
. ' {--before : Run actions marked as before}';

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

$this->migrator->setOutput($this->output)
->run($this->getMigrationPaths(), [
'step' => $this->optionStep(),
'step' => $this->optionStep(),
'before' => $this->optionBefore(),
]);
});

Expand Down
17 changes: 17 additions & 0 deletions src/Support/Actionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ abstract class Actionable extends Migration implements Contract
*/
protected $except_environment;

/**
* Defines a possible "pre-launch" of the action.
*
* @var bool
*/
protected $before = true;

/**
* Reverse the actions.
*/
Expand Down Expand Up @@ -121,6 +128,16 @@ public function allow(): bool
return true;
}

/**
* Defines a possible "pre-launch" of the action.
*
* @return bool
*/
public function hasBefore(): bool
{
return $this->before;
}

/**
* Method to be called when the job completes successfully.
*
Expand Down
29 changes: 28 additions & 1 deletion src/Support/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Migrator extends BaseMigrator
use Infoable;
use Anonymous;

protected $is_before = false;

public function usingConnection($name, callable $callback)
{
$prev = $this->resolver->getDefaultConnection();
Expand All @@ -25,6 +27,13 @@ public function usingConnection($name, callable $callback)
});
}

public function runPending(array $migrations, array $options = [])
{
$this->is_before = $options['before'] ?? false;

return parent::runPending($migrations, $options);
}

/**
* Run "up" a migration instance.
*
Expand All @@ -45,7 +54,7 @@ protected function runUp($file, $batch, $pretend)
$name = $this->getMigrationName($file);
} else {
$migration = $this->resolve(
$name = $this->getMigrationName($file)
$name = $this->getMigrationName($file)
);
}

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

if ($this->disallowBefore($migration)) {
$this->note("<info>Migrate:</info> {$name} was omitted because the 'before' parameter is enabled.");

return;
}

if ($pretend) {
$this->pretendToRun($migration, 'up');

Expand Down Expand Up @@ -196,6 +211,18 @@ protected function transactionAttempts(ActionableContract $migration): int
return (int) abs($value);
}

/**
* Defines a possible "pre-launch" of the action.
*
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
*
* @return bool
*/
protected function disallowBefore(ActionableContract $migration): bool
{
return $this->is_before && ! $migration->hasBefore();
}

/**
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
* @param callable $handle
Expand Down
Loading