-- Make deprecated columns nullable.
ALTER TABLE "User" ALTER COLUMN "isEmailVerified" DROP NOT NULL,
ALTER COLUMN "hashedEmailVerificationToken" DROP NOT NULL;

-- Create verified auth factors for all users with "isEmailVerified" == true.
INSERT INTO "AuthFactor" ("userId", "email", "isVerified", "updatedAt")
-- The result of this select will be the VALUES that are inserted.
SELECT
  "User"."id" as "userId",
  "User"."email" as "email",
  TRUE as "isVerified", -- Constant value of True.
  CURRENT_DATE as "updatedAt" -- This is normally set by Prisma.
FROM "User"
WHERE
  "User"."isEmailVerified" = TRUE
  AND
  -- Filter out users who already have a verified auth factor for their email address!
  NOT EXISTS (
    SELECT
      "id"
    FROM "AuthFactor" "userAuthFactor"
    WHERE
      "userAuthFactor"."userId" = "User"."id"
      AND
      "userAuthFactor"."email" = "User"."email"
      AND
      "userAuthFactor"."isVerified" = TRUE
    LIMIT 1
  )
;
