diff --git a/.github/README.md b/.github/README.md index 1f19842..94d38e3 100644 --- a/.github/README.md +++ b/.github/README.md @@ -71,7 +71,22 @@ protected function getMigrationChecksumFile(): string return storage_path('custom/some-other-file.txt'); } ``` - + +## Customising the migration path locations + +You may customise the migration path locations by extending the trait and overwriting the `getCustomMigrationPaths()` method. + +```php +protected function getCustomMigrationPaths(): array +{ + return [ + database_path('other-migrations'), + ]; +} +``` + +If you require full control over the migration paths, you may overwrite the `getMigrationPaths()` method. + ## Known Issues ### ParaTest Databases diff --git a/src/Traits/FastRefreshDatabase.php b/src/Traits/FastRefreshDatabase.php index 9bc56ec..829dc43 100644 --- a/src/Traits/FastRefreshDatabase.php +++ b/src/Traits/FastRefreshDatabase.php @@ -20,7 +20,7 @@ trait FastRefreshDatabase * @return void * @throws \JsonException */ - protected function refreshTestDatabase() + protected function refreshTestDatabase(): void { if (! RefreshDatabaseState::$migrated) { $cachedChecksum = FastRefreshDatabaseState::$cachedChecksum ??= $this->getCachedMigrationChecksum(); @@ -48,30 +48,32 @@ protected function refreshTestDatabase() */ protected function calculateMigrationChecksum(): string { + // Filter out non-existing paths + $paths = collect($this->getMigrationPaths()) + ->map(fn ($path) => realpath($path)) + ->toArray(); + $finder = Finder::create() - ->in(database_path('migrations')) + ->in($paths) ->name('*.php') ->ignoreDotFiles(true) ->ignoreVCS(true) ->files(); - $migrations = array_map(static function (SplFileInfo $fileInfo) { - return [$fileInfo->getMTime(), $fileInfo->getPath()]; - }, iterator_to_array($finder)); - - // Reset the array keys so there is less data - - $migrations = array_values($migrations); + // Get all the migration files and their last modified date + $migrations = collect(iterator_to_array($finder)) + ->map(fn (SplFileInfo $fileInfo) => [$fileInfo->getMTime()]) + // Reset the array keys so there is less data + ->values() + ->toArray(); // Add the current git branch - $checkBranch = new Process(['git', 'branch', '--show-current']); $checkBranch->run(); $migrations['gitBranch'] = trim($checkBranch->getOutput()); // Create a hash - return hash('sha256', json_encode($migrations, JSON_THROW_ON_ERROR)); } @@ -96,6 +98,30 @@ protected function storeMigrationChecksum(string $checksum): void file_put_contents($this->getMigrationChecksumFile(), $checksum); } + /** + * The paths that should be used to discover migrations + * + * @return array + */ + protected function getMigrationPaths(): array + { + return [ + database_path('migrations'), + ...app('migrator')->paths(), + ...$this->getCustomMigrationPaths(), + ]; + } + + /** + * Custom migration paths that should be used when discovering migrations + * + * @return array + */ + protected function getCustomMigrationPaths(): array + { + return []; + } + /** * Provides a configurable migration checksum file path *