From 201e53ddd64a88ded2e748a1133226b34c884811 Mon Sep 17 00:00:00 2001 From: Yuriy Moskalchuk Date: Thu, 12 Jul 2018 00:23:03 +0300 Subject: [PATCH] updated RefreshDatabase trait (backport from Laravel v5.6.27) --- src/Concerns/RefreshDatabase.php | 42 +++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Concerns/RefreshDatabase.php b/src/Concerns/RefreshDatabase.php index 43d58a4..b193578 100644 --- a/src/Concerns/RefreshDatabase.php +++ b/src/Concerns/RefreshDatabase.php @@ -14,8 +14,8 @@ trait RefreshDatabase public function refreshDatabase() { $this->usingInMemoryDatabase() - ? $this->refreshInMemoryDatabase() - : $this->refreshTestDatabase(); + ? $this->refreshInMemoryDatabase() + : $this->refreshTestDatabase(); } /** @@ -26,8 +26,8 @@ public function refreshDatabase() protected function usingInMemoryDatabase() { return config('database.connections')[ - config('database.default') - ]['database'] == ':memory:'; + config('database.default') + ]['database'] == ':memory:'; } /** @@ -48,7 +48,9 @@ protected function refreshInMemoryDatabase() protected function refreshTestDatabase() { if (! RefreshDatabaseState::$migrated) { - $this->artisan('migrate:fresh'); + $this->artisan('migrate:fresh', $this->shouldDropViews() ? [ + '--drop-views' => true, + ] : []); RefreshDatabaseState::$migrated = true; } @@ -66,12 +68,23 @@ public function beginDatabaseTransaction() $database = $this->app->make('db'); foreach ($this->connectionsToTransact() as $name) { - $database->connection($name)->beginTransaction(); + $connection = $database->connection($name); + $dispatcher = $connection->getEventDispatcher(); + + $connection->unsetEventDispatcher(); + $connection->beginTransaction(); + $connection->setEventDispatcher($dispatcher); } $this->beforeApplicationDestroyed(function () use ($database) { foreach ($this->connectionsToTransact() as $name) { - $database->connection($name)->rollBack(); + $connection = $database->connection($name); + $dispatcher = $connection->getEventDispatcher(); + + $connection->unsetEventDispatcher(); + $connection->rollback(); + $connection->setEventDispatcher($dispatcher); + $connection->disconnect(); } }); } @@ -84,6 +97,17 @@ public function beginDatabaseTransaction() protected function connectionsToTransact() { return property_exists($this, 'connectionsToTransact') - ? $this->connectionsToTransact : [null]; + ? $this->connectionsToTransact : [null]; + } + + /** + * Determine if views should be dropped when refreshing the database. + * + * @return bool + */ + protected function shouldDropViews() + { + return property_exists($this, 'dropViews') + ? $this->dropViews : false; } -} +} \ No newline at end of file