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

Add PostgreSQL database support #86

Open
burleight opened this issue Mar 5, 2023 · 1 comment
Open

Add PostgreSQL database support #86

burleight opened this issue Mar 5, 2023 · 1 comment

Comments

@burleight
Copy link

I believe PostgreSQL support is possible by changing the DatabaseStore->write INSERT statement to use ON CONFLICT instead of ON DUPLICATE. Take this subclass of DatabaseStore for example:

use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Convert;
use SilverStripe\HybridSessions\Store\DatabaseStore;
use SilverStripe\ORM\DB;
use SilverStripe\PostgreSQL\PostgreSQLDatabase;

class PostgreSQLDatabaseStore extends DatabaseStore
{
    protected function isDatabaseReady()
    {
        // Such as during setup of testsession prior to DB connection.
        if (!DB::is_active()) {
            return false;
        }

        // If we have a DB of the wrong type then complain
        if (!(DB::get_conn() instanceof PostgreSQLDatabase)) {
            throw new Exception('PostgreSQLDatabaseStore currently only works with PostgreSQL databases');
        }

        // Prevent freakout during dev/build
        return ClassInfo::hasTable('HybridSessionDataObject');
    }

    public function write($session_id, $session_data)
    {
        if (!$this->isDatabaseReady()) {
            return false;
        }

        $expiry = $this->getNow() + $this->getLifetime();

        DB::query($str = sprintf(
            'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data")
            VALUES (\'%1$s\', %2$u, \'%3$s\')
            ON CONFLICT ("SessionID") DO UPDATE SET "Expiry" = %2$u, "Data" = \'%3$s\'',
            Convert::raw2sql($session_id),
            $expiry,
            Convert::raw2sql(static::binaryDataJsonEncode($session_data))
        ));

        return true;
    }
}
@maxime-rainville
Copy link

Honestly, this would be a very low priority for us. If you care about this option, you'll have to submit your own PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants