Skip to content

Commit 3cafdbd

Browse files
authored
Merge pull request #2 from krzysztofrewak/patch-1
configurable checksum file path
2 parents c71c75b + b815465 commit 3cafdbd

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

.github/README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Have you ever come across an issue where the traditional `RefreshDatabase` trait
66
Traditionally, the `RefreshDatabase` trait will run `php artisan migrate:fresh` every time you run tests. After the first test, it will use transactions to roll back the data and run the next one, so subsequent tests are fast, but the initial test is slow. This can be really annoying if you are used to running a single test, as it could take seconds to run a single test.
77

88
## The Solution
9-
You don't need to run `php artisan migrate:fresh` every time you run tests, only when you add a new migration or change an old one. The `FastRefreshDatabase` trait will create a checksum of your `migrations` folder as well as your current Git branch, if you are using Git and will create a `migrationChecksum.txt` file in your application. When your migrations change or your branch changes, the checksum won't match the cached one and `php artisan migrate:fresh` is run.
9+
You don't need to run `php artisan migrate:fresh` every time you run tests, only when you add a new migration or change an old one. The `FastRefreshDatabase` trait will create a checksum of your `migrations` folder as well as your current Git branch. It will then create a checksum file in your application's `storage/app` directory. When your migrations change or your branch changes, the checksum won't match the cached one and `php artisan migrate:fresh` is run.
1010

1111
When you don't make any changes, it will continue to use the same database without refreshing, which can speed up the test time by 100x!
1212

@@ -57,7 +57,23 @@ Just replace the `uses` line in your `Pest.php` file
5757
+uses(FastRefreshDatabase::class)->in(__DIR__);
5858
```
5959

60-
## Ignoring MigrationChecksum.txt
61-
Next, add "MigrationChecksum.txt" to your .gitignore file.
60+
## Deleting The Migration Checksum
6261

63-
Copyright (c) 2022 Plannr Technologies Ltd
62+
Sometimes you may wish to force-update database migrations, to do this, locate the `migrationChecksum.txt` file within `storage/app`.
63+
64+
## Customising the checksum file location
65+
66+
You may customise the migration checksum file location and name by extending the trait and overwriting the `getMigrationChecksumFile()` method.
67+
68+
```php
69+
protected function getMigrationChecksumFile(): string
70+
{
71+
return storage_path('custom/some-other-file.txt');
72+
}
73+
```
74+
75+
## Known Issues
76+
77+
### ParaTest Databases
78+
79+
The trait is unaware of what database or environment your tests are running within. Sometimes when running a parallel test after running individual tests, the migration checksum file may not have been deleted. You may have to manually delete the checksum file before running parallel tests.

src/Traits/FastRefreshDatabase.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function calculateMigrationChecksum(): string
8282
*/
8383
protected function getCachedMigrationChecksum(): ?string
8484
{
85-
return rescue(static fn () => file_get_contents(base_path('migrationChecksum.txt')), null, false);
85+
return rescue(static fn () => file_get_contents($this->getMigrationChecksumFile()), null, false);
8686
}
8787

8888
/**
@@ -93,6 +93,16 @@ protected function getCachedMigrationChecksum(): ?string
9393
*/
9494
protected function storeMigrationChecksum(string $checksum): void
9595
{
96-
file_put_contents(base_path('migrationChecksum.txt'), $checksum);
96+
file_put_contents($this->getMigrationChecksumFile(), $checksum);
97+
}
98+
99+
/**
100+
* Provides a configurable migration checksum file path
101+
*
102+
* @return string
103+
*/
104+
protected function getMigrationChecksumFile(): string
105+
{
106+
return storage_path('app/migrationChecksum.txt');
97107
}
98108
}

0 commit comments

Comments
 (0)