CREATE OR REPLACE PROCEDURE ADD_PRIMARY_KEY_COLUMN(TABLE_NAME IN VARCHAR2, SEQUENCE_NAME IN VARCHAR2, TRIGGER_NAME IN VARCHAR2)
    IS
BEGIN
    DECLARE
        ADD_COLUMN_STATEMENT VARCHAR2(300);
        SEQ_STATEMENT VARCHAR2(400);
        TRIG_STATEMENT VARCHAR2(600);
        UPDATE_STATEMENT VARCHAR2(300);
        PK_STATEMENT VARCHAR2(300);

    BEGIN
        ADD_COLUMN_STATEMENT := 'ALTER TABLE ' || TABLE_NAME || ' ADD ID INTEGER DEFAULT 0 NOT NULL';
        SEQ_STATEMENT := 'CREATE SEQUENCE ' || SEQUENCE_NAME || ' START WITH 1 INCREMENT BY 1 NOCACHE';
        TRIG_STATEMENT := 'CREATE OR REPLACE TRIGGER ' || TRIGGER_NAME || ' BEFORE INSERT ON ' || TABLE_NAME || ' REFERENCING NEW AS NEW FOR EACH ROW BEGIN SELECT ' || SEQUENCE_NAME || '.nextval INTO :NEW.ID FROM dual; END;';
        UPDATE_STATEMENT := 'UPDATE ' || TABLE_NAME || ' SET ID = ' || SEQUENCE_NAME || '.NEXTVAL';
        PK_STATEMENT := 'ALTER TABLE ' || TABLE_NAME || ' ADD PRIMARY KEY (ID)';

        EXECUTE IMMEDIATE ADD_COLUMN_STATEMENT;
        EXECUTE IMMEDIATE SEQ_STATEMENT;
        EXECUTE IMMEDIATE TRIG_STATEMENT;
        EXECUTE IMMEDIATE UPDATE_STATEMENT;
        EXECUTE IMMEDIATE PK_STATEMENT;
        COMMIT;
    END;
END;
/

CALL ADD_PRIMARY_KEY_COLUMN('IDN_OAUTH2_ACCESS_TOKEN_AUDIT', 'IDN_OAUTH2_ACCESS_TKN_ADT_SEQ', 'IDN_OAUTH2_ACCESS_TKN_ADT_TRIG')
/

CALL ADD_PRIMARY_KEY_COLUMN('IDN_OAUTH2_SCOPE_BINDING', 'IDN_OAUTH2_SCOPE_BINDING_SEQ', 'IDN_OAUTH2_SCOPE_BINDING_TRIG')
/

CALL ADD_PRIMARY_KEY_COLUMN('IDN_AUTH_USER_SESSION_MAPPING', 'IDN_AUTH_USER_SESSION_MAP_SEQ', 'IDN_AUTH_USER_SESSION_MAP_TRIG')
/

CALL ADD_PRIMARY_KEY_COLUMN('IDN_OAUTH2_CIBA_REQUEST_SCOPES', 'IDN_OAUTH2_CIBA_SCOPES_SEQ', 'IDN_OAUTH2_CIBA_SCOPES_TRIG')
/

ALTER TABLE IDN_OAUTH2_ACCESS_TOKEN ADD CONSENTED_TOKEN VARCHAR(6)
/

ALTER TABLE IDN_FED_AUTH_SESSION_MAPPING DROP PRIMARY KEY
/

CALL ADD_PRIMARY_KEY_COLUMN('IDN_FED_AUTH_SESSION_MAPPING')
/

ALTER TABLE IDN_FED_AUTH_SESSION_MAPPING ADD COLUMN TENANT_ID INTEGER DEFAULT 0 NOT NULL
/

ALTER TABLE IDN_FED_AUTH_SESSION_MAPPING ADD UNIQUE (IDP_SESSION_ID, TENANT_ID)
/

DROP PROCEDURE ADD_PRIMARY_KEY_COLUMN
/
