Skip to content

Commit

Permalink
Error out if incompatible DBAL version is detected (laravel#38543)
Browse files Browse the repository at this point in the history
doctrine/dbal is an optional dependency of laravel/framework, and v3 of
the DBAL has been released recently, but obviously, Laravel 6 was never
meant to be compatible with that version.
Users still using Laravel 6 and updating their dependencies get confused
and file invalid reports on the DBAL, see for instance
doctrine/dbal#4439
or
doctrine/dbal#4757

Fixes laravel#24271
  • Loading branch information
greg0ire authored and victorvilella committed Oct 12, 2021
1 parent 3c1663c commit 7aca26e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 115 deletions.
38 changes: 9 additions & 29 deletions src/Illuminate/Database/MySqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,14 @@
namespace Illuminate\Database;

use Doctrine\DBAL\Driver\PDOMySql\Driver as DoctrineDriver;
use Doctrine\DBAL\Version;
use Illuminate\Database\PDO\MySqlDriver;
use Illuminate\Database\Query\Grammars\MySqlGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Illuminate\Database\Schema\Grammars\MySqlGrammar as SchemaGrammar;
use Illuminate\Database\Schema\MySqlBuilder;
use Illuminate\Database\Schema\MySqlSchemaState;
use Illuminate\Filesystem\Filesystem;
use PDO;
use LogicException;

class MySqlConnection extends Connection
{
/**
* Determine if the connected database is a MariaDB database.
*
* @return bool
*/
public function isMaria()
{
return strpos($this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), 'MariaDB') !== false;
}

/**
* Get the default query grammar instance.
*
Expand Down Expand Up @@ -59,18 +45,6 @@ protected function getDefaultSchemaGrammar()
return $this->withTablePrefix(new SchemaGrammar);
}

/**
* Get the schema state for the connection.
*
* @param \Illuminate\Filesystem\Filesystem|null $files
* @param callable|null $processFactory
* @return \Illuminate\Database\Schema\MySqlSchemaState
*/
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
{
return new MySqlSchemaState($this, $files, $processFactory);
}

/**
* Get the default post processor instance.
*
Expand All @@ -84,10 +58,16 @@ protected function getDefaultPostProcessor()
/**
* Get the Doctrine DBAL driver.
*
* @return \Doctrine\DBAL\Driver\PDOMySql\Driver|\Illuminate\Database\PDO\MySqlDriver
* @return \Doctrine\DBAL\Driver\PDOMySql\Driver
*/
protected function getDoctrineDriver()
{
return class_exists(Version::class) ? new DoctrineDriver : new MySqlDriver;
if (! class_exists(DoctrineDriver::class)) {
throw new LogicException(
'Laravel v6 is only compatible with doctrine/dbal 2, in order to use this feature you must require the package "doctrine/dbal:^2.6".'
);
}

return new DoctrineDriver;
}
}
54 changes: 9 additions & 45 deletions src/Illuminate/Database/PostgresConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,14 @@
namespace Illuminate\Database;

use Doctrine\DBAL\Driver\PDOPgSql\Driver as DoctrineDriver;
use Doctrine\DBAL\Version;
use Illuminate\Database\PDO\PostgresDriver;
use Illuminate\Database\Query\Grammars\PostgresGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\PostgresProcessor;
use Illuminate\Database\Schema\Grammars\PostgresGrammar as SchemaGrammar;
use Illuminate\Database\Schema\PostgresBuilder;
use Illuminate\Database\Schema\PostgresSchemaState;
use Illuminate\Filesystem\Filesystem;
use PDO;
use LogicException;

class PostgresConnection extends Connection
{
/**
* Bind values to their parameters in the given statement.
*
* @param \PDOStatement $statement
* @param array $bindings
* @return void
*/
public function bindValues($statement, $bindings)
{
foreach ($bindings as $key => $value) {
if (is_int($value)) {
$pdoParam = PDO::PARAM_INT;
} elseif (is_resource($value)) {
$pdoParam = PDO::PARAM_LOB;
} else {
$pdoParam = PDO::PARAM_STR;
}

$statement->bindValue(
is_string($key) ? $key : $key + 1,
$value,
$pdoParam
);
}
}

/**
* Get the default query grammar instance.
*
Expand Down Expand Up @@ -75,18 +45,6 @@ protected function getDefaultSchemaGrammar()
return $this->withTablePrefix(new SchemaGrammar);
}

/**
* Get the schema state for the connection.
*
* @param \Illuminate\Filesystem\Filesystem|null $files
* @param callable|null $processFactory
* @return \Illuminate\Database\Schema\PostgresSchemaState
*/
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
{
return new PostgresSchemaState($this, $files, $processFactory);
}

/**
* Get the default post processor instance.
*
Expand All @@ -100,10 +58,16 @@ protected function getDefaultPostProcessor()
/**
* Get the Doctrine DBAL driver.
*
* @return \Doctrine\DBAL\Driver\PDOPgSql\Driver|\Illuminate\Database\PDO\PostgresDriver
* @return \Doctrine\DBAL\Driver\PDOPgSql\Driver
*/
protected function getDoctrineDriver()
{
return class_exists(Version::class) ? new DoctrineDriver : new PostgresDriver;
if (! class_exists(DoctrineDriver::class)) {
throw new LogicException(
'Laravel v6 is only compatible with doctrine/dbal 2, in order to use this feature you must require the package "doctrine/dbal:^2.6".'
);
}

return new DoctrineDriver;
}
}
28 changes: 9 additions & 19 deletions src/Illuminate/Database/SQLiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
namespace Illuminate\Database;

