/*
  Warnings:

  - A unique constraint covering the columns `[externalServiceId]` on the table `MemberPaycheck` will be added. If there are existing duplicate values, this will fail.
  - Made the column `externalServiceType` on table `MemberPaycheck` required. This step will fail if there are existing NULL values in that column.

*/
-- DropIndex
DROP INDEX "MemberPaycheck_externalServiceId_idx";

-- There should be no rows without an externalServiceType.
-- The existing default for externalServiceId is not needed any more, as this is now a required
-- field.

-- AlterTable
ALTER TABLE "MemberPaycheck" ALTER COLUMN "externalServiceType" SET NOT NULL,
ALTER COLUMN "externalServiceId" DROP DEFAULT;

-- Delete any duplicate rows before applying unique constraint onto externalServiceId
DELETE FROM
    "MemberPaycheck" a
    USING "MemberPaycheck" b
WHERE
    a."createdAt" > b."createdAt"
    AND a."externalServiceId" = b."externalServiceId";


-- CreateIndex
CREATE UNIQUE INDEX "MemberPaycheck_externalServiceId_key" ON "MemberPaycheck"("externalServiceId");
