ALTER TABLE gam.Session ADD SesOauthScope VARCHAR(1024) , ADD SesOauthTokenExpires integer , ADD SesOauthTokenMaxRenew smallint , ADD SesAutTypeName CHAR(60) , ADD SesExtToken2 VARCHAR(1024)

ALTER TABLE gam.SessionHistory ADD SesHisOauthScope VARCHAR(1024) , ADD SesHisOauthTokenExpires integer , ADD SesHisOauthTokenMaxRenew smallint , ADD SesHisAppTokenExp timestamp without time zone , ADD SesHisDeviceId CHAR(40) , ADD SesHisRefreshToken CHAR(40) , ADD SesHisAppId bigint , ADD SesHisExtToken2 VARCHAR(1024)

ALTER TABLE gam.Application ADD AppCliLocalLoginURL VARCHAR(1024) , ADD AppCliAllowGetUserAdditionalDa BOOLEAN , ADD AppCliAllowGetUserRoles BOOLEAN , ADD AppCliAllowRemoteAuth BOOLEAN
INSERT INTO gam.Repository (RepId, RepVer, RepUpg, RepDefAutTypeName, RepUserActType, RepUserActTimeOut, RepDefSecPolId, RepGenAud, RepUserRemMeTimeOut, RepGenSesSta, RepUserChrMinLog, RepGUID, RepCanRegUser, RepUserRemMeType, RepGiveAnoSes, RepAnoUserGUID, RepDefRoleId, RepCreDate, RepCreUser, RepUpdDate, RepUpdUser, RepNameSpace, RepUserRecPwdKeyTimeOut, RepUserReqPwd, RepUserReqEmail, RepUserReqFirstName, RepUserReqLastName, RepUserReqBir, RepUserReqGen, RepUserReqPhone, RepUserReqAddress, RepUserReqCity, RepUserReqState, RepUserReqCountry, RepUserReqPostCode, RepUserReqLng, RepUserReqTimeZone, RepUserReqPhoto, RepUserAttLogLockUser, RepUserAttLogLockSession, RepDsc, RepUserEmailSecAdm, RepUserTimeToUnblock, RepName, RepUserEmailUnique, RepAllowOauthAccess, RepUserIdentification, RepKeepSesOnlyOriIP, RepUserSessionCacheTimeout, RepAnoUserSDRoleId) SELECT DISTINCT RepId, ' ', ' ', Cast(NULL AS CHAR(60)), 'A', 36, Cast(NULL AS integer), 'None', 0, 'None', 3, ' ', FALSE, ' ', FALSE, Cast(NULL AS CHAR(40)), Cast(NULL AS bigint), to_timestamp( '1-1-1 0:0:0', 'YYYY-MM-DD HH24:MI:SS'), ' ', DATE '00010101', ' ', ' ', 120, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 8, 10, Cast(NULL AS CHAR(254)), Cast(NULL AS CHAR(254)), 30, ' ', FALSE, TRUE, 'name', FALSE, 60, Cast(NULL AS bigint) FROM gam.Application T WHERE NOT EXISTS (SELECT 1 FROM gam.Repository WHERE RepId = T.RepId)
INSERT INTO gam.Application (RepId, AppId, AppName, AppGUID, AppVer, AppComName, AppTypeId, AppParAppId, AppDsc, AppCopyright, AppOrder, AppRetMnuOptWithoutPrm, AppCurEnvId, AppBaseAppId, AppCreDate, AppCreUser, AppUpdDate, AppUpdUser, AppIsBaseApplication, AppCliId, AppCliSecret, AppCliUserGUID, AppCliImageURL, AppCliSiteURL, AppCliSiteDomain, AppCliRevoked, AppType, AppCliEncKey, AppCliDefPrmGUID, AppAccessReqPrm, AppCliAccessUniqueByUser, AppCliAutoRegAnomymousUser, AppCliAllowRemoteAuth, AppCliAllowGetUserRoles, AppCliAllowGetUserAdditionalDa, AppCliLocalLoginURL) SELECT DISTINCT RepId, AppId, AppName, AppGUID, AppVer, AppComName, AppTypeId, AppParAppId, AppDsc, AppCopyright, AppOrder, AppRetMnuOptWithoutPrm, AppCurEnvId, AppBaseAppId, AppCreDate, AppCreUser, AppUpdDate, AppUpdUser, AppIsBaseApplication, AppCliId, AppCliSecret, AppCliUserGUID, AppCliImageURL, AppCliSiteURL, AppCliSiteDomain, AppCliRevoked, AppType, AppCliEncKey, AppCliDefPrmGUID, AppAccessReqPrm, AppCliAccessUniqueByUser, AppCliAutoRegAnomymousUser, Cast(NULL AS BOOLEAN), Cast(NULL AS BOOLEAN), Cast(NULL AS BOOLEAN), Cast(NULL AS VARCHAR(1024)) FROM gam.Application T WHERE NOT EXISTS (SELECT 1 FROM gam.Application WHERE RepId = T.RepId AND AppId = T.AppId)
INSERT INTO gam.AppEnvironment (RepId, AppId, AppEnvId, AppEnvOrder, AppEnvGUID, AppEnvSrvHost, AppEnvSrvPort, AppEnvSrvVirDir, AppEnvSrvHttps, AppEnvSrvPgmPac, AppEnvSrvPgmExt, AppEnvName, AppEnvCreDate, AppEnvCreUser, AppEnvUpdDate, AppEnvUpdUser) SELECT DISTINCT RepId, AppId, AppCurEnvId, 0, '', '', Cast(NULL AS integer), Cast(NULL AS CHAR(120)), FALSE, Cast(NULL AS CHAR(254)), Cast(NULL AS CHAR(60)), Cast(NULL AS CHAR(120)), Cast(NULL AS timestamp without time zone), Cast(NULL AS VARCHAR(250)), DATE '00010101', Cast(NULL AS VARCHAR(250)) FROM gam.Application T WHERE NOT EXISTS (SELECT 1 FROM gam.AppEnvironment WHERE RepId = T.RepId AND AppId = T.AppId AND AppEnvId = T.AppCurEnvId) AND (NOT AppCurEnvId IS NULL )
ALTER TABLE gam.Application ALTER COLUMN AppCliSecret TYPE CHAR(120)
CREATE INDEX IAPPLICATIONENV ON gam.Application (RepId ,AppId ,AppCurEnvId )

