/*
 Migrates the Airlock IAM DB schema from version 8.1 to version 8.2.
 Written for MSSQL.
*/

CREATE TABLE oauth2_consent
(
    id                      NVARCHAR(36) COLLATE SQL_Latin1_General_CP1_CS_AS  NOT NULL,
    user_id                 NVARCHAR(100) COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL,
    authorization_server_id NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CS_AS  NOT NULL,
    client_id               NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CS_AS  NOT NULL,
    scope_name              NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CS_AS  NOT NULL,
    status                  NVARCHAR(20) COLLATE SQL_Latin1_General_CP1_CS_AS  NOT NULL,
    updated_at              BIGINT                                             NOT NULL,
    tenant_id               NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL
);
-- Additional indexes
CREATE UNIQUE INDEX ux_oauth2_consent_id
    ON oauth2_consent (id);

CREATE INDEX ix_oauth2_consent_user_as_client
    ON oauth2_consent (user_id, authorization_server_id, client_id);
