diff --git a/config.php b/config.php index 95b6dbd..5b15023 100644 --- a/config.php +++ b/config.php @@ -46,6 +46,21 @@ */ 'modules_directory' => 'app-modules', + + /* + |-------------------------------------------------------------------------- + | Migrations + |-------------------------------------------------------------------------- + | + | Here you can specify which modules should be ignored by the migrator. + | This is useful if you want to disable migrations for a specific + | module. + | + */ + + 'migrations' => [ + 'ignore' => [], + ], /* |-------------------------------------------------------------------------- diff --git a/src/Support/ModularServiceProvider.php b/src/Support/ModularServiceProvider.php index 22120ca..53ea4f7 100644 --- a/src/Support/ModularServiceProvider.php +++ b/src/Support/ModularServiceProvider.php @@ -234,6 +234,10 @@ protected function registerMigrations(Migrator $migrator): void { $this->autoDiscoveryHelper() ->migrationDirectoryFinder() + ->reject(function (SplFileInfo $path) { + $module = $this->registry()->moduleForPathOrFail($path->getPath()); + return in_array($module->name, Config::get('app-modules.migrations.ignore', [])); + }) ->each(function(SplFileInfo $path) use ($migrator) { $migrator->path($path->getRealPath()); }); diff --git a/tests/ModularServiceProviderTest.php b/tests/ModularServiceProviderTest.php index 5d4c74f..359f39b 100644 --- a/tests/ModularServiceProviderTest.php +++ b/tests/ModularServiceProviderTest.php @@ -3,6 +3,7 @@ namespace InterNACHI\Modular\Tests; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Facades\DB; use InterNACHI\Modular\Support\ModuleRegistry; use InterNACHI\Modular\Tests\Concerns\WritesToAppFilesystem; @@ -163,4 +164,27 @@ public function test_it_loads_translations_from_module(): void $this->assertEquals('Test JSON translation', $translator->get('Test JSON string')); $this->assertEquals('Test PHP translation', $translator->get('test-module::foo.bar')); } + + public function test_it_does_not_load_migrations_from_ignored_modules(): void + { + $this->makeModule('module-a'); + $this->makeModule('module-b'); + + $this->artisan('make:migration', ['name' => 'create_module_a_table', '--module' => 'module-a']); + $this->artisan('make:migration', ['name' => 'create_module_b_table', '--module' => 'module-b']); + + $this->app['config']->set('app-modules.migrations.ignore', ['module-b']); + + $this->artisan('migrate'); + + $migrations = DB::table('migrations')->get()->pluck('migration'); + + $this->assertTrue($migrations->contains(function ($migration) { + return str_contains($migration, 'create_module_a_table'); + })); + + $this->assertFalse($migrations->contains(function ($migration) { + return str_contains($migration, 'create_module_b_table'); + })); + } }