Skip to content

Commit

Permalink
Merge branch '8.x'
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG-8.x.md
#	composer.json
#	src/Illuminate/Foundation/Application.php
#	src/Illuminate/Routing/UrlGenerator.php
#	tests/Integration/Routing/UrlSigningTest.php
  • Loading branch information
driesvints committed Jun 7, 2021
2 parents 7c2c330 + ece544c commit 22d51fe
Show file tree
Hide file tree
Showing 27 changed files with 1,121 additions and 71 deletions.
47 changes: 9 additions & 38 deletions Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Traits\Conditionable;
use InvalidArgumentException;
use RuntimeException;

trait BuildsQueries
{
use Conditionable;

/**
* Chunk the results of the query.
*
Expand Down Expand Up @@ -81,6 +84,8 @@ public function chunkMap(callable $callback, $count = 1000)
* @param callable $callback
* @param int $count
* @return bool
*
* @throws \RuntimeException
*/
public function each(callable $callback, $count = 1000)
{
Expand Down Expand Up @@ -172,6 +177,8 @@ public function eachById(callable $callback, $count = 1000, $column = null, $ali
*
* @param int $chunkSize
* @return \Illuminate\Support\LazyCollection
*
* @throws \InvalidArgumentException
*/
public function lazy($chunkSize = 1000)
{
Expand Down Expand Up @@ -205,6 +212,8 @@ public function lazy($chunkSize = 1000)
* @param string|null $column
* @param string|null $alias
* @return \Illuminate\Support\LazyCollection
*
* @throws \InvalidArgumentException
*/
public function lazyById($chunkSize = 1000, $column = null, $alias = null)
{
Expand Down Expand Up @@ -272,25 +281,6 @@ public function sole($columns = ['*'])
return $result->first();
}

/**
* Apply the callback's query changes if the given "value" is true.
*
* @param mixed $value
* @param callable $callback
* @param callable|null $default
* @return mixed|$this
*/
public function when($value, $callback, $default = null)
{
if ($value) {
return $callback($this, $value) ?: $this;
} elseif ($default) {
return $default($this, $value) ?: $this;
}

return $this;
}

/**
* Pass the query to a given callback.
*
Expand All @@ -302,25 +292,6 @@ public function tap($callback)
return $this->when(true, $callback);
}

/**
* Apply the callback's query changes if the given "value" is false.
*
* @param mixed $value
* @param callable $callback
* @param callable|null $default
* @return mixed|$this
*/
public function unless($value, $callback, $default = null)
{
if (! $value) {
return $callback($this, $value) ?: $this;
} elseif ($default) {
return $default($this, $value) ?: $this;
}

return $this;
}

/**
* Create a new length-aware paginator instance.
*
Expand Down
2 changes: 2 additions & 0 deletions Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ public function transactionLevel()
*
* @param callable $callback
* @return void
*
* @throws \RuntimeException
*/
public function afterCommit($callback)
{
Expand Down
65 changes: 63 additions & 2 deletions Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class Connection implements ConnectionInterface
*/
protected $database;

/**
* The type of the connection.
*
* @var string|null
*/
protected $type;

/**
* The table prefix for the connection.
*
Expand Down Expand Up @@ -122,10 +129,17 @@ class Connection implements ConnectionInterface
/**
* Indicates if changes have been made to the database.
*
* @var int
* @var bool
*/
protected $recordsModified = false;

/**
* Indicates if the connection should use the "write" PDO connection.
*
* @var bool
*/
protected $readOnWriteConnection = false;

/**
* All of the queries run against the connection.
*
Expand Down Expand Up @@ -854,6 +868,16 @@ public function raw($value)
return new Expression($value);
}

/**
* Determine if the database connection has modified any database records.
*
* @return bool
*/
public function hasModifiedRecords()
{
return $this->recordsModified;
}

/**
* Indicate if any records have been modified.
*
Expand All @@ -877,6 +901,19 @@ public function forgetRecordModificationState()
$this->recordsModified = false;
}

/**
* Indicate that the connection should use the write PDO connection for reads.
*
* @param bool $value
* @return $this
*/
public function useWriteConnectionWhenReading($value = true)
{
$this->readOnWriteConnection = $value;

return $this;
}

/**
* Is Doctrine available?
*
Expand Down Expand Up @@ -973,7 +1010,8 @@ public function getReadPdo()
return $this->getPdo();
}

if ($this->recordsModified && $this->getConfig('sticky')) {
if ($this->readOnWriteConnection ||
($this->recordsModified && $this->getConfig('sticky'))) {
return $this->getPdo();
}

Expand Down Expand Up @@ -1045,6 +1083,16 @@ public function getName()
return $this->getConfig('name');
}

/**
* Get the database connection full name.
*
* @return string|null
*/
public function getNameWithReadWriteType()
{
return $this->getName().($this->readWriteType ? '::'.$this->readWriteType : '');
}

/**
* Get an option from the configuration options.
*
Expand Down Expand Up @@ -1274,6 +1322,19 @@ public function setDatabaseName($database)
return $this;
}

/**
* Set the read / write type of the connection.
*
* @param string|null $readWriteType
* @return $this
*/
public function setReadWriteType($readWriteType)
{
$this->readWriteType = $readWriteType;

return $this;
}

/**
* Get the table prefix for the connection.
*
Expand Down
12 changes: 11 additions & 1 deletion Console/DbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class DbCommand extends Command
*
* @var string
*/
protected $signature = 'db {connection? : The database connection that should be used}';
protected $signature = 'db {connection? : The database connection that should be used}
{--read : Connect to the read connection}
{--write : Connect to the write connection}';

/**
* The console command description.
Expand Down Expand Up @@ -47,6 +49,8 @@ public function handle()
* Get the database connection configuration.
*
* @return array
*
* @throws \UnexpectedValueException
*/
public function getConnection()
{
Expand All @@ -62,6 +66,12 @@ public function getConnection()
$connection = (new ConfigurationUrlParser)->parseConfiguration($connection);
}

if ($this->option('read')) {
$connection = array_merge($connection, $connection['read']);
} elseif ($this->option('write')) {
$connection = array_merge($connection, $connection['write']);
}

return $connection;
}

Expand Down
14 changes: 9 additions & 5 deletions DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct($app, ConnectionFactory $factory)
$this->factory = $factory;

$this->reconnector = function ($connection) {
$this->reconnect($connection->getName());
$this->reconnect($connection->getNameWithReadWriteType());
};
}

Expand Down Expand Up @@ -165,7 +165,7 @@ protected function configuration($name)
*/
protected function configure(Connection $connection, $type)
{
$connection = $this->setPdoForType($connection, $type);
$connection = $this->setPdoForType($connection, $type)->setReadWriteType($type);

// First we'll set the fetch mode and a few other dependencies of the database
// connection. This method basically just configures and prepares it to get
Expand Down Expand Up @@ -275,11 +275,15 @@ public function usingConnection($name, callable $callback)
*/
protected function refreshPdoConnections($name)
{
$fresh = $this->makeConnection($name);
[$database, $type] = $this->parseConnectionName($name);

$fresh = $this->configure(
$this->makeConnection($database), $type
);

return $this->connections[$name]
->setPdo($fresh->getRawPdo())
->setReadPdo($fresh->getRawReadPdo());
->setPdo($fresh->getRawPdo())
->setReadPdo($fresh->getRawReadPdo());
}

/**
Expand Down
Loading

0 comments on commit 22d51fe

Please sign in to comment.