Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🚀 Refresh your test databases faster than you've ever seen before

License

NotificationsYou must be signed in to change notification settings

PlannrCrm/laravel-fast-refresh-database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Have you ever come across an issue where the traditionalRefreshDatabase trait takes ages to run tests when you have lots of migrations? If so, you may be after this package!

The Problem

Traditionally, theRefreshDatabase trait will runphp 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.

The Solution

You don't need to runphp artisan migrate:fresh every time you run tests, only when you add a new migration or change an old one. TheFastRefreshDatabase trait will create a checksum of yourmigrations folder as well as your current Git branch. It will then create a checksum file in your application'sstorage/app directory. When your migrations change or your branch changes, the checksum won't match the cached one andphp artisan migrate:fresh is run.

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!

Benchmarks

Running a single test, with about 400 migrations.

ProcessorBeforeAfter
Intel Core i530 seconds100 milliseconds
Apple M1 Pro5 seconds100 milliseconds

Installation

Install the package with Composer

composer require plannr/laravel-fast-refresh-database --dev

Adding to your TestCase

Next, just replace the existingRefreshDatabase trait you are using in your TestCase file with theFastRefreshDatabase trait

<?phpnamespace Tests;use Illuminate\Foundation\Testing\TestCase as BaseTestCase;-use Illuminate\Foundation\Testing\RefreshDatabase;+use Plannr\Laravel\FastRefreshDatabase\Traits\FastRefreshDatabase;abstract class TestCase extends BaseTestCase{    use CreatesApplication;-   use RefreshDatabase;+   use FastRefreshDatabase;}

Using Pest

Just replace theuses line in yourPest.php file

-use Illuminate\Foundation\Testing\RefreshDatabase;+use Plannr\Laravel\FastRefreshDatabase\Traits\FastRefreshDatabase;-uses(RefreshDatabase::class)->in(__DIR__);uses(FastRefreshDatabase::class)->in('Feature');

Deleting The Migration Checksum

Sometimes you may wish to force-update database migrations, to do this, locate themigration-checksum_{Database Name Slug}.txt file withinstorage/app.

Customising the checksum file location

You may customise the migration checksum file location and name by extending the trait and overwriting thegetMigrationChecksumFile() method.

protectedfunctiongetMigrationChecksumFile():string{returnstorage_path('custom/some-other-file.txt');}

ParaTest Databases

Parallel testing databases contain tokens that serve as unique identifiers for each test runner. This makes the trait inherently able to support parallel testing without any extra effort, because the database name is stored in the checksum file name.

About

🚀 Refresh your test databases faster than you've ever seen before

Topics

Resources

License

Stars

Watchers

Forks

Contributors5

Languages


[8]ページ先頭

©2009-2025 Movatter.jp