umami/db/postgresql/migrations/05_add_visit_id/migration.sql

22 lines
738 B
MySQL
Raw Normal View History

2024-03-21 17:30:42 +01:00
-- AlterTable
2024-03-21 17:36:27 +01:00
ALTER TABLE "website_event" ADD COLUMN "visit_id" UUID NULL;
2024-03-21 17:30:42 +01:00
UPDATE "website_event" we
SET visit_id = a.uuid
FROM (SELECT DISTINCT
s.session_id,
s.visit_time,
gen_random_uuid() uuid
FROM (SELECT DISTINCT session_id,
date_trunc('hour', created_at) visit_time
FROM "website_event") s) a
WHERE we.session_id = a.session_id
and date_trunc('hour', we.created_at) = a.visit_time;
2024-03-21 17:30:42 +01:00
2024-03-21 17:36:27 +01:00
ALTER TABLE "website_event" ALTER COLUMN "visit_id" SET NOT NULL;
2024-03-21 17:30:42 +01:00
-- CreateIndex
CREATE INDEX "website_event_visit_id_idx" ON "website_event"("visit_id");
-- CreateIndex
2024-03-21 17:36:27 +01:00
CREATE INDEX "website_event_website_id_visit_id_created_at_idx" ON "website_event"("website_id", "visit_id", "created_at");