@@ -33,7 +33,85 @@ class LogoutStore
3333 private static function createLogoutTable (SQLStore $ store ): void
3434 {
3535 $ tableVer = $ store ->getTableVersion ('saml_LogoutStore ' );
36- if ($ tableVer === 4 ) {
36+ if ($ tableVer === 5 ) {
37+ return ;
38+ } elseif ($ tableVer === 4 ) {
39+ // The _authSource index is being changed from UNIQUE to PRIMARY KEY for table version 5.
40+ switch ($ store ->driver ) {
41+ case 'pgsql ' :
42+ // Drop old index and add primary key
43+ $ update = [
44+ 'ALTER TABLE ' . $ store ->prefix . '_saml_LogoutStore DROP CONSTRAINT IF EXISTS ' .
45+ $ store ->prefix . '_saml_LogoutStore___authSource__nameId__sessionIndex_key ' ,
46+ 'ALTER TABLE ' . $ store ->prefix . '_saml_LogoutStore ADD PRIMARY KEY ' .
47+ '(_authSource, _nameId, _sessionIndex) '
48+ ];
49+ break ;
50+ case 'sqlsrv ' :
51+ /**
52+ * Drop old index and add primary key.
53+ * NOTE: We get the name of the index by looking for the only unique index with a default name.
54+ */
55+ $ update = [
56+ 'ALTER TABLE ' . $ store ->prefix . '_saml_LogoutStore DROP INDEX IF EXISTS SELECT CONSTRAINT_NAME ' .
57+ 'FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME= ' . $ store ->prefix . '_saml_LogoutStore ' .
58+ 'AND CONSTRAINT_NAME LIKE \'UQ__%" \'' ,
59+ 'ALTER TABLE ' . $ store ->prefix . '_saml_LogoutStore ADD CONSTRAINT _authSource ' .
60+ 'PRIMARY KEY CLUSTERED (_authSource, _nameId, _sessionIndex) '
61+ ];
62+ break ;
63+ case 'sqlite ' :
64+ /**
65+ * Because SQLite does not support field alterations, the approach is to:
66+ * Create a new table with the primary key
67+ * Copy the current data to the new table
68+ * Drop the old table
69+ * Rename the new table correctly
70+ */
71+ $ update = [
72+ 'CREATE TABLE ' . $ store ->prefix .
73+ '_saml_LogoutStore_new (_authSource VARCHAR(255) NOT NULL, ' .
74+ '_nameId VARCHAR(40) NOT NULL, _sessionIndex VARCHAR(50) NOT NULL, ' .
75+ '_expire TIMESTAMP NOT NULL, _sessionId VARCHAR(50) NOT NULL, PRIMARY KEY ' .
76+ '(_authSource, _nameID, _sessionIndex)) ' ,
77+ 'INSERT INTO ' . $ store ->prefix . '_saml_LogoutStore_new SELECT * FROM ' .
78+ $ store ->prefix . '_saml_LogoutStore ' ,
79+ 'DROP TABLE ' . $ store ->prefix . '_saml_LogoutStore ' ,
80+ 'ALTER TABLE ' . $ store ->prefix . '_saml_LogoutStore_new RENAME TO ' .
81+ $ store ->prefix . '_saml_LogoutStore ' ,
82+ 'CREATE INDEX ' . $ store ->prefix . '_saml_LogoutStore_expire ON ' .
83+ $ store ->prefix . '_saml_LogoutStore (_expire) ' ,
84+ 'CREATE INDEX ' . $ store ->prefix . '_saml_LogoutStore_nameId ON ' .
85+ $ store ->prefix . '_saml_LogoutStore (_authSource, _nameId) '
86+ ];
87+ break ;
88+ case 'mysql ' :
89+ // Drop old index and add primary key
90+ $ update = [
91+ 'ALTER TABLE ' . $ store ->prefix . '_saml_LogoutStore DROP INDEX _authSource ' ,
92+ 'ALTER TABLE ' . $ store ->prefix . '_saml_LogoutStore ADD PRIMARY KEY ' .
93+ '(_authSource(191), _nameId, _sessionIndex) '
94+ ];
95+ break ;
96+ default :
97+ // Drop old index and add primary key
98+ $ update = [
99+ 'ALTER TABLE ' . $ store ->prefix . '_saml_LogoutStore DROP INDEX _authSource ' ,
100+ 'ALTER TABLE ' . $ store ->prefix . '_saml_LogoutStore ADD PRIMARY KEY ' .
101+ '(_authSource, _nameId, _sessionIndex) '
102+ ];
103+ break ;
104+ }
105+
106+ try {
107+ foreach ($ update as $ query ) {
108+ $ store ->pdo ->exec ($ query );
109+ }
110+ } catch (Exception $ e ) {
111+ Logger::warning ('Database error: ' . var_export ($ store ->pdo ->errorInfo (), true ));
112+ return ;
113+ }
114+ $ store ->setTableVersion ('saml_LogoutStore ' , 5 );
37115 return ;
38116 } elseif ($ tableVer < 4 && $ tableVer > 0 ) {
39117 throw new Exception (
@@ -48,7 +126,7 @@ private static function createLogoutTable(SQLStore $store): void
48126 _sessionIndex VARCHAR(50) NOT NULL,
49127 _expire ' . ($ store ->driver === 'pgsql ' ? 'TIMESTAMP ' : 'DATETIME ' ) . ' NOT NULL,
50128 _sessionId VARCHAR(50) NOT NULL,
51- UNIQUE (_authSource ' . ($ store ->driver === 'mysql ' ? '(191) ' : '' ) . ', _nameID , _sessionIndex)
129+ PRIMARY KEY (_authSource ' . ($ store ->driver === 'mysql ' ? '(191) ' : '' ) . ', _nameId , _sessionIndex)
52130 ) ' ;
53131 $ store ->pdo ->exec ($ query );
54132
@@ -61,7 +139,7 @@ private static function createLogoutTable(SQLStore $store): void
61139 ', _nameId) ' ;
62140 $ store ->pdo ->exec ($ query );
63141
64- $ store ->setTableVersion ('saml_LogoutStore ' , 4 );
142+ $ store ->setTableVersion ('saml_LogoutStore ' , 5 );
65143 }
66144
67145
0 commit comments