Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose better types for the ci_sessions table #2256

Merged
merged 3 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions system/Session/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function read($sessionID): string
$this->sessionID = $sessionID;

$builder = $this->db->table($this->table)
->select('data')
->select($this->platform === 'postgre' ? "encode(data, 'base64') AS data" : 'data')
->where('id', $sessionID);

if ($this->matchIP)
Expand All @@ -188,9 +188,6 @@ public function read($sessionID): string
return '';
}

// PostgreSQL's variant of a BLOB datatype is Bytea, which is a
// PITA to work with, so we use base64-encoded data in a TEXT
// field instead.
if (is_bool($result))
{
$result = '';
Expand Down Expand Up @@ -242,8 +239,8 @@ public function write($sessionID, $sessionData): bool
$insertData = [
'id' => $sessionID,
'ip_address' => $this->ipAddress,
'timestamp' => time(),
'data' => $this->platform === 'postgre' ? base64_encode($sessionData) : $sessionData,
'timestamp' => 'now()',
'data' => $this->platform === 'postgre' ? '\x' . bin2hex($sessionData) : $sessionData,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just remember \x prefix is valid for hex in postgres 👍

];

if (! $this->db->table($this->table)->insert($insertData))
Expand All @@ -265,12 +262,12 @@ public function write($sessionID, $sessionData): bool
}

$updateData = [
'timestamp' => time(),
'timestamp' => 'now()',
];

if ($this->fingerprint !== md5($sessionData))
{
$updateData['data'] = ($this->platform === 'postgre') ? base64_encode($sessionData) : $sessionData;
$updateData['data'] = ($this->platform === 'postgre') ? '\x' . bin2hex($sessionData) : $sessionData;
}

if (! $builder->update($updateData))
Expand Down Expand Up @@ -348,7 +345,8 @@ public function destroy($sessionID): bool
*/
public function gc($maxlifetime): bool
{
return ($this->db->table($this->table)->delete('timestamp < ' . (time() - $maxlifetime))) ? true : $this->fail();
$interval = implode(" '"[(int)($this->platform === 'postgre')], ['', "{$maxlifetime} second", '']);
return ($this->db->table($this->table)->delete("timestamp < now() - INTERVAL {$interval}")) ? true : $this->fail();
}

//--------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions user_guide_src/source/libraries/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ For MySQL::
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(128) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`timestamp` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
);
Expand All @@ -594,9 +594,9 @@ For PostgreSQL::

CREATE TABLE "ci_sessions" (
"id" varchar(128) NOT NULL,
"ip_address" varchar(45) NOT NULL,
"timestamp" bigint DEFAULT 0 NOT NULL,
"data" text DEFAULT '' NOT NULL
"ip_address" inet NOT NULL,
"timestamp" timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
"data" bytea DEFAULT '' NOT NULL
);

CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");
Expand Down