ALTER TABLE IDN_OAUTH2_ACCESS_TOKEN_AUDIT ADD ID INTEGER NOT NULL IDENTITY PRIMARY KEY;

ALTER TABLE IDN_OAUTH2_SCOPE_BINDING ADD ID INTEGER NOT NULL IDENTITY PRIMARY KEY;

ALTER TABLE IDN_AUTH_USER_SESSION_MAPPING ADD ID INTEGER NOT NULL IDENTITY PRIMARY KEY;

ALTER TABLE IDN_OAUTH2_CIBA_REQUEST_SCOPES ADD ID INTEGER NOT NULL IDENTITY PRIMARY KEY;

-- Check if the column exists.
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'IDN_OAUTH2_ACCESS_TOKEN' AND COLUMN_NAME = 'CONSENTED_TOKEN')
ALTER TABLE IDN_OAUTH2_ACCESS_TOKEN ADD CONSENTED_TOKEN VARCHAR(6);

DECLARE @COMMAND NVARCHAR(200);SELECT @COMMAND='ALTER TABLE IDN_FED_AUTH_SESSION_MAPPING DROP CONSTRAINT ' + NAME + ';' FROM   sys.key_constraints WHERE  [type] = 'PK' AND [parent_object_id] = Object_id('dbo.IDN_FED_AUTH_SESSION_MAPPING');EXEC (@COMMAND);
ALTER TABLE IDN_FED_AUTH_SESSION_MAPPING ADD ID INTEGER NOT NULL IDENTITY PRIMARY KEY;
ALTER TABLE IDN_FED_AUTH_SESSION_MAPPING ADD TENANT_ID INTEGER DEFAULT 0 NOT NULL;
ALTER TABLE IDN_FED_AUTH_SESSION_MAPPING ADD UNIQUE(IDP_SESSION_ID, TENANT_ID);
