dentro/laravel-patcher

Laravel 补丁脚本。

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

This package is auto-updated.

Last update: 2024-09-03 07:14:10 UTC


README

一个类似迁移的补丁工具,用于生产更新。

Total Downloads GitHub Workflow Status

要求

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

安装

以下任一方法均可。

  • 通过 shell
composer require dentro/laravel-patcher
  • "dentro/laravel-patcher": "^1.0" 添加到 composer.json
{
  "require": {
    "dentro/laravel-patcher": "^1.0"
  }
}

安装后

此过程是可选的,您可以跳过。

创建 patches 表

 php artisan patcher:install

使用方法

创建新补丁

要创建新补丁,需要运行以下命令

php artisan make:patch what_do_you_want_to_patch

运行该命令后,您将在 patches 文件夹中看到一个新文件。该文件将类似于

<?php

use Dentro\Patcher\Patch;

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

该文件的 patch 方法将填充您的逻辑。在 Dentro\Patcher\Patch 中,有一些有用的属性可以帮助您支持补丁,例如

  1. $container: \Illuminate\Container\Container

  2. $command: \Illuminate\Console\Command

    我们经常使用 $command 属性来打印我们正在执行的过程。示例

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

    您可以在 这里 了解更多关于 \Illuminate\Console\Command 的信息。

  3. $logger: \Illuminate\Log\Logger

    $logger 将日志存储在 storage/logs/patches.log 中。如果您想更改它,请在您的 config/logging.php 中的 channels 部分添加以下行。

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

    您可以在 这里 了解更多关于 \Illuminate\Log\Logger 的信息。

显示补丁状态

php artisan patcher:status

示例

➜ 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     |
+------+---------------------------------------+-------+

运行挂起的补丁

php artisan patcher:run

示例

➜ 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)

条件补丁

在运行 php artisan patcher:run 时,您可能需要跳过单个补丁。由于补丁不必要,或者在您的环境中不适用。在这里,您可以在补丁类中添加 eligible 方法来评估在运行 patch 方法之前的条件。

<?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.
    }
}

然后,php artisan patcher:run 的输出将是

➜ 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)

永久补丁

在某些情况下,您可能还需要无限期地运行补丁脚本。您可以将补丁文件中的 isPerpetual 属性更改为 true

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