|
@@ -3,22 +3,20 @@
|
|
|
namespace Knuckles\Scribe\Extracting;
|
|
|
|
|
|
use Exception;
|
|
|
-use Knuckles\Scribe\Exceptions\DbTransactionSupportException;
|
|
|
+use Knuckles\Scribe\Exceptions\DatabaseTransactionsNotSupported;
|
|
|
use Knuckles\Scribe\Exceptions\ScribeException;
|
|
|
use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
|
|
|
+use Knuckles\Scribe\Tools\DocumentationConfig;
|
|
|
|
|
|
trait DatabaseTransactionHelpers
|
|
|
{
|
|
|
- /**
|
|
|
- * @return void
|
|
|
- */
|
|
|
private function startDbTransaction()
|
|
|
{
|
|
|
$connections = array_keys(config('database.connections', []));
|
|
|
|
|
|
- foreach ($connections as $conn) {
|
|
|
+ foreach ($connections as $connection) {
|
|
|
try {
|
|
|
- $driver = app('db')->connection($conn);
|
|
|
+ $driver = app('db')->connection($connection);
|
|
|
|
|
|
if (self::driverSupportsTransactions($driver)) {
|
|
|
$driver->beginTransaction();
|
|
@@ -26,12 +24,13 @@ trait DatabaseTransactionHelpers
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if ($this->isNoTransactionSupportAllowed($conn)) {
|
|
|
- throw DbTransactionSupportException::create($conn, get_class($driver));
|
|
|
+ $driverClassName = get_class($driver);
|
|
|
+
|
|
|
+ if ($this->shouldAllowDatabasePersistence($driverClassName)) {
|
|
|
+ throw DatabaseTransactionsNotSupported::create($connection, $driverClassName);
|
|
|
}
|
|
|
|
|
|
- c::warn("Database driver for the connection [{$conn}] does not support transactions!");
|
|
|
- c::warn("Any changes made to your database will persist!");
|
|
|
+ c::warn("Database driver [$driverClassName] for the connection [{$connection}] does not support transactions. Any changes made to your database will persist.");
|
|
|
} catch (ScribeException $e) {
|
|
|
throw $e;
|
|
|
} catch (Exception $e) {
|
|
@@ -46,9 +45,9 @@ trait DatabaseTransactionHelpers
|
|
|
{
|
|
|
$connections = array_keys(config('database.connections', []));
|
|
|
|
|
|
- foreach ($connections as $conn) {
|
|
|
+ foreach ($connections as $connection) {
|
|
|
try {
|
|
|
- $driver = app('db')->connection($conn);
|
|
|
+ $driver = app('db')->connection($connection);
|
|
|
|
|
|
if (self::driverSupportsTransactions($driver)) {
|
|
|
$driver->rollBack();
|
|
@@ -56,23 +55,16 @@ trait DatabaseTransactionHelpers
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- c::warn("Database driver for the connection [{$conn}] does not support transactions!");
|
|
|
- c::warn("Any changes made to your database will persist!");
|
|
|
+ $driverClassName = get_class($driver);
|
|
|
+ c::warn("Database driver [$driverClassName] for the connection [{$connection}] does not support transactions. Any changes made to your database have been persisted.");
|
|
|
} catch (Exception $e) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Assesses whether or not the "PDO" driver provided supports transactions
|
|
|
- *
|
|
|
- * @param mixed $driver Driver prodicted for particular connection
|
|
|
- *
|
|
|
- * @return boolean
|
|
|
- */
|
|
|
- private static function driverSupportsTransactions($driver)
|
|
|
+ private static function driverSupportsTransactions($driver): bool
|
|
|
{
|
|
|
- $methods = [ 'beginTransaction', 'rollback' ];
|
|
|
+ $methods = ['beginTransaction', 'rollback'];
|
|
|
|
|
|
foreach ($methods as $method) {
|
|
|
if (! method_exists($driver, $method)) {
|
|
@@ -86,21 +78,16 @@ trait DatabaseTransactionHelpers
|
|
|
/**
|
|
|
* Assesses whether drivers without transaction support can proceed
|
|
|
*
|
|
|
- * @param string $connection_name Name of the connection
|
|
|
+ * @param string $driverClassName
|
|
|
*
|
|
|
- * @return boolean
|
|
|
+ * @return bool
|
|
|
*/
|
|
|
- private function isNoTransactionSupportAllowed(string $connection_name)
|
|
|
+ private function shouldAllowDatabasePersistence(string $driverClassName): bool
|
|
|
{
|
|
|
$config = $this->getConfig();
|
|
|
|
|
|
- $allow_list = $config->get('run_without_database_transactions', false);
|
|
|
-
|
|
|
- if (is_array($allow_list)) {
|
|
|
- return in_array($connection_name, $allow_list);
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
+ $whitelistedDrivers = $config->get('continue_without_database_transactions', []);
|
|
|
+ return in_array($driverClassName, $whitelistedDrivers);
|
|
|
}
|
|
|
|
|
|
/**
|