CREATE TABLE gam.UserMemRoleProp (UserGUID CHAR(40) NOT NULL , RepId integer NOT NULL , UserMemRoleRoleId bigint NOT NULL , UserMemRolePropId CHAR(60) NOT NULL , UserMemRolePropToken CHAR(40) NOT NULL , UserMemRolePropValue VARCHAR(400) NOT NULL , PRIMARY KEY(UserGUID, RepId, UserMemRoleRoleId, UserMemRolePropId, UserMemRolePropToken))

CREATE TABLE gam.UserMemAppRoleProp (UserGUID CHAR(40) NOT NULL , RepId integer NOT NULL , UserMemAppRoleAppId bigint NOT NULL , UserMemAppRoleRoleId bigint NOT NULL , UserMemAppRolePropId CHAR(60) NOT NULL , UserMemAppRolePropToken CHAR(40) NOT NULL , UserMemAppRolePropValue VARCHAR(400) NOT NULL , PRIMARY KEY(UserGUID, RepId, UserMemAppRoleAppId, UserMemAppRoleRoleId, UserMemAppRolePropId, UserMemAppRolePropToken))

CREATE TABLE gam.SecurityPolicyProp (RepId integer NOT NULL , SecPolId integer NOT NULL , SecPolPropId CHAR(60) NOT NULL , SecPolPropToken CHAR(40) NOT NULL , SecPolPropValue VARCHAR(400) NOT NULL , PRIMARY KEY(RepId, SecPolId, SecPolPropId, SecPolPropToken))

DROP INDEX gam.IUSERMEMAPPROLE1
CREATE INDEX IUSERMEMAPPROLEAPP ON gam.UserMemAppRole (RepId ,UserMemAppRoleAppId )
CREATE INDEX IUSERMEMAPPROLEROLE ON gam.UserMemAppRole (RepId ,UserMemAppRoleRoleId )

ALTER TABLE gam.SessionHistoryHisLogAtt RENAME TO SessionHistoryLogAtt

UPDATE gam.SysPar SET SysParVal='3.0.6' WHERE SysParId=( 'GAMVersion')