use Doctrine\DBAL\Driver\PDOSqlite\Driver as DoctrineDriver;
use Doctrine\DBAL\Version;
use Illuminate\Database\PDO\SQLiteDriver;
use Illuminate\Database\Query\Grammars\SQLiteGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\SQLiteProcessor;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar as SchemaGrammar;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Database\Schema\SqliteSchemaState;
use Illuminate\Filesystem\Filesystem;
use LogicException;

class SQLiteConnection extends Connection
{
Expand Down Expand Up @@ -72,19 +69,6 @@ protected function getDefaultSchemaGrammar()
return $this->withTablePrefix(new SchemaGrammar);
}

/**
* Get the schema state for the connection.
*
* @param \Illuminate\Filesystem\Filesystem|null $files
* @param callable|null $processFactory
*
* @throws \RuntimeException
*/
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
{
return new SqliteSchemaState($this, $files, $processFactory);
}

/**
* Get the default post processor instance.
*
Expand All @@ -98,11 +82,17 @@ protected function getDefaultPostProcessor()
/**
* Get the Doctrine DBAL driver.
*
* @return \Doctrine\DBAL\Driver\PDOSqlite\Driver|\Illuminate\Database\PDO\SQLiteDriver
* @return \Doctrine\DBAL\Driver\PDOSqlite\Driver
*/
protected function getDoctrineDriver()
{
return class_exists(Version::class) ? new DoctrineDriver : new SQLiteDriver;
if (! class_exists(DoctrineDriver::class)) {
throw new LogicException(
'Laravel v6 is only compatible with doctrine/dbal 2, in order to use this feature you must require the package "doctrine/dbal:^2.6".'
);
}

return new DoctrineDriver;
}

/**
Expand Down
39 changes: 17 additions & 22 deletions src/Illuminate/Database/SqlServerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

use Closure;
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as DoctrineDriver;
use Doctrine\DBAL\Version;
use Illuminate\Database\PDO\SqlServerDriver;
use Exception;
use Illuminate\Database\Query\Grammars\SqlServerGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\SqlServerProcessor;
use Illuminate\Database\Schema\Grammars\SqlServerGrammar as SchemaGrammar;
use Illuminate\Database\Schema\SqlServerBuilder;
use Illuminate\Filesystem\Filesystem;
use RuntimeException;
use LogicException;
use Throwable;

class SqlServerConnection extends Connection
Expand All @@ -23,7 +21,7 @@ class SqlServerConnection extends Connection
* @param int $attempts
* @return mixed
*
* @throws \Throwable
* @throws \Exception|\Throwable
*/
public function transaction(Closure $callback, $attempts = 1)
{
Expand All @@ -43,10 +41,14 @@ public function transaction(Closure $callback, $attempts = 1)
$this->getPdo()->exec('COMMIT TRAN');
}

// If we catch an exception, we will rollback so nothing gets messed
// If we catch an exception, we will roll back so nothing gets messed
// up in the database. Then we'll re-throw the exception so it can
// be handled how the developer sees fit for their applications.
catch (Throwable $e) {
catch (Exception $e) {
$this->getPdo()->exec('ROLLBACK TRAN');

throw $e;
} catch (Throwable $e) {
$this->getPdo()->exec('ROLLBACK TRAN');

throw $e;
Expand Down Expand Up @@ -90,19 +92,6 @@ protected function getDefaultSchemaGrammar()
return $this->withTablePrefix(new SchemaGrammar);
}

/**
* Get the schema state for the connection.
*
* @param \Illuminate\Filesystem\Filesystem|null $files
* @param callable|null $processFactory
*
* @throws \RuntimeException
*/
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
{
throw new RuntimeException('Schema dumping is not supported when using SQL Server.');
}

/**
* Get the default post processor instance.
*
Expand All @@ -116,10 +105,16 @@ protected function getDefaultPostProcessor()
/**
* Get the Doctrine DBAL driver.
*
* @return \Doctrine\DBAL\Driver\PDOSqlsrv\Driver|\Illuminate\Database\PDO\SqlServerDriver
* @return \Doctrine\DBAL\Driver\PDOSqlsrv\Driver
*/
protected function getDoctrineDriver()
{
return class_exists(Version::class) ? new DoctrineDriver : new SqlServerDriver;
if (! class_exists(DoctrineDriver::class)) {
throw new LogicException(
'Laravel v6 is only compatible with doctrine/dbal 2, in order to use this feature you must require the package "doctrine/dbal:^2.6".'
);
}

return new DoctrineDriver;
}
}

0 comments on commit 7aca26e

Please sign in to comment.