create table BACKUP (SELECT * FROM IDN_OAUTH2_ACCESS_TOKEN);
CREATE TABLE  IDN_OAUTH2_ACCESS_TOKEN1 (ACCESS_TOKEN varchar(255) NOT NULL DEFAULT '',TIME_CREATED timestamp NOT NULL);
INSERT  INTO IDN_OAUTH2_ACCESS_TOKEN1 select ACCESS_TOKEN, TIME_CREATED from IDN_OAUTH2_ACCESS_TOKEN AS t where TIME_CREATED IN (SELECT MAX(TIME_CREATED) AS MAXTIME FROM IDN_OAUTH2_ACCESS_TOKEN group by CONSUMER_KEY, AUTHZ_USER,TOKEN_SCOPE,TOKEN_STATE,USER_TYPE);
DELETE FROM IDN_OAUTH2_ACCESS_TOKEN WHERE ACCESS_TOKEN NOT IN (SELECT ACCESS_TOKEN FROM IDN_OAUTH2_ACCESS_TOKEN1);
DROP TABLE IDN_OAUTH2_ACCESS_TOKEN1;
DELETE FROM IDN_OAUTH2_ACCESS_TOKEN WHERE USER_TYPE IS NULL;
update IDN_OAUTH2_ACCESS_TOKEN set USER_TYPE = 'APPLICATION' WHERE USER_TYPE = 'DEVELOPER';
update IDN_OAUTH2_ACCESS_TOKEN set USER_TYPE = 'APPLICATION_USER' where USER_TYPE = 'APPLICATION';
ALTER TABLE IDN_OAUTH2_ACCESS_TOKEN ADD(TOKEN_STATE_ID varchar(256) default 'NONE');
CREATE INDEX IDX_AT_CK_AU ON IDN_OAUTH2_ACCESS_TOKEN(CONSUMER_KEY, AUTHZ_USER, TOKEN_STATE, USER_TYPE);
ALTER TABLE IDN_OAUTH2_ACCESS_TOKEN ADD CONSTRAINT CON_APP_KEY UNIQUE (CONSUMER_KEY, AUTHZ_USER,TOKEN_SCOPE,TOKEN_STATE,TOKEN_STATE_ID);
DELETE FROM IDN_OAUTH2_ACCESS_TOKEN WHERE USER_TYPE IS NULL;
