Skip to content

Commit

Permalink
Fix integrity constraints for database session driver. (#17301)
Browse files Browse the repository at this point in the history
Backporting fix of issue #9251 to 5.3 branch. Originally by Taylor
Otwell <[email protected]>.

Signed-off-by: Gleb Golubitsky <[email protected]>
  • Loading branch information
Sectoid authored and taylorotwell committed Jan 12, 2017
1 parent e9e79cc commit 3ee1c8f
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/Illuminate/Session/DatabaseSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use SessionHandlerInterface;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\QueryException;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Container\Container;

Expand Down Expand Up @@ -112,18 +113,46 @@ public function write($sessionId, $data)
}

if ($this->exists) {
$this->getQuery()->where('id', $sessionId)->update($payload);
$this->performUpdate($sessionId, $payload);
} else {
$payload['id'] = $sessionId;

$this->getQuery()->insert($payload);
$this->performInsert($sessionId, $payload);
}

$this->exists = true;

return true;
}

/**
* Perform an insert operation on the session ID.
*
* @param string $sessionId
* @param string $payload
* @return void
*/
protected function performInsert($sessionId, $payload)
{
try {
$payload['id'] = $sessionId;

return $this->getQuery()->insert($payload);
} catch (QueryException $e) {
$this->performUpdate($sessionId, $payload);
}
}

/**
* Perform an update operation on the session ID.
*
* @param string $sessionId
* @param string $payload
* @return int
*/
protected function performUpdate($sessionId, $payload)
{
return $this->getQuery()->where('id', $sessionId)->update($payload);
}

/**
* Get the default payload for the session.
*
Expand Down

0 comments on commit 3ee1c8f

Please sign in to comment.