/*
  Warnings:

  - Added the required column `occurredAt` to the `MemberPaycheck` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "MemberPaycheck" ADD COLUMN     "occurredAt" DATE;

/*

  Update any existing paychecks to have an "occurredAt" that matches its current "endAt".

  We have to be kind of careful with this for a hot minute.
  Though this will be "occurredAt" correct for "PaloloPayroll" paychecks, it will NOT be correct
  for Finch and Argyle on all previously created paychecks.

  We do not have a good way of remediating the fact that we do not have an accurate "occurredAt" for
  Finch and Argyle paychecks from any time prior to this migration, but "occurredAt" needs to be a
  non-nullable column going forward.  Setting "occurredAt" to "endAt" for all rows is probably mostly
  OK here, as we should not reference occuredAt on any paycheck that is not a "PaloloPayroll" paycheck.

*/
UPDATE "MemberPaycheck" m
SET "occurredAt" = "endAt";

ALTER TABLE "MemberPaycheck"
ALTER COLUMN "occurredAt" SET NOT NULL,
ALTER COLUMN "startAt" DROP NOT NULL,
ALTER COLUMN "endAt" DROP NOT NULL;

-- Remove all startAt and endAt dates for Palolo Paychecks
UPDATE "MemberPaycheck" m
set "startAt" = null, "endAt" = null
where m."externalServiceType" = 'PaloloPayroll';
