LoginSignup
1
1

More than 1 year has passed since last update.

Laravelのartisanでmigrate:rollback時にstepを必須にする

Last updated at Posted at 2022-03-05

概要

本番環境でやらかさない為に。

php artisan実行時に
・migrate:rollbackする時にstep指定を必須
・migrate:fresh|resetを実行禁止

Laravel:8.81
PHP:8.1

参考

こちらの記事から追記・修正させて頂きました
https://cpoint-lab.co.jp/article/202010/17274/

コード

App/Console/Kernel
namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Str;
class Kernel extends ConsoleKernel
{

    // コマンド一部禁止
    public function handle($input, $output = null)
    {
        $this->bootstrap();// Laravel 関連の色々を使うために実行するべきメソッド

        // 指定ワードが含まれている場合に色々と処理
        // @see https://cpoint-lab.co.jp/article/202010/17274/
        foreach ($_SERVER['argv'] as $arg) {
            // reset関連
            if (Str::contains($arg, 'migrate:fresh') || Str::contains($arg, 'migrate:reset')) {
                // dd でコマンド実行前に処理を打ち切り
                dd('STOP! Don\'t such a command.');
            }

            // rollback はstep指定必須
            if(Str::contains($arg, 'migrate:rollback')){
                $step_contain_flag = false;
                foreach ($_SERVER['argv'] as $arg2) {
                    if(str_contains($arg2, 'step')){
                        $step_contain_flag = true;
                    }
                }
                if($step_contain_flag === false){
                    dd('STOP! Rollback must "step"!');
                }
            }
        }

        // 特別打ち切りをする必要がないならば予定されていた処理を実行
        return parent::handle($input, $output);
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1