ALTER TABLE IDN_OAUTH_CONSUMER_APPS ADD ID_TOKEN_EXPIRE_TIME BIGINT DEFAULT 3600 WITH VALUES;

IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[IDN_AUTH_TEMP_SESSION_DATA_STORE]') AND TYPE IN (N'U'))
CREATE TABLE IDN_AUTH_TEMP_SESSION_STORE (
  SESSION_ID VARCHAR (100) NOT NULL,
  SESSION_TYPE VARCHAR(100) NOT NULL,
  OPERATION VARCHAR(10) NOT NULL,
  SESSION_OBJECT VARBINARY(MAX),
  TIME_CREATED BIGINT,
  TENANT_ID INTEGER DEFAULT -1,
  EXPIRY_TIME BIGINT,
  PRIMARY KEY (SESSION_ID, SESSION_TYPE, TIME_CREATED, OPERATION)
);

ALTER TABLE IDN_AUTH_SESSION_STORE ADD EXPIRY_TIME BIGINT;

IF EXISTS (SELECT *  FROM sys.indexes WHERE name='IDX_AT_TI_UD') begin DROP INDEX IDN_OAUTH2_ACCESS_TOKEN.IDX_AT_TI_UD; end;

IF EXISTS (SELECT *  FROM sys.indexes WHERE name='IDX_AUTHORIZATION_CODE_AU_TI') begin DROP INDEX IDN_OAUTH2_AUTHORIZATION_CODE.IDX_AUTHORIZATION_CODE_AU_TI; end;

IF EXISTS (SELECT *  FROM sys.indexes WHERE name='IDX_IDN_SCIM_GROUP_TI_RN') begin DROP INDEX IDN_SCIM_GROUP.IDX_IDN_SCIM_GROUP_TI_RN; end;

IF EXISTS (SELECT *  FROM sys.indexes WHERE name='IDX_IDN_SCIM_GROUP_TI_RN_AN') begin DROP INDEX IDN_SCIM_GROUP.IDX_IDN_SCIM_GROUP_TI_RN_AN; end;

IF EXISTS (SELECT *  FROM sys.indexes WHERE name='IDX_IDN_AUTH_SESSION_TIME') begin DROP INDEX IDN_AUTH_SESSION_STORE.IDX_IDN_AUTH_SESSION_TIME; end;

IF EXISTS (SELECT *  FROM sys.indexes WHERE name='IDX_IDN_AUTH_TMP_SESSION_TIME') begin DROP INDEX IDN_AUTH_TEMP_SESSION_STORE.IDX_IDN_AUTH_TMP_SESSION_TIME; end;

CREATE INDEX IDX_AT_TI_UD ON IDN_OAUTH2_ACCESS_TOKEN(AUTHZ_USER, TENANT_ID, TOKEN_STATE, USER_DOMAIN);

CREATE INDEX IDX_AUTHORIZATION_CODE_AU_TI ON IDN_OAUTH2_AUTHORIZATION_CODE (AUTHZ_USER,TENANT_ID, USER_DOMAIN, STATE);

CREATE INDEX IDX_IDN_SCIM_GROUP_TI_RN ON IDN_SCIM_GROUP (TENANT_ID, ROLE_NAME);

CREATE INDEX IDX_IDN_SCIM_GROUP_TI_RN_AN ON IDN_SCIM_GROUP (TENANT_ID, ROLE_NAME, ATTR_NAME);

CREATE INDEX IDX_IDN_AUTH_SESSION_TIME ON IDN_AUTH_SESSION_STORE (TIME_CREATED);

CREATE INDEX IDX_IDN_AUTH_TMP_SESSION_TIME ON IDN_AUTH_TEMP_SESSION_STORE (TIME_CREATED);
