From 122149b83b91ae8d63e1ba90271d7369188bd127 Mon Sep 17 00:00:00 2001
From: Tim van Dijen <tim.dijen@minbzk.nl>
Date: Mon, 22 Jul 2019 15:40:58 +0200
Subject: [PATCH] Rewrite table upgrade for SQLite

---
 modules/saml/lib/SP/LogoutStore.php | 33 ++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/modules/saml/lib/SP/LogoutStore.php b/modules/saml/lib/SP/LogoutStore.php
index c06cec565..a324529b6 100644
--- a/modules/saml/lib/SP/LogoutStore.php
+++ b/modules/saml/lib/SP/LogoutStore.php
@@ -50,17 +50,40 @@ class LogoutStore
             switch ($store->driver) {
                 case 'pgsql':
                     // This does not affect the NOT NULL constraint
-                    $query = 'ALTER TABLE '.$store->prefix.
-                        '_saml_LogoutStore ALTER COLUMN _authSource TYPE VARCHAR(255)';
+                    $update = ['ALTER TABLE '.$store->prefix.
+                        '_saml_LogoutStore ALTER COLUMN _authSource TYPE VARCHAR(255)'];
+                    break;
+                case  'sqlite':
+                    /**
+                     * TableVersion 2 increased the column size to 255 which is the maximum length of a FQDN
+                     * Because SQLite does not support field alterations, the approach is to:
+                     *     Create a new table without the proper column size
+                     *     Copy the current data to the new table
+                     *     Drop the old table
+                     *     Rename the new table correctly
+                     *     Read the index
+                     */
+                    $update = [
+                        'CREATE TABLE '.$store->prefix.'_saml_LogoutStore_new (_authSource VARCHAR(255) NOT NULL,'.
+                        '_nameId VARCHAR(40) NOT NULL, _sessionIndex VARCHAR(50) NOT NULL, _expire TIMESTAMP NOT NULL,'.
+                        '_sessionId VARCHAR(50) NOT NULL, UNIQUE (_authSource, _nameID, _sessionIndex))',
+                        'INSERT INTO '.$store->prefix.'_saml_LogoutStore_new SELECT * FROM '.$store->prefix.'_saml_LogoutStore',
+                        'DROP TABLE '.$store->prefix.'_saml_LogoutStore',
+                        'ALTER TABLE '.$store->prefix.'_saml_LogoutStore_new RENAME TO '.$store->prefix.'_saml_LogoutStore',
+                        'CREATE INDEX '.$store->prefix.'_saml_LogoutStore_expire ON '.$store->prefix.'_saml_LogoutStore (_expire)',
+                        'CREATE INDEX '.$store->prefix.'_saml_LogoutStore_nameId ON '.$store->prefix.'_saml_LogoutStore (_authSource, _nameId)'
+                    ];
                     break;
                 default:
-                    $query = 'ALTER TABLE '.$store->prefix.
-                        '_saml_LogoutStore MODIFY _authSource VARCHAR(255) NOT NULL';
+                    $update = ['ALTER TABLE '.$store->prefix.
+                        '_saml_LogoutStore MODIFY _authSource VARCHAR(255) NOT NULL'];
                     break;
             }
 
             try {
-                $store->pdo->exec($query);
+                foreach ($update as $query) {
+                    $store->pdo->exec($query);
+                }
             } catch (\Exception $e) {
                 Logger::warning('Database error: '.var_export($store->pdo->errorInfo(), true));
                 return;
-- 
GitLab