       -- 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","statusCode","subscriptionId", "createdBy", "updatedBy", "updatedAt","lastSuccessfulJobId","lastFailedJobId", ROW_NUMBER() OVER (PARTITION BY "pid","contextTypeCode","contextValue" ORDER BY "createdAt" desc)
+         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","lastSuccessfulJobId","lastFailedJobId","subscriptionId", "createdBy", "updatedBy", "updatedAt"
+      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", "lastSuccessfulJobId", "lastFailedJobId", "lastJobId", "createdBy", "updatedBy", "updatedAt", "createdAt")
-      SELECT T1.pid, T1."subscriptionId", T1.rid, 202, T1."contextTypeCode", T1."contextValue", 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;
+      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;

      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 "updatedBy" = tjsm."createdBy" where "updatedBy" is null;

      -- update lastSuccessfulJobId, lastUnstableJobId, lastTimedoutJobId, lastFailedJobId

      update rtjsm set "contextTypeCode" = 312 where "contextTypeCode" = 2014;
      update rtjsm set "contextTypeCode" = 313 where "contextTypeCode" = 2013;

      update tjsm T1 set "contextValue" = T2."contextValue", "contextTypeCode" = T2."contextTypeCode", "createdBy" = T2."createdBy", "updatedBy" = T2."updatedBy", "contextDetail" = T2."contextDetail" from rtjsm T2 where T1.rid = T2.id;

      -- ignoring records which are not part of (2010,2000,2005,2007,2012,2011,2009) typeCodes
      delete from tjsm where "contextValue" is null;
