dentro/laravel-patcher

Laravel patch scripts.

v1.4.2 2024-04-03 06:19 UTC

This package is auto-updated.

Last update: 2024-11-03 07:31:12 UTC


README

A (migration like) patcher for a smoldering production update.

Total Downloads GitHub Workflow Status

Requirements:

  • PHP : 8.*
  • Laravel: 9.* / 10.* / 11.*

Installation

Do either of these methods below.

  • via shell
composer require dentro/laravel-patcher
  • adding "dentro/laravel-patcher": "^1.0" to composer.json
{
  "require": {
    "dentro/laravel-patcher": "^1.0"
  }
}

Post Installation

this process is optional, you can skip it though.

patches table creation.

 php artisan patcher:install

Usage

Create New Patch

for creating a new patch, you need to run these following command

php artisan make:patch what_do_you_want_to_patch

After run that command, you will see new file in patches folder. That file will be like:

<?php

use Dentro\Patcher\Patch;

class WhatDoYouWantToPatch extends Patch
{
    public function patch()
    {
        // 
    }
}

Method patch on this file will be filled with your logic. in Dentro\Patcher\Patch there is some useful properties that you can use for supporting your patch such as:

  1. $container: \Illuminate\Container\Container

  2. $command: \Illuminate\Console\Command

    we frequently used $command property to print process that we're doing. example:

    $this->command->warn('i patch something danger!');
    $this->command->confirm('do you wish to continue?');

    you can learn more about \Illuminate\Console\Command here.

  3. $logger: \Illuminate\Log\Logger

    $logger will store log in storage/logs/patches.log. if you want to change it, add this line below in your config/logging.php in channels section.

    [
        'channels' => [
            'patcher' => [
                 'driver' => 'patcher', // you can change me if you want
                 'path' => storage_path('logs/patches.log'), // change me
             ],
        ],
    ];

    you can learn more about \Illuminate\Log\Logger here

Show Patch Status

php artisan patcher:status

Example:

➜ php artisan patcher:status
+------+---------------------------------------+-------+
| Ran? | Patch                                 | Batch |
+------+---------------------------------------+-------+
| Yes  | 2020_09_29_190531_fix_double_sections | 1     |
| Yes  | 2020_10_09_124616_add_attachment_beep | 1     |
+------+---------------------------------------+-------+

Run Pending Patch(es)

php artisan patcher:run

Example:

➜ php artisan patcher:run
Patches table created successfully.
Patching: 2020_09_29_190531_fix_double_sections
Patched:  2020_09_29_190531_fix_double_sections (0.03 seconds)
Patching: 2020_10_09_124616_add_attachment_beep
Patched:  2020_10_09_124616_add_attachment_beep (0.06 seconds)

Conditional Patch

You might need to skip single patch when run php artisan patcher:run. Due to patch is unnecessary or patch is not eligible to run in your environment. Here you can add the eligible method to your patch class to evaluate the condition before running the patch method.

<?php

use Dentro\Patcher\Patch;
use App\Models\User;

class WhatDoYouWantToPatch extends Patch
{
    public function eligible(): bool
    {
        return User::query()->where('id', 331)->exists();
    }
    
    public function patch()
    {
        $user = User::query()->find(331);
        // do something with user.
    }
}

then the output of php artisan patcher:run will be:

➜ php artisan patcher:run
Patching: 2020_09_29_190531_fix_double_sections
Skipped:  2020_09_29_190531_fix_double_sections is not eligible to run in current condition.
Patching: 2020_10_09_124616_add_attachment_beep
Patched:  2020_10_09_124616_add_attachment_beep (0.06 seconds)

Perpetual Patch

In some cases you might also want to run patches script indefinitely, you can change isPerpetual property on your patch file to true

class WhatDoYouWantToPatch extends Patch
{
    public bool $isPerpetual = true;
}