From 061bc857241a3ff2c74370661a3ea7738531fc54 Mon Sep 17 00:00:00 2001 From: Craig Lawson Date: Wed, 7 Nov 2018 20:39:38 +0000 Subject: [PATCH] This should solve - 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 https://github.com/laravel/framework/issues/9251#issuecomment-270991219 Fix - https://github.com/laravel/framework/commit/d9e0a6a03891d16ed6a71151354445fbdc9e6f50 --- .../Session/DatabaseSessionHandler.php | 61 ++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Session/DatabaseSessionHandler.php b/src/Illuminate/Session/DatabaseSessionHandler.php index dca8979f49ca..1d7667a4f23f 100644 --- a/src/Illuminate/Session/DatabaseSessionHandler.php +++ b/src/Illuminate/Session/DatabaseSessionHandler.php @@ -1,6 +1,8 @@ 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} */