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

23 lines
887 B
MySQL
Raw Normal View History

2024-03-21 19:10:13 +01:00
-- AlterTable
2024-03-21 19:33:35 +01:00
ALTER TABLE `website_event` ADD COLUMN `visit_id` VARCHAR(36) NULL;
2024-03-21 19:10:13 +01:00
2024-03-21 19:33:35 +01:00
UPDATE `website_event` we
JOIN (SELECT DISTINCT
s.session_id,
2024-03-21 19:10:13 +01:00
s.visit_time,
BIN_TO_UUID(RANDOM_BYTES(16) & 0xffffffffffff0fff3fffffffffffffff | 0x00000000000040008000000000000000) uuid
FROM (SELECT DISTINCT session_id,
DATE_FORMAT(created_at, '%Y-%m-%d %H:00:00') visit_time
2024-03-21 19:33:35 +01:00
FROM `website_event`) s) a
2024-03-21 19:10:13 +01:00
ON we.session_id = a.session_id and DATE_FORMAT(we.created_at, '%Y-%m-%d %H:00:00') = a.visit_time
SET we.visit_id = a.uuid
WHERE we.visit_id IS NULL;
2024-03-21 19:33:35 +01:00
ALTER TABLE `website_event` MODIFY `visit_id` VARCHAR(36) NOT NULL;
2024-03-21 19:10:13 +01:00
-- CreateIndex
CREATE INDEX `website_event_visit_id_idx` ON `website_event`(`visit_id`);
-- CreateIndex
CREATE INDEX `website_event_website_id_visit_id_created_at_idx` ON `website_event`(`website_id`, `visit_id`, `created_at`);