-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[9.x] Add DatabaseTruncation
trait for testing
#45726
[9.x] Add DatabaseTruncation
trait for testing
#45726
Conversation
Migrate the database in the first run, then truncate the affected tables and use the seeder for subsequent runs.
* allow excluding tables per connection * unset the event dispatcher before truncating, re-set afterwards
DatabaseTruncates
trait for testing
* This removes potential conflicts with other beforeApplicationDestroyed callbacks, where a database is needed * This matches the behaviour of the other tests instead of removing data afterwards. * It also makes it easier to check the data after failed tests, especially when a dusk test fails.
* allow the developer to specify which connections use foreign key checks, default to all connections * allwo the developer to create an excludeTables property instead of having to override the method
Nice approach. 😄 But this While P.S. |
@nshiro can you elaborate? error messages? |
(Edited) |
I also noticed I needed |
Thanks for your feedback @nshiro Are you able to create a repository with the failing tests? I have created one here based on what you mentioned and all tests pass with and without parallel. Here is the diff from a fresh install Yes |
@patrickomeara |
@nshiro Awesome. Thanks for testing this PR out. |
Why not extract |
Thanks - could you PR some docs to laravel/docs where you think this is relevant? |
@patrickomeara It looks to be useful for testing native-transaction-related libraries: (e.g. https://github.com/mpyw/laravel-database-advisory-lock/blob/master/tests/PostgresTransactionErrorRecoveryTest.php) |
|
||
if ($seeder = $this->seeder()) { | ||
// Use a specific seeder class... | ||
$this->artisan('db:seed', ['--class' => $seeder]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it will be better to use FQCN instead of the command name?
@taylorotwell Yes, will do this ASAP. |
DatabaseTruncates
trait for testingDatabaseTruncation
trait for testing
This is a second attempt at #45630
The
DatabaseTruncates
trait is a drop in replacement forDatabaseMigrations
however it only truncates tables with data instead of dropping and migrating the whole database, this is a more performant approach in most cases.In this PR I have added support for multiple connections, just like the
RefreshDatabase
trait.Excluding tables can be done per connection or across all connections depending on what the developer needs.
@emargareten raised the issue of foreign keys in the last PR. As this PR moves the truncating to the start of the test, I have added a
usesForeignKeyChecks()
which allows the developer to set the check per connection, defaulting to true.Once this PR is resolved I will PR this change that cleans up the logic around the event dispatcher