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.
To get the latest version of Laravel Actions, simply require the project using Composer:
$ composer require andrey-helldar/laravel-actions
Or manually update require
block of composer.json
and run composer update
.
{
"require-dev": {
"andrey-helldar/laravel-actions": "^1.1"
}
}
You can also publish the config file to change implementations (ie. interface to specific class):
php artisan vendor:publish --provider="Helldar\LaravelActions\ServiceProvider"
This package is focused on Laravel development, but it can also be used in Lumen with some workarounds. Because Lumen works a little different, as it is like a
barebone version of Laravel and the main configuration parameters are instead located in bootstrap/app.php
, some alterations must be made.
You can install Laravel Lang Publisher in app/Providers/AppServiceProvider.php
, and uncommenting this line that registers the App Service Providers so it can
properly load.
// $app->register(App\Providers\AppServiceProvider::class);
If you are not using that line, that is usually handy to manage gracefully multiple Lumen installations, you will have to add this line of code under
the Register Service Providers
section of your bootstrap/app.php
.
$app->register(\Helldar\LaravelActions\ServiceProvider::class);
To create a migration, use the make:migration:action
Artisan command:
php artisan make:migration:action my_action
The new action will be placed in your database/actions
directory. Each action file name contains a timestamp, which allows Laravel to determine the order of
the actions.
At the first start, you need to create a table by running the
migrate:actions:install
command.If you execute
migrate:actions
with the first command, themigrate:actions:install
command will be called automatically.
To run all of your outstanding actions, execute the migrate:actions
Artisan command:
php artisan migrate:actions
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, you will be prompted for confirmation before the commands are executed. To force the commands to run without a prompt, use the --force
flag:
php artisan migrate:actions --force
In some cases, you need to call the code every time you deploy the application. For example, to call reindexing.
To do this, override the $once
variable in the action file:
use Helldar\LaravelActions\Support\Actionable;
class Reindex extends Actionable
{
/**
* Determines the type of launch of the action.
*
* If true, then it will be executed once.
* If false, then the action will run every time the `migrate:actions` command is invoked.
*
* @var bool
*/
protected $once = false;
public function up(): void
{
// your calling code
}
public function down(): void
{
//
}
}
If the value is $once = false
, the up
method will be called every time the migrate:actions
command called.
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.
To roll back the latest action operation, you may use the rollback
command. This command rolls back the last "batch" of actions, which may include multiple
action files:
php artisan migrate:actions:rollback
You may roll back a limited number of actions by providing the step
option to the rollback command. For example, the following command will roll back the last
five actions:
php artisan migrate:actions:rollback --step=5
The migrate:actions:reset
command will roll back all of your application's migrations:
php artisan migrate:actions:reset
The migrate:actions:refresh
command will roll back all of your migrations and then execute the migrate:actions
command. This command effectively re-creates
your entire database:
php artisan migrate:actions:refresh
You may roll back & re-migrate a limited number of migrations by providing the step
option to the refresh
command. For example, the following command will
roll back & re-migrate the last five migrations:
php artisan migrate:actions:refresh --step=5
The migrate:actions:status
command displays the execution status of actions. In it you can see which actions were executed and which were not:
php artisan migrate:actions:status