定制 noouh/laravel-multiple-migrations 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

noouh/laravel-multiple-migrations

最新稳定版本:v0.1

Composer 安装命令:

composer require noouh/laravel-multiple-migrations

包简介

A Laravel package to run migrations from multiple paths.

README 文档

README

A Laravel package to run migrations from multiple paths.

Installation

composer require noouh/laravel-multiple-migrations

Usage

php artisan migrate:multiple database/migrations

License

MIT

To ensure that the command skips any migrations that fail or if the table already exists, you can wrap the migration call in a try-catch block. Here's how you can modify the command class to handle this:

Updated Command Class

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Exception;

class MigrateMultiplePaths extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrate:multiple {paths*}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run migrations from multiple paths';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $paths = $this->argument('paths');

        foreach ($paths as $path) {
            $this->info("Running migrations in: $path");

            $migrationFiles = $this->getMigrationFiles($path);

            foreach ($migrationFiles as $file) {
                $this->info("Running migration: $file");
                try {
                    $this->call('migrate', [
                        '--path' => $file,
                    ]);
                } catch (Exception $e) {
                    $this->error("Failed to run migration: $file. Error: " . $e->getMessage());
                }
            }
        }

        return 0;
    }

    /**
     * Get all migration files from the specified directory.
     *
     * @param string $path
     * @return array
     */
    protected function getMigrationFiles($path)
    {
        $files = [];

        if (File::isDirectory($path)) {
            $files = File::allFiles($path);
        }

        return array_map(function ($file) use ($path) {
            return $path . '/' . $file->getFilename();
        }, $files);
    }
}

Explanation

  • Try-Catch Block: Each migration is wrapped in a try-catch block to handle any exceptions that occur during the migration. If an exception is caught, an error message is logged, and the command proceeds to the next migration file.
  • Error Handling: If a table already exists or any other error occurs, it will be caught and skipped without stopping the execution of the remaining migrations.

Register the Command

Ensure the command is registered in the app/Console/Kernel.php file:

protected $commands = [
    \App\Console\Commands\MigrateMultiplePaths::class,
];

Running the Command

You can run the custom command and pass the path to the extracted migrations:

php artisan migrate:multiple /mnt/data/extracted

This command will detect all migration files in the specified directory, attempt to run them, and skip any that fail or if the table already exists.

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-01