-- IDN_OAUTH2_TOKEN_BINDING table --
CREATE OR REPLACE PROCEDURE CREATE_LARGE_TABLE_SPACE
BEGIN
	IF NOT EXISTS (SELECT * FROM SYSCAT.BUFFERPOOLS WHERE BPNAME = 'BP32K')
	THEN
		EXECUTE IMMEDIATE 'CREATE BUFFERPOOL BP32K IMMEDIATE SIZE 250 AUTOMATIC PAGESIZE 32K';
	END IF;
    IF NOT EXISTS (SELECT * FROM SYSIBM.SYSTABLESPACES WHERE TBSPACE = 'TS32K')
    THEN
    	EXECUTE IMMEDIATE 'CREATE LARGE TABLESPACE TS32K PAGESIZE 32K MANAGED by AUTOMATIC STORAGE BUFFERPOOL BP32K';
    END IF;
END
/

CALL CREATE_LARGE_TABLE_SPACE
/

DROP PROCEDURE CREATE_LARGE_TABLE_SPACE
/

CREATE OR REPLACE PROCEDURE EXECUTE_ADMIN_MOVE_TABLE
BEGIN
	IF NOT EXISTS (SELECT * FROM SYSCAT.TABLES where TABNAME = 'IDN_OAUTH2_TOKEN_BINDING' AND INDEX_TBSPACE = 'TS32K')
	THEN
		CALL SYSPROC.ADMIN_MOVE_TABLE(
		(SELECT TABSCHEMA FROM SYSCAT.TABLES where TABNAME = 'IDN_OAUTH2_TOKEN_BINDING'),
		'IDN_OAUTH2_TOKEN_BINDING',
		(SELECT TBSPACE FROM SYSCAT.TABLES where TABNAME = 'IDN_OAUTH2_TOKEN_BINDING'),
		'TS32K',
		(SELECT TBSPACE FROM SYSCAT.TABLES where TABNAME = 'IDN_OAUTH2_TOKEN_BINDING'),
		'',
		'',
		'',
		'',
		'',
		'MOVE');
	END IF;
END
/

CALL EXECUTE_ADMIN_MOVE_TABLE
/

DROP PROCEDURE EXECUTE_ADMIN_MOVE_TABLE
/

ALTER TABLE IDN_OAUTH2_TOKEN_BINDING ADD UNIQUE (TOKEN_ID,TOKEN_BINDING_TYPE,TOKEN_BINDING_VALUE)
/

ALTER TABLE IDN_OAUTH2_TOKEN_BINDING DROP PRIMARY KEY
/
