ziming/laravel-specific-migrate-fresh

仅针对特定表进行迁移刷新

0.2 2024-04-02 04:15 UTC

This package is auto-updated.

Last update: 2024-09-08 17:35:33 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

想使用php artisan migrate:fresh但不删除所有表?这就是您要找的。

支持我

欢迎捐赠。

安装

您可以通过composer安装此包

composer require ziming/laravel-specific-migrate-fresh

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="specific-migrate-fresh-config"

这是已发布配置文件的内容

return [

    /*
     * There are 2 modes 'exclude' and 'include'.
     *
     * 'exclude' mode will drop all tables except the tables you specified in the 'excluded_tables' array.
     * 'include' mode will drop only the tables you specified in the 'included_tables' array.
     */
    'mode' => env('SPECIFIC_MIGRATE_FRESH_MODE', 'exclude'),

    /*
     * This will be used if mode is 'exclude'.
     *
     * If mode is 'exclude', the database tables in this array will be excluded
     * from getting dropped.
     */
    'excluded_tables' => [
        //
    ],

    /*
     * This will be used if mode is 'include'.
     *
     * If mode is 'include', only the database tables in this array will be dropped
     */
    'included_tables' => [
        //
    ],
];

使用方法

转到配置文件,选择您喜欢的模式('exclude' 或 'include')。

填写相关数组('excluded_tables' 或 'included_tables')。

转到您的迁移文件,对于您不想删除的表,在up()方法中添加一个条件检查,如果表仍然存在,则跳过迁移。您需要这样做,以便php artisan migrate不会在稍后抛出关于表或列已存在的错误。

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('some_giant_table_i_do_not_want_to_drop')) {
            return;
        }

        Schema::create('some_giant_table_i_do_not_want_to_drop', function (Blueprint $table) {
            $table->id();
            // Other columns
        }
}

然后调用以下命令。--seed选项是可选的。

php artisan migrate:specific-fresh --seed

此命令的末尾只是简单地调用php artisan migrate --seed命令。

如果您不想调用您的DatabaseSeeder,请从您的命令中省略--seed

php artisan migrate:specific-fresh

测试

欢迎在此处提交PR

composer test

变更日志

请参阅CHANGELOG了解最近更改的详细信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全漏洞

请查看我们的安全策略以了解如何报告安全漏洞。

鸣谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。