/*
 Migrates the Airlock IAM DB schema from version 8.2 to version 8.3.
 Written for PostgreSQL.
 */

CREATE TABLE oauth2_par_request
(
    request_uri_reference   VARCHAR(50) NOT NULL,
    authorization_server_id VARCHAR(50) NOT NULL,
    client_id               VARCHAR(50) NOT NULL,
    query_string            TEXT        NOT NULL,
    valid_until             BIGINT      NOT NULL,
    tenant_id               VARCHAR(50) NOT NULL
);
-- Additional indexes
CREATE UNIQUE INDEX ux_oauth2_par_request_uri ON oauth2_par_request (request_uri_reference);
CREATE INDEX ix_oauth2_par_request_valid_until ON oauth2_par_request (valid_until ASC);

CREATE TABLE oauth2_accepted_client_assertions
(
    jti        VARCHAR(100) NOT NULL,
    expires_at BIGINT       NOT NULL,
    tenant_id  VARCHAR(50)  NOT NULL
);
-- Additional indexes
CREATE UNIQUE INDEX ux_oauth2_accepted_client_assertions ON oauth2_accepted_client_assertions (jti);
CREATE INDEX ix_oauth2_accepted_client_assertions_expires_at ON oauth2_accepted_client_assertions (expires_at ASC);

ALTER TABLE oauth2_session
    ADD COLUMN claims TEXT;
