Skip to content

Commit 8bef094

Browse files
committed
sqlsrv update for schema migration (#2238)
1 parent 0ece194 commit 8bef094

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/SimpleSAML/Store/SQLStore.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,20 @@ private function initTableVersionTable(): void
133133
* schema.
134134
*/
135135
$update = [
136-
'ALTER TABLE ' . $this->prefix . '_tableVersion DROP INDEX IF EXISTS SELECT CONSTRAINT_NAME ' .
137-
'FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME=' . $this->prefix . '_tableVersion',
138-
'ALTER TABLE ' . $this->prefix . '_tableVersion ADD CONSTRAINT _name PRIMARY KEY CLUSTERED (_name)',
136+
// Use dynamic SQL to drop the existing unique constraint
137+
'DECLARE @constraintName NVARCHAR(128); ' .
138+
'SELECT @constraintName = CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS ' .
139+
'WHERE TABLE_NAME = \'' . $this->prefix . '_tableVersion\' AND CONSTRAINT_TYPE = \'UNIQUE\'; ' .
140+
'IF @constraintName IS NOT NULL ' .
141+
'BEGIN ' .
142+
'EXEC(\'ALTER TABLE ' . $this->prefix . '_tableVersion ' .
143+
' DROP CONSTRAINT \' + @constraintName); ' .
144+
'END;',
145+
146+
// Add the new primary key constraint
147+
'ALTER TABLE ' . $this->prefix . '_tableVersion ' .
148+
' ADD CONSTRAINT PK_' . $this->prefix . '_tableVersion ' .
149+
' PRIMARY KEY CLUSTERED (_name);',
139150
];
140151
break;
141152
case 'sqlite':

0 commit comments

Comments
 (0)