没有不值得去解决的问题,也没有不值得去学习的技术!

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

当在 Modules/Blog 下删除了文件 module.json 后,生成新的迁移文件:Nwidart\Modules\Exceptions\ModuleNotFoundException : Module [Blog] does not exist!
1、当在 Modules/Blog 下删除了文件 module.json 后,生成新的迁移文件:Nwidart\Modules\Exceptions\ModuleNotFoundException : Module [Blog] does not exist!。如图1
当在 Modules/Blog 下删除了文件 module.json 后,生成新的迁移文件:Nwidart\Modules\Exceptions\ModuleNotFoundException  : Module [Blog] does not exist!
图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
使用 make:migration Artisan 命令来创建迁移,使用 --path 选项以自定义生成迁移文件的个人存放路径
图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>


PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理