From 964651bad349079ea3abc6f09100b0a01a7b36bf Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Wed, 10 Sep 2025 09:21:27 -0700 Subject: [PATCH 1/3] add data migration to convert funnel and goals reports to new structure --- .../14_add_link_and_pixel/migration.sql | 26 +++++++++++++++++++ src/components/icons.ts | 1 + 2 files changed, 27 insertions(+) diff --git a/prisma/migrations/14_add_link_and_pixel/migration.sql b/prisma/migrations/14_add_link_and_pixel/migration.sql index 45320b03..4e51bdb1 100644 --- a/prisma/migrations/14_add_link_and_pixel/migration.sql +++ b/prisma/migrations/14_add_link_and_pixel/migration.sql @@ -71,3 +71,29 @@ CREATE INDEX "pixel_team_id_idx" ON "pixel"("team_id"); -- CreateIndex CREATE INDEX "pixel_created_at_idx" ON "pixel"("created_at"); + +-- DataMigration +UPDATE "report" +SET parameters = parameters - 'websiteId' - 'dateRange' +WHERE type = 'funnel'; + +INSERT INTO "report" (report_id, user_id, website_id, type, name, description, parameters, created_at, updated_at) +SELECT gen_random_uuid(), + user_id, + website_id, + 'goal', + concat(name, ' - ', elem ->> 'value'), + description, + jsonb_build_object( + 'type', CASE WHEN elem ->> 'type' = 'url' THEN 'path' + ELSE elem ->> 'type' END, + 'value', elem ->> 'value' + ) AS parameters, + created_at, + updated_at +FROM "report" +CROSS JOIN LATERAL jsonb_array_elements(parameters -> 'goals') elem +WHERE elem ->> 'type' IN ('event', 'url') + and type = 'goal'; + +DELETE FROM "report" WHERE parameters ? 'goals' and type = 'goal'; \ No newline at end of file diff --git a/src/components/icons.ts b/src/components/icons.ts index c96a27b6..287dc650 100644 --- a/src/components/icons.ts +++ b/src/components/icons.ts @@ -59,6 +59,7 @@ export { Funnel, Lightbulb, Lightning, + Location, Magnet, Money, Network, From 1825524e95af5107538a4c4a57917b5948db5885 Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Wed, 10 Sep 2025 09:29:49 -0700 Subject: [PATCH 2/3] convert report type before data migratino --- prisma/migrations/14_add_link_and_pixel/migration.sql | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/prisma/migrations/14_add_link_and_pixel/migration.sql b/prisma/migrations/14_add_link_and_pixel/migration.sql index 4e51bdb1..14feaa66 100644 --- a/prisma/migrations/14_add_link_and_pixel/migration.sql +++ b/prisma/migrations/14_add_link_and_pixel/migration.sql @@ -73,9 +73,8 @@ CREATE INDEX "pixel_team_id_idx" ON "pixel"("team_id"); CREATE INDEX "pixel_created_at_idx" ON "pixel"("created_at"); -- DataMigration -UPDATE "report" -SET parameters = parameters - 'websiteId' - 'dateRange' -WHERE type = 'funnel'; +UPDATE "report" SET parameters = parameters - 'websiteId' - 'dateRange' WHERE type = 'funnel'; +UPDATE "report" SET type = 'goal' WHERE type = 'goals'; INSERT INTO "report" (report_id, user_id, website_id, type, name, description, parameters, created_at, updated_at) SELECT gen_random_uuid(), From f3125c2add28ab04e460d835e64e3c52c78d2ced Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Wed, 10 Sep 2025 16:38:51 -0700 Subject: [PATCH 3/3] clean up funnel migration and report errors --- .../14_add_link_and_pixel/migration.sql | 31 ++++++++++++++++--- src/queries/sql/reports/getFunnel.ts | 3 +- src/queries/sql/reports/getGoal.ts | 6 ++-- src/queries/sql/reports/getRevenue.ts | 2 +- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/prisma/migrations/14_add_link_and_pixel/migration.sql b/prisma/migrations/14_add_link_and_pixel/migration.sql index 14feaa66..29d9e300 100644 --- a/prisma/migrations/14_add_link_and_pixel/migration.sql +++ b/prisma/migrations/14_add_link_and_pixel/migration.sql @@ -72,8 +72,29 @@ CREATE INDEX "pixel_team_id_idx" ON "pixel"("team_id"); -- CreateIndex CREATE INDEX "pixel_created_at_idx" ON "pixel"("created_at"); --- DataMigration -UPDATE "report" SET parameters = parameters - 'websiteId' - 'dateRange' WHERE type = 'funnel'; +-- DataMigration Funnel +DELETE FROM "report" WHERE type = 'funnel' and jsonb_array_length(parameters->'steps') = 1; +UPDATE "report" SET parameters = parameters - 'websiteId' - 'dateRange' - 'urls' WHERE type = 'funnel'; + +UPDATE "report" +SET parameters = jsonb_set( + parameters, + '{steps}', + ( + SELECT jsonb_agg( + CASE + WHEN step->>'type' = 'url' + THEN jsonb_set(step, '{type}', '"path"') + ELSE step + END + ) + FROM jsonb_array_elements(parameters->'steps') step + ) +) +WHERE type = 'funnel' + and parameters @> '{"steps":[{"type":"url"}]}'; + +-- DataMigration Goals UPDATE "report" SET type = 'goal' WHERE type = 'goals'; INSERT INTO "report" (report_id, user_id, website_id, type, name, description, parameters, created_at, updated_at) @@ -92,7 +113,7 @@ SELECT gen_random_uuid(), updated_at FROM "report" CROSS JOIN LATERAL jsonb_array_elements(parameters -> 'goals') elem -WHERE elem ->> 'type' IN ('event', 'url') - and type = 'goal'; +WHERE type = 'goal' + and elem ->> 'type' IN ('event', 'url'); -DELETE FROM "report" WHERE parameters ? 'goals' and type = 'goal'; \ No newline at end of file +DELETE FROM "report" WHERE type = 'goal' and parameters ? 'goals'; \ No newline at end of file diff --git a/src/queries/sql/reports/getFunnel.ts b/src/queries/sql/reports/getFunnel.ts index abc0dab3..a56439db 100644 --- a/src/queries/sql/reports/getFunnel.ts +++ b/src/queries/sql/reports/getFunnel.ts @@ -32,14 +32,13 @@ async function relationalQuery( ): Promise { const { startDate, endDate, window, steps } = parameters; const { rawQuery, getAddIntervalQuery, parseFilters } = prisma; - const { levelOneQuery, levelQuery, sumQuery, params } = getFunnelQuery(steps, window); - const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId, startDate, endDate, }); + const { levelOneQuery, levelQuery, sumQuery, params } = getFunnelQuery(steps, window); function getFunnelQuery( steps: { type: string; value: string }[], diff --git a/src/queries/sql/reports/getGoal.ts b/src/queries/sql/reports/getGoal.ts index b00e362e..81ec80ab 100644 --- a/src/queries/sql/reports/getGoal.ts +++ b/src/queries/sql/reports/getGoal.ts @@ -48,15 +48,15 @@ async function relationalQuery( from website_event ${cohortQuery} ${joinSessionQuery} - where website_id = {websiteId:UUID} + where website_id = {{websiteId::uuid}} ${dateQuery} ${filterQuery} ) as total from website_event ${cohortQuery} ${joinSessionQuery} - where website_id = {websiteId:UUID} - and ${column} = {value:String} + where website_id = {{websiteId::uuid}} + and ${column} = {{value}} ${dateQuery} ${filterQuery} `, diff --git a/src/queries/sql/reports/getRevenue.ts b/src/queries/sql/reports/getRevenue.ts index f5c652aa..d73d90c2 100644 --- a/src/queries/sql/reports/getRevenue.ts +++ b/src/queries/sql/reports/getRevenue.ts @@ -75,7 +75,7 @@ async function relationalQuery( ` select session.country as name, - sum(r.revenue) value + sum(revenue) value from revenue join website_event on website_event.website_id = revenue.website_id