       update tjsm T set "releaseName" = T."gitTagName" where "isRelease" = true and "releaseName" is null;
 
       -- update contextTypeCode, contextValue
-      update tjsm set "contextTypeCode" = 302, "contextValue" = tjsm."gitTagName" where "isGitTag" = true;
-      update tjsm set "contextTypeCode" = 303, "contextValue" = tjsm."releaseName" where "isRelease" = true;
-      update tjsm set "contextTypeCode" = 304, "contextValue" = tjsm."pullRequestNumber" where "isPullRequest" = true;
-      update tjsm set "contextTypeCode" = 301, "contextValue" = tjsm.bn where "isPullRequest" = false and "isGitTag" = false and "isRelease" = false;
+      update tjsm set "contextTypeCode" = 302, "contextValue" = tjsm."gitTagName", "contextDetail" = tjsm."commitSha" where "isGitTag" = true;
+      update tjsm set "contextTypeCode" = 303, "contextValue" = tjsm."releaseName", "contextDetail" = tjsm."gitTagName" where "isRelease" = true;
+      update tjsm set "contextTypeCode" = 304, "contextValue" = tjsm."pullRequestNumber", "contextDetail" = tjsm.bn where "isPullRequest" = true;
+      update tjsm set "contextTypeCode" = 301, "contextValue" = tjsm.bn, "contextDetail" = tjsm."commitSha" where "isPullRequest" = false and "isGitTag" = false and "isRelease" = false;
       update tjsm set "updatedBy" = tjsm."createdBy" where "updatedBy" is null;
 
       -- update lastSuccessfulJobId, lastUnstableJobId, lastTimedoutJobId, lastFailedJobId

      -- Migration Script to populate jobstatesMap for runs
      create temp table tjsm as WITH cte AS
      (
         SELECT "id","projectId","branchName","runNumber","createdAt","statusCode","isGitTag","isPullRequest","pullRequestNumber","isRelease","gitTagName","releaseName","subscriptionId", "createdBy", "updatedBy", "updatedAt", ROW_NUMBER() OVER (PARTITION BY "projectId","branchName","statusCode","isGitTag","isPullRequest","isRelease" ORDER BY "createdAt" desc)
      AS rn FROM runs WHERE "statusCode" in (30,50,60,80) and "branchName" is not null
      )
      SELECT "id", "projectId" as pid,"branchName" as bn,"runNumber","createdAt","statusCode","isGitTag","isPullRequest","pullRequestNumber","isRelease","gitTagName","releaseName","subscriptionId", "createdBy", "updatedBy", "updatedAt"
      FROM cte
      WHERE rn = 1;

      -- add required columns to temp table tjsm
      alter table tjsm add column "contextTypeCode" int, add column "contextValue" varchar(255), add column "lastSuccessfulJobId" varchar(24), add column "lastFailedJobId" varchar(24), add column "lastUnstableJobId" varchar(24), add column "lastTimedoutJobId" varchar(24);

      -- update null fields
      update tjsm T set "isPullRequest" = false where "isPullRequest" is null;

      -- creating a temp table temptjsm which have only latest records
      create temp table temptjsm as WITH cte AS
      (
         SELECT id,pid,rid, "createdAt", "contextTypeCode", "contextValue", "contextDetail", "statusCode", "subscriptionId", "createdBy", "updatedBy", "updatedAt", "lastSuccessfulJobId", "lastFailedJobId", ROW_NUMBER() OVER (PARTITION BY "pid", "contextTypeCode", "contextValue" ORDER BY "createdAt" desc)
      AS rn FROM tjsm
      )
      SELECT "id",pid,rid, "createdAt", "statusCode", "contextTypeCode", "contextValue", "contextDetail", "lastSuccessfulJobId", "lastFailedJobId", "subscriptionId", "createdBy", "updatedBy", "updatedAt"
      FROM cte
      WHERE rn = 1;

      -- insert if not present
      INSERT INTO "jobStatesMap" ("projectId", "subscriptionId", "resourceId", "jobTypeCode", "contextTypeCode", "contextValue", "contextDetail", "lastSuccessfulJobId", "lastFailedJobId", "lastJobId", "createdBy", "updatedBy", "updatedAt", "createdAt")
      SELECT T1.pid, T1."subscriptionId", T1.rid, 202, T1."contextTypeCode", T1."contextValue", T1."contextDetail", T1."lastSuccessfulJobId", T1."lastFailedJobId", T1.id, T1."createdBy", T1."updatedBy", T1."updatedAt", T1."createdAt" FROM temptjsm T1 left join "jobStatesMap" T2 on T1.pid = T2."projectId" and T1."contextTypeCode" = T2."contextTypeCode" and T1."contextValue" = T2."contextValue" where T2."projectId" IS NULL and T2."contextTypeCode" is null and T2."contextValue" is null;

      -- update if present
      update "jobStatesMap" T1 set "lastSuccessfulJobId" = T2."lastSuccessfulJobId" from temptjsm T2 where T1."projectId" = T2.pid and T1."contextTypeCode" = T2."contextTypeCode" and T1."contextValue" = T2."contextValue" and T1."lastSuccessfulJobId" is null and T2."lastSuccessfulJobId" is not null;
