repair-ops-coach-identity.sql 1.1 KB

123456789101112131415161718192021222324252627
  1. -- Pass the affected company ID as $1. Only restore accounts created as coaches
  2. -- by the operations gateway that do not have formal student records.
  3. UPDATE "_User" u
  4. SET "identityType" = 'coach', "updatedAt" = now()
  5. WHERE u.company = $1
  6. AND u."legacyGroupId" = 3
  7. AND u."legacyUserId" > 0
  8. AND u."legacyUserData"->>'UserID' = u."legacyUserId"::text
  9. AND u."legacyUserData"->>'GroupID' = '3'
  10. AND COALESCE(u."legacyUserData"->>'OpsCreatedAt', '') <> ''
  11. AND COALESCE(u."identityType", '') IN ('', 'unknown', 'member')
  12. AND NOT COALESCE(u."isAdmin", false)
  13. AND NOT COALESCE(u."isDeleted", false)
  14. AND NOT EXISTS (
  15. SELECT 1 FROM "CourseAppointment" a
  16. WHERE a.company = u.company AND a.szyh::text = u."legacyUserId"::text
  17. UNION ALL
  18. SELECT 1 FROM "DailyStudyRecord" d
  19. WHERE d.company = u.company AND d."userId"::text = u."legacyUserId"::text
  20. UNION ALL
  21. SELECT 1 FROM "CourseBinding" b
  22. WHERE b.company = u.company AND b.yhid::text = u."legacyUserId"::text
  23. UNION ALL
  24. SELECT 1 FROM "LessonRecord" l
  25. WHERE l.company = u.company AND l.xymz::text = u."legacyUserId"::text
  26. )
  27. RETURNING u."objectId", u."legacyUserId";