Nwidart\Modules\Exceptions\ModuleNotFoundException : Module [Blog] does not exist!,模块中数据库迁移文件的执行(无需发布)

1、当在 Modules/Blog 下删除了文件 module.json 后,生成新的迁移文件:Nwidart\Modules\Exceptions\ModuleNotFoundException : Module [Blog] does not exist!。如图1

图1

PS E:\wwwroot\laravel6-modules-demo> php artisan module:make-migration delete_wp_is_default_theme_id_from_theme_saas_task_table Blog

   Nwidart\Modules\Exceptions\ModuleNotFoundException  : Module [Blog] does not exist!

  at E:\wwwroot\laravel6-modules-demo\vendor\nwidart\laravel-modules\src\FileRepository.php:396
    392|         if ($module !== null) {
    393|             return $module;
    394|         }
    395|
  > 396|         throw new ModuleNotFoundException("Module [{$name}] does not exist!");
    397|     }
    398|
    399|     /**
    400|      * Get all modules as laravel collection instance.

  Exception trace:

  1   Nwidart\Modules\FileRepository::findOrFail("Blog")
      E:\wwwroot\laravel6-modules-demo\vendor\nwidart\laravel-modules\src\Traits\ModuleCommandTrait.php:16

  2   Nwidart\Modules\Commands\MigrationMakeCommand::getModuleName()
      E:\wwwroot\laravel6-modules-demo\vendor\nwidart\laravel-modules\src\Commands\MigrationMakeCommand.php:115

  Please use the argument -v to see more details.

2、使用 make:migration Artisan 命令来创建迁移,使用 –path 选项以自定义生成迁移文件的个人存放路径。如图2

图2

PS E:\wwwroot\laravel6-modules-demo> php artisan make:migration delete_wp_is_default_theme_id_from_theme_saas_task_table --path=Modules/Blog/Database/Migrations
Created Migration: 2023_01_09_073351_delete_wp_is_default_theme_id_from_theme_saas_task_table
PS E:\wwwroot\laravel6-modules-demo>

3、一般在模块中的迁移是需要发布的,但是由于现在模块 Blog 已经不存在,因此,无法发布

PS E:\wwwroot\laravel6-modules-demo> php artisan module:publish-migration Blog

   Nwidart\Modules\Exceptions\ModuleNotFoundException  : Module [Blog] does not exist!

  at E:\wwwroot\laravel6-modules-demo\vendor\nwidart\laravel-modules\src\FileRepository.php:396
    392|         if ($module !== null) {
    393|             return $module;
    394|         }
    395|
  > 396|         throw new ModuleNotFoundException("Module [{$name}] does not exist!");
    397|     }
    398|
    399|     /**
    400|      * Get all modules as laravel collection instance.

  Exception trace:

  1   Nwidart\Modules\FileRepository::findOrFail("Blog")
      E:\wwwroot\laravel6-modules-demo\vendor\nwidart\laravel-modules\src\Commands\PublishMigrationCommand.php:32

  2   Nwidart\Modules\Commands\PublishMigrationCommand::handle()
      E:\wwwroot\laravel6-modules-demo\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:36

  Please use the argument -v to see more details.

4、最终决定修改 Blog 模块的 服务加载类 ServiceProvider

    /**
     *
     * @return void
     */    public function register()
    {
        // ...
        $this->registerMigrations();
        $this->registerPublishing();
 }


    /**
     * Register the package's migrations.
     *
     * @return void
     */    private function registerMigrations()
    {
        if ($this->app->runningInConsole()) {
            $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
            $this->loadFactoriesFrom(__DIR__.'/../Database/factories');
        }
    }

    /**
     * Register the package's publishable resources.
     *
     * @return void
     */    private function registerPublishing()
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__.'/../Database/Migrations' => database_path('migrations'),
            ], 'theme-store-db-migrations');
        }
    }

5、已经无需发布模块中的数据迁移文件,在执行数据库迁移时,会自动运行模块 Blog 中的数据库迁移文件。

E:\wwwroot\laravel6-modules-demo> php artisan migrate
Migrating: 2023_01_09_154319_delete_wp_is_default_theme_id_from_theme_saas_task_table
Migrated:  2023_01_09_154319_delete_wp_is_default_theme_id_from_theme_saas_task_table (0 seconds)
E:\wwwroot\laravel6-modules-demo>

6、即使待执行的数据库迁移文件,有在目录:/database/migrations,还有在模块目录:/Modules/ThemeStoreDB/Database/Migrations 中的,也会自动按照创建时间顺序执行。

PS E:\wwwroot\object> php artisan migrate
Migrating: 2022_12_20_145928_create_theme_saas_task_table
Migrated:  2022_12_20_145928_create_theme_saas_task_table (0.05 seconds)
Migrating: 2023_01_04_094154_add_from_theme_installation_ids_to_theme_saas_task_table
Migrated:  2023_01_04_094154_add_from_theme_installation_ids_to_theme_saas_task_table (0.04 seconds)
Migrating: 2023_01_09_155849_drop_wp_is_default_theme_id_from_theme_saas_task_table
Migrated:  2023_01_09_155849_drop_wp_is_default_theme_id_from_theme_saas_task_table (0.04 seconds)
PS E:\wwwroot\object>
永夜