Blog Detail

07

Feb
Perform different Actions with Laravel Migration Actions  cover image

arrow_back Perform different Actions with Laravel Migration Actions

Laravel Migration Actions is a package by The Dragon Code that offers you the opportunity to Perform actions by saving the list of called files. 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 production server, you’ve come across an issue that actions solve.

Installation

To get the latest version of Laravel Actions, simply require the project using Composer:

$ composer require dragon-code/laravel-migration-actions

Or manually update require a block of composer.json and run composer update.

{
    "require": {
        "dragon-code/laravel-migration-actions": "^2.6"
    }
}

How To Use

Creating Actions

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, the migrate:actions:install command will be called automatically.

Automatically Generate A File Name

If you are not worried about the names of your files, then in version 2.6 we added the ability to automatically generate file names.

Just don’t include the name attribute when creating the migration.

If a git repository is found in the main folder, then the name of the current active branch will be taken as a prefix:

php artisan make:migration:action

### Before Laravel 8.37
# 2022_01_28_184116_main_1643384476.php
# 2022_01_28_184117_main_1643384477.php
# 2022_01_28_184118_crm_2345_1643384478.php
# 2022_01_28_184119_crm_2345_1643384479.php

### Laravel 8.37 or higher
# 2022_01_28_184116_main.php
# 2022_01_28_184117_main.php
# 2022_01_28_184118_crm_2345.php
# 2022_01_28_184119_crm_2345.php

Running actions

To run all of your outstanding actions, execute the migrate:actions Artisan command:

php artisan migrate:actions

Forcing Actions To Run In Production

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

Execution Every Time

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 DragonCode\LaravelActions\Support\Actionable;

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

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

If the value is $once = false, the up method will be called every time the migrate:actions command is 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.

This package has a lot of amazing features. If you want to dig more about this package, you can visit its complete documentation on Github.

Published at : 07-02-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project