-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add saml idp metadata cleanup scripts
- Loading branch information
Showing
2 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...ed-procedures/mssql/saml-idp-metadata-cleanup/mssql-saml-idp-metadata-cleanup-restore.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
CREATE OR ALTER PROCEDURE WSO2_SAML_IDP_METADATA_CLEANUP_RESTORE AS | ||
BEGIN TRY | ||
-- ------------------------------------------ | ||
-- DECLARE VARIABLES | ||
-- ------------------------------------------ | ||
DECLARE @rowCount INT | ||
DECLARE @enableLog BIT | ||
|
||
-- ------------------------------------------ | ||
-- CONFIGURABLE ATTRIBUTES | ||
-- ------------------------------------------ | ||
SET @enableLog = 1 -- ENABLE LOGGING [DEFAULT : TRUE] | ||
|
||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] SAML IDP METADATA CLEANUP DATA RESTORATION STARTED .... !' AS 'INFO_LOG' | ||
END | ||
|
||
SELECT @rowCount = COUNT(1) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME IN ('REG_RESOURCE') | ||
IF (@rowCount = 1) | ||
BEGIN | ||
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; | ||
BEGIN TRANSACTION; | ||
|
||
SET IDENTITY_INSERT REG_RESOURCE ON; | ||
INSERT INTO REG_RESOURCE (REG_PATH_ID, REG_NAME, REG_VERSION, REG_MEDIA_TYPE, REG_CREATOR, REG_CREATED_TIME, | ||
REG_LAST_UPDATOR, REG_LAST_UPDATED_TIME, REG_DESCRIPTION, REG_CONTENT_ID, REG_TENANT_ID, REG_UUID) SELECT | ||
A.REG_PATH_ID, A.REG_NAME, A.REG_VERSION, A.REG_MEDIA_TYPE, A.REG_CREATOR, A.REG_CREATED_TIME, A.REG_LAST_UPDATOR, | ||
A.REG_LAST_UPDATED_TIME, A.REG_DESCRIPTION, A.REG_CONTENT_ID, A.REG_TENANT_ID, A.REG_UUID FROM BAK_REG_RESOURCE A LEFT | ||
JOIN REG_RESOURCE B ON A.REG_VERSION = B.REG_VERSION AND A.REG_TENANT_ID = B.REG_TENANT_ID WHERE B.REG_VERSION IS NULL; | ||
SET @rowCount = @@ROWCOUNT | ||
SET IDENTITY_INSERT REG_RESOURCE OFF; | ||
|
||
SET IDENTITY_INSERT REG_CONTENT ON; | ||
INSERT INTO REG_CONTENT (REG_CONTENT_ID, REG_CONTENT_DATA, REG_TENANT_ID) SELECT A.REG_CONTENT_ID, | ||
A.REG_CONTENT_DATA, A.REG_TENANT_ID FROM BAK_REG_CONTENT A LEFT JOIN REG_CONTENT B ON A.REG_CONTENT_ID = B.REG_CONTENT_ID WHERE B.REG_CONTENT_ID IS NULL; | ||
SET IDENTITY_INSERT REG_CONTENT OFF; | ||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT CONCAT('CLEANUP DATA RESTORATION COMPLETED ON REG_RESOURCE WITH ', @rowCount) AS 'INFO_LOG' | ||
END | ||
COMMIT TRANSACTION; | ||
END | ||
|
||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] CLEANUP DATA RESTORATION COMPLETED .... !' AS 'INFO_LOG' | ||
END | ||
END TRY | ||
BEGIN CATCH | ||
|
||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] ERROR OCCURRED: ' + ERROR_MESSAGE() AS 'ERROR LOG' | ||
END | ||
|
||
ROLLBACK TRANSACTION | ||
END CATCH |
135 changes: 135 additions & 0 deletions
135
...pts/stored-procedures/mssql/saml-idp-metadata-cleanup/mssql-saml-idp-metadata-cleanup.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
CREATE OR ALTER PROCEDURE WSO2_SAML_IDP_METADATA_CLEANUP AS | ||
BEGIN TRY | ||
|
||
-- ------------------------------------------ | ||
-- DECLARE VARIABLES | ||
-- ------------------------------------------ | ||
DECLARE @batchSize INT | ||
DECLARE @batchCount INT | ||
DECLARE @rowCount INT | ||
DECLARE @enableLog BIT | ||
DECLARE @backupTables BIT | ||
|
||
-- ------------------------------------------ | ||
-- CONFIGURABLE VARIABLES | ||
-- ------------------------------------------ | ||
SET @batchSize = 10000 -- SET BATCH SIZE TO AVOID TABLE LOCKS [DEFAULT : 10000] | ||
SET @enableLog = 1 -- ENABLE LOGGING [DEFAULT : 1] | ||
SET @backupTables = 1 -- SET IF REGISTRY TABLES NEEDS TO BE BACKED-UP BEFORE DELETE [DEFAULT : 1]. | ||
|
||
SET @rowCount = 0 | ||
SET @batchCount = 1 | ||
|
||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] WSO2_SAML_IDP_METADATA_CLEANUP() STARTED...!' AS 'INFO LOG' | ||
END | ||
|
||
-- ------------------------------------------ | ||
-- GET PATH ID LIST TO DELETE | ||
-- ------------------------------------------ | ||
DECLARE @RootPathId INT | ||
SELECT @RootPathId = REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE = '/_system/governance/repository/identity/provider/saml' | ||
|
||
DECLARE @PathIdList TABLE (REG_PATH_ID INT) | ||
INSERT INTO @PathIdList (REG_PATH_ID) SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_PARENT_ID = @RootPathId | ||
|
||
-- ------------------------------------------ | ||
-- BACKUP DATA | ||
-- ------------------------------------------ | ||
IF (@backupTables = 1) | ||
BEGIN | ||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] TABLE BACKUP STARTED ... !' AS 'INFO LOG' | ||
END | ||
|
||
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'BAK_REG_RESOURCE')) | ||
BEGIN | ||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] DELETING OLD BACKUP...' AS 'INFO LOG' | ||
END | ||
DROP TABLE BAK_REG_RESOURCE | ||
DROP TABLE BAK_REG_CONTENT | ||
END | ||
|
||
-- BACKUP REG_RESOURCE TABLE | ||
SELECT * INTO BAK_REG_RESOURCE FROM REG_RESOURCE WHERE REG_PATH_ID IN (SELECT REG_PATH_ID FROM @PathIdList) | ||
|
||
-- BACKUP REG_CONTENT TABLE | ||
DECLARE @ContentIdList TABLE (REG_CONTENT_ID INT) | ||
INSERT INTO @ContentIdList (REG_CONTENT_ID) SELECT DISTINCT REG_CONTENT_ID FROM BAK_REG_RESOURCE WHERE REG_CONTENT_ID IS NOT NULL | ||
SELECT * INTO BAK_REG_CONTENT FROM REG_CONTENT WHERE REG_CONTENT_ID IN (SELECT REG_CONTENT_ID FROM @ContentIdList) | ||
END | ||
|
||
-- ------------------------------------------ | ||
-- CLEANUP DATA | ||
-- ------------------------------------------ | ||
|
||
BEGIN TRANSACTION | ||
|
||
WHILE (@batchCount > 0) | ||
BEGIN | ||
-- CREATE BATCH TABLE | ||
DROP TABLE IF EXISTS REG_RESOURCE_BATCH_TMP | ||
DROP TABLE IF EXISTS REG_CONTENT_BATCH_TMP | ||
|
||
CREATE TABLE REG_RESOURCE_BATCH_TMP(REG_VERSION INT, REG_TENANT_ID INT, REG_CONTENT_ID INT) | ||
CREATE TABLE REG_CONTENT_BATCH_TMP(REG_CONTENT_ID INT) | ||
|
||
INSERT INTO REG_RESOURCE_BATCH_TMP(REG_VERSION, REG_TENANT_ID, REG_CONTENT_ID) SELECT TOP (@batchSize) REG_VERSION, | ||
REG_TENANT_ID, REG_CONTENT_ID FROM REG_RESOURCE WHERE REG_PATH_ID IN (SELECT REG_PATH_ID FROM @PathIdList) | ||
SET @batchCount = @@ROWCOUNT | ||
INSERT INTO REG_CONTENT_BATCH_TMP(REG_CONTENT_ID) SELECT TOP (@batchSize) REG_CONTENT_ID | ||
FROM REG_RESOURCE_BATCH_TMP WHERE REG_CONTENT_ID IS NOT NULL | ||
|
||
IF (@batchCount = 0) | ||
BEGIN | ||
BREAK | ||
END | ||
|
||
CREATE INDEX REG_RESOURCE_BATCH_TMP on REG_RESOURCE_BATCH_TMP (REG_VERSION, REG_TENANT_ID) | ||
CREATE INDEX REG_CONTENT_BATCH_TMP on REG_CONTENT_BATCH_TMP (REG_CONTENT_ID) | ||
|
||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] CREATED REG_RESOURCE_BATCH_TMP AND REG_CONTENT_BATCH_TMP...' AS 'INFO LOG' | ||
END | ||
|
||
-- BATCH DELETION | ||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] BATCH DELETE STARTED ON REG_RESOURCE...' AS 'INFO LOG' | ||
END | ||
DELETE FROM REG_RESOURCE WHERE REG_VERSION IN (SELECT REG_VERSION FROM REG_RESOURCE_BATCH_TMP) AND REG_TENANT_ID | ||
IN (SELECT REG_TENANT_ID FROM REG_RESOURCE_BATCH_TMP) | ||
SET @rowCount = @@ROWCOUNT | ||
DELETE FROM REG_CONTENT WHERE REG_CONTENT_ID IN (SELECT REG_CONTENT_ID FROM REG_CONTENT_BATCH_TMP) | ||
|
||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT CONCAT('BATCH DELETE FINISHED ON REG_RESOURCE : ', @rowCount) AS 'INFO LOG' | ||
END | ||
END | ||
|
||
-- DELETE TEMP TABLES | ||
DROP TABLE IF EXISTS REG_RESOURCE_BATCH_TMP | ||
DROP TABLE IF EXISTS REG_CONTENT_BATCH_TMP | ||
|
||
COMMIT TRANSACTION | ||
|
||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] CLEANUP COMPLETED...!' AS 'INFO_LOG' | ||
END | ||
END TRY | ||
BEGIN CATCH | ||
|
||
IF (@enableLog = 1) | ||
BEGIN | ||
SELECT '[' + convert(varchar, getdate(), 121) + '] ERROR OCCURRED: ' + ERROR_MESSAGE() AS 'ERROR LOG' | ||
END | ||
|
||
ROLLBACK TRANSACTION | ||
END CATCH |