|
| 1 | +# Running Actions |
| 2 | + |
| 3 | +To run all of your outstanding actions, execute the `migrate:actions` artisan command: |
| 4 | + |
| 5 | +```bash |
| 6 | +php artisan migrate:actions |
| 7 | +``` |
| 8 | + |
| 9 | +## Split Launch Option |
| 10 | + |
| 11 | +Sometimes it becomes necessary to launch actions separately, for example, to notify about the successful deployment of a project. |
| 12 | + |
| 13 | +There is a `before` option for this when calling actions: |
| 14 | + |
| 15 | +```bash |
| 16 | +php artisan migrate:actions --before |
| 17 | +``` |
| 18 | + |
| 19 | +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`. |
| 20 | + |
| 21 | +For backwards compatibility, the `before` parameter is set to `true` by default, but actions will only be executed if the option is explicitly passed. |
| 22 | + |
| 23 | +```php |
| 24 | +use DragonCode\LaravelActions\Action; |
| 25 | + |
| 26 | +return new class () extends Action |
| 27 | +{ |
| 28 | + protected $before = false; |
| 29 | + |
| 30 | + public function __invoke(): void |
| 31 | + { |
| 32 | + // some code |
| 33 | + } |
| 34 | +}; |
| 35 | +``` |
| 36 | + |
| 37 | +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 |
| 38 | +launched. |
| 39 | + |
| 40 | +To run, you need to pass the `before` parameter. For example, when using [`deployer`](https://github.com/deployphp/deployer) it would look like this: |
| 41 | + |
| 42 | +```php |
| 43 | +task('deploy', [ |
| 44 | + // ... |
| 45 | + 'artisan:migrate', |
| 46 | + 'artisan:migrate:actions --before', // here |
| 47 | + 'deploy:publish', |
| 48 | + 'php-fpm:reload', |
| 49 | + 'artisan:queue:restart', |
| 50 | + 'artisan:migrate:actions', // here |
| 51 | +]); |
| 52 | +``` |
| 53 | + |
| 54 | +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. |
| 55 | + |
| 56 | +> Note: |
| 57 | +> If you call the `migrate:actions` command without the `before` parameter, |
| 58 | +> then all tasks will be executed regardless of the value of the `$before` |
| 59 | +> attribute inside the action class. |
| 60 | +
|
| 61 | +## Forcing Actions To Run In Production |
| 62 | + |
| 63 | +> Some commands cannot be executed in production without confirmation. |
| 64 | +> These include all commands except `migrate:actions:status` and `migrate:actions`. |
| 65 | +
|
| 66 | +Some action operations are destructive, which means they may cause you to lose data. In order to protect you from running these commands against your production database, |
| 67 | +you will be prompted for confirmation before the commands are executed. To force the commands to run without a prompt, use the `--force` flag: |
| 68 | + |
| 69 | +```bash |
| 70 | +php artisan migrate:actions:install --force |
| 71 | +``` |
| 72 | + |
| 73 | +## Execution Every Time |
| 74 | + |
| 75 | +In some cases, you need to call the code every time you deploy the application. For example, to call reindexing. |
| 76 | + |
| 77 | +To do this, override the `$once` variable in the action file: |
| 78 | + |
| 79 | +```php |
| 80 | +use DragonCode\LaravelActions\Action; |
| 81 | + |
| 82 | +return new class () extends Action |
| 83 | +{ |
| 84 | + protected $once = false; |
| 85 | + |
| 86 | + public function __invoke(): void |
| 87 | + { |
| 88 | + // some code |
| 89 | + } |
| 90 | +}; |
| 91 | +``` |
| 92 | + |
| 93 | +If the value is `$once = false`, the `up` method will be called every time the `migrate:actions` command called. |
| 94 | + |
| 95 | +In this case, information about it will not be written to the `migration_actions` table and, therefore, the `down` method will not be called when the rollback command is called. |
| 96 | + |
| 97 | +> Note |
| 98 | +> |
| 99 | +> When using the `before` parameter to run command, it is recommended to override the value of the `$before` attribute to `false`, otherwise this action will be executed twice. |
| 100 | +
|
| 101 | +## Execution In A Specific Environment |
| 102 | + |
| 103 | +In some cases, it becomes necessary to execute an action in a specific environment. For example `production`. |
| 104 | + |
| 105 | +For this you can use the `$environment` parameter: |
| 106 | + |
| 107 | +```php |
| 108 | +use DragonCode\LaravelActions\Action; |
| 109 | + |
| 110 | +return new class () extends Action |
| 111 | +{ |
| 112 | + /** @var string|array|null */ |
| 113 | + protected $environment = 'production'; |
| 114 | + |
| 115 | + public function __invoke(): void |
| 116 | + { |
| 117 | + // some code |
| 118 | + } |
| 119 | +}; |
| 120 | +``` |
| 121 | + |
| 122 | +You can also specify multiple environment names: |
| 123 | + |
| 124 | +```php |
| 125 | +use DragonCode\LaravelActions\Action; |
| 126 | + |
| 127 | +return new class () extends Action |
| 128 | +{ |
| 129 | + /** @var string|array|null */ |
| 130 | + protected $environment = ['testing', 'staging']; |
| 131 | + |
| 132 | + public function __invoke(): void |
| 133 | + { |
| 134 | + // some code |
| 135 | + } |
| 136 | +}; |
| 137 | +``` |
| 138 | + |
| 139 | +By default, the action will run in all environments. The same will happen if you specify `null` or `[]` as the value. |
| 140 | + |
| 141 | +## Execution Excluding Certain Environments |
| 142 | + |
| 143 | +In some cases, it becomes necessary to execute an action excluding certain environments. For example `production`. |
| 144 | + |
| 145 | +For this you can use the `$except_environment` parameter: |
| 146 | + |
| 147 | +```php |
| 148 | +use DragonCode\LaravelActions\Action; |
| 149 | + |
| 150 | +return new class () extends Action |
| 151 | +{ |
| 152 | + /** @var string|array|null */ |
| 153 | + protected $except_environment = 'production'; |
| 154 | + |
| 155 | + public function __invoke(): void |
| 156 | + { |
| 157 | + // some code |
| 158 | + } |
| 159 | +}; |
| 160 | +``` |
| 161 | + |
| 162 | +You can also specify multiple environment names: |
| 163 | + |
| 164 | +```php |
| 165 | +use DragonCode\LaravelActions\Action; |
| 166 | + |
| 167 | +return new class () extends Action |
| 168 | +{ |
| 169 | + /** @var string|array|null */ |
| 170 | + protected $except_environment = ['testing', 'staging']; |
| 171 | + |
| 172 | + public function __invoke(): void |
| 173 | + { |
| 174 | + // some code |
| 175 | + } |
| 176 | +}; |
| 177 | +``` |
| 178 | + |
| 179 | +By default, no actions will be excluded. The same happens if you specify `null` or `[]` value. |
| 180 | + |
| 181 | +## Database Transactions |
| 182 | + |
| 183 | +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 |
| 184 | +a transaction. |
| 185 | + |
| 186 | +By setting the `$transactions = true` parameter, you will ensure that your code is wrapped in a transaction without having to manually call the `DB::transaction()` method. This |
| 187 | +will reduce the time it takes to create the action. |
| 188 | + |
| 189 | +```php |
| 190 | +use DragonCode\LaravelActions\Action; |
| 191 | + |
| 192 | +return new class () extends Action |
| 193 | +{ |
| 194 | + protected $transactions = true; |
| 195 | + |
| 196 | + protected $transactionAttempts = 3; |
| 197 | + |
| 198 | + public function __invoke(): void |
| 199 | + { |
| 200 | + // some code |
| 201 | + } |
| 202 | +}; |
| 203 | +``` |
0 commit comments