Skip to content

Commit

Permalink
This should solve -
Browse files Browse the repository at this point in the history
local.ERROR: 500 exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'XXXXXXX' for key 'sessions_id_unique'' in vendor/laravel/framework/src/Illuminate/Database/Connection.php:369

laravel#9251 (comment)

Fix -

laravel@d9e0a6a
  • Loading branch information
web-engineer committed Nov 7, 2018
1 parent 7bfe4a1 commit 061bc85
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions src/Illuminate/Session/DatabaseSessionHandler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php namespace Illuminate\Session;

use Illuminate\Database\Connection;
use Carbon\Carbon;
use Illuminate\Database\QueryException;

class DatabaseSessionHandler implements \SessionHandlerInterface, ExistenceAwareInterface {

Expand Down Expand Up @@ -74,22 +76,67 @@ public function read($sessionId)
*/
public function write($sessionId, $data)
{
$payload = $this->getDefaultPayload($data);

if (! $this->exists) {
$this->read($sessionId);
}

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

$this->exists = true;
}

/**
* Perform an insert operation on the session ID.
*
* @param string $sessionId
* @param string $payload
* @return void
*/
protected function performInsert($sessionId, $payload)
{
try {
return $this->getQuery()->insert(Arr::set($payload, 'id', $sessionId));
} 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.
*
* @param string $data
* @return array
*/
protected function getDefaultPayload($data)
{
//timestamp was time() in 4.2 origionally
$payload = [
'payload' => base64_encode($data),
'last_activity' => Carbon::now()->getTimestamp(),
];
//just return the payload
return $payload;
}
/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 061bc85

Please sign in to comment.