mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-30 09:16:23 +02:00
🏗️ Update how we store date values
This also fixes a few bugs: - some values had end times that were before the start times - some values has end times a couple of days in the future It’s not entirely clear how users were able to set these values but this update fixes these values and helps avoid similar issues in the future.
This commit is contained in:
parent
8a9159c322
commit
51c5016656
12 changed files with 203 additions and 158 deletions
|
@ -0,0 +1,35 @@
|
|||
-- AlterTable
|
||||
ALTER TABLE "options"
|
||||
ADD COLUMN "duration_minutes" INTEGER NOT NULL DEFAULT 0,
|
||||
ADD COLUMN "start" TIMESTAMP(0);
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "polls" DROP COLUMN "type";
|
||||
|
||||
-- DropEnum
|
||||
DROP TYPE "poll_type";
|
||||
|
||||
-- Reformat option value into new columns
|
||||
UPDATE "options"
|
||||
SET "start" = CASE
|
||||
WHEN POSITION('/' IN "value") = 0 THEN (value || 'T00:00:00')::TIMESTAMP WITHOUT TIME ZONE
|
||||
ELSE SPLIT_PART("value", '/', 1)::TIMESTAMP WITHOUT TIME ZONE
|
||||
END,
|
||||
"duration_minutes" = CASE
|
||||
WHEN POSITION('/' IN "value") = 0 THEN 0
|
||||
ELSE
|
||||
LEAST(EXTRACT(EPOCH FROM (split_part("value", '/', 2)::timestamp - split_part("value", '/', 1)::timestamp)) / 60, 1440)
|
||||
END;
|
||||
|
||||
-- Fix cases where we have a negative duration due to the end time being in the past
|
||||
-- eg. Some polls have value 2023-03-29T23:00:00/2023-03-29T01:00:00
|
||||
UPDATE "options"
|
||||
SET "duration_minutes" = "duration_minutes" + 1440
|
||||
WHERE "duration_minutes" < 0;
|
||||
|
||||
-- Set start date to be not null now that we have all the data and drop the old value column
|
||||
ALTER TABLE "options"
|
||||
DROP COLUMN "value",
|
||||
DROP COLUMN "updated_at",
|
||||
ALTER COLUMN "start" SET NOT NULL;
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue