Skip to content

Commit

Permalink
Use write database connection when validating uniqueness (#13718)
Browse files Browse the repository at this point in the history
It would be possible that when an application have a huge latency
between master and slave that a novice user would reattempt to recreate
an entry which is now only available on the master connection.

When he/she create the record again which require unique value such as email
it currently would only fetch from the slave database connection and
would return pass, however when the application try to save the actual
entry to the database it would fail due to unique (if we set it on the
database).

This solve the issue by always checking against master (write)
connection.

Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone authored and taylorotwell committed May 26, 2016
1 parent 1c1f2f2 commit c688e5a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/DatabasePresenceVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function addWhere($query, $key, $extraValue)
*/
protected function table($table)
{
return $this->db->connection($this->connection)->table($table);
return $this->db->connection($this->connection)->table($table)->useWritePdo();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function testBasicCount()
$verifier->setConnection('connection');
$db->shouldReceive('connection')->once()->with('connection')->andReturn($conn = m::mock('StdClass'));
$conn->shouldReceive('table')->once()->with('table')->andReturn($builder = m::mock('StdClass'));
$builder->shouldReceive('useWritePdo')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('column', '=', 'value')->andReturn($builder);
$extra = ['foo' => 'NULL', 'bar' => 'NOT_NULL', 'baz' => 'taylor', 'faz' => true];
$builder->shouldReceive('whereNull')->with('foo');
Expand Down

0 comments on commit c688e5a

Please sign in to comment.