From 8abeba4c0dddf5af92e066024bc212a1e8e8b4bb Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Mon, 10 Jul 2023 23:08:57 -0700 Subject: [PATCH] squash psql migration. add event_data migration --- components/pages/console/TestConsole.js | 1 + .../migrations/02_report_schema/migration.sql | 29 -------------- .../migration.sql | 39 +++++++++++++++++++ queries/analytics/eventData/saveEventData.ts | 5 ++- queries/analytics/session/saveSessionData.ts | 8 +--- yarn.lock | 9 ++++- 6 files changed, 53 insertions(+), 38 deletions(-) delete mode 100644 db/postgresql/migrations/02_report_schema/migration.sql rename db/postgresql/migrations/{03_session_data => 02_report_schema_session_data}/migration.sql (52%) diff --git a/components/pages/console/TestConsole.js b/components/pages/console/TestConsole.js index 3e907856..4f167b9a 100644 --- a/components/pages/console/TestConsole.js +++ b/components/pages/console/TestConsole.js @@ -33,6 +33,7 @@ export function TestConsole() { booleanError: 'true', time: new Date(), number: 1, + number2: Math.random() * 100, time2: new Date().toISOString(), nested: { test: 'test-data', diff --git a/db/postgresql/migrations/02_report_schema/migration.sql b/db/postgresql/migrations/02_report_schema/migration.sql deleted file mode 100644 index f4f3e885..00000000 --- a/db/postgresql/migrations/02_report_schema/migration.sql +++ /dev/null @@ -1,29 +0,0 @@ --- CreateTable -CREATE TABLE "report" ( - "report_id" UUID NOT NULL, - "user_id" UUID NOT NULL, - "website_id" UUID NOT NULL, - "type" VARCHAR(200) NOT NULL, - "name" VARCHAR(200) NOT NULL, - "description" VARCHAR(500) NOT NULL, - "parameters" VARCHAR(6000) NOT NULL, - "created_at" TIMESTAMPTZ(6) DEFAULT CURRENT_TIMESTAMP, - "updated_at" TIMESTAMPTZ(6), - - CONSTRAINT "report_pkey" PRIMARY KEY ("report_id") -); - --- CreateIndex -CREATE UNIQUE INDEX "report_report_id_key" ON "report"("report_id"); - --- CreateIndex -CREATE INDEX "report_user_id_idx" ON "report"("user_id"); - --- CreateIndex -CREATE INDEX "report_website_id_idx" ON "report"("website_id"); - --- CreateIndex -CREATE INDEX "report_type_idx" ON "report"("type"); - --- CreateIndex -CREATE INDEX "report_name_idx" ON "report"("name"); diff --git a/db/postgresql/migrations/03_session_data/migration.sql b/db/postgresql/migrations/02_report_schema_session_data/migration.sql similarity index 52% rename from db/postgresql/migrations/03_session_data/migration.sql rename to db/postgresql/migrations/02_report_schema_session_data/migration.sql index 0a93d39c..41c907cd 100644 --- a/db/postgresql/migrations/03_session_data/migration.sql +++ b/db/postgresql/migrations/02_report_schema_session_data/migration.sql @@ -21,6 +21,21 @@ CREATE TABLE "session_data" ( CONSTRAINT "session_data_pkey" PRIMARY KEY ("session_data_id") ); +-- CreateTable +CREATE TABLE "report" ( + "report_id" UUID NOT NULL, + "user_id" UUID NOT NULL, + "website_id" UUID NOT NULL, + "type" VARCHAR(200) NOT NULL, + "name" VARCHAR(200) NOT NULL, + "description" VARCHAR(500) NOT NULL, + "parameters" VARCHAR(6000) NOT NULL, + "created_at" TIMESTAMPTZ(6) DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMPTZ(6), + + CONSTRAINT "report_pkey" PRIMARY KEY ("report_id") +); + -- CreateIndex CREATE INDEX "session_data_created_at_idx" ON "session_data"("created_at"); @@ -29,3 +44,27 @@ CREATE INDEX "session_data_website_id_idx" ON "session_data"("website_id"); -- CreateIndex CREATE INDEX "session_data_session_id_idx" ON "session_data"("session_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "report_report_id_key" ON "report"("report_id"); + +-- CreateIndex +CREATE INDEX "report_user_id_idx" ON "report"("user_id"); + +-- CreateIndex +CREATE INDEX "report_website_id_idx" ON "report"("website_id"); + +-- CreateIndex +CREATE INDEX "report_type_idx" ON "report"("type"); + +-- CreateIndex +CREATE INDEX "report_name_idx" ON "report"("name"); + +-- EventData migration +UPDATE "event_data" +SET string_value = number_value +WHERE data_type = 2; + +UPDATE "event_data" +SET string_value = CONCAT(REPLACE(TO_CHAR(date_value, 'YYYY-MM-DD HH24:MI:SS:MS'), ' ', 'T'), 'Z') +WHERE data_type = 4; \ No newline at end of file diff --git a/queries/analytics/eventData/saveEventData.ts b/queries/analytics/eventData/saveEventData.ts index c889955f..f7cf06ee 100644 --- a/queries/analytics/eventData/saveEventData.ts +++ b/queries/analytics/eventData/saveEventData.ts @@ -37,8 +37,9 @@ async function relationalQuery(data: { websiteEventId: eventId, websiteId, eventKey: a.key, - stringValue: a.value.toString(), - numericValue: a.dynamicDataType === DATA_TYPE.number ? a.value : null, + stringValue: + a.dynamicDataType === DATA_TYPE.number ? parseFloat(a.value).toFixed(4) : a.value.toString(), + numberValue: a.dynamicDataType === DATA_TYPE.number ? a.value : null, dateValue: a.dynamicDataType === DATA_TYPE.date ? new Date(a.value) : null, dataType: a.dynamicDataType, })); diff --git a/queries/analytics/session/saveSessionData.ts b/queries/analytics/session/saveSessionData.ts index 79104270..312f1f53 100644 --- a/queries/analytics/session/saveSessionData.ts +++ b/queries/analytics/session/saveSessionData.ts @@ -20,12 +20,8 @@ export async function saveSessionData(data: { sessionId, key: a.key, stringValue: - a.dynamicDataType === DATA_TYPE.string || - a.dynamicDataType === DATA_TYPE.boolean || - a.dynamicDataType === DATA_TYPE.array - ? a.value - : null, - numericValue: a.dynamicDataType === DATA_TYPE.number ? a.value : null, + a.dynamicDataType === DATA_TYPE.number ? parseFloat(a.value).toFixed(4) : a.value.toString(), + numberValue: a.dynamicDataType === DATA_TYPE.number ? a.value : null, dateValue: a.dynamicDataType === DATA_TYPE.date ? new Date(a.value) : null, dataType: a.dynamicDataType, })); diff --git a/yarn.lock b/yarn.lock index 7dafda26..9d06a5c3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8048,13 +8048,20 @@ semver@^6.0.0, semver@^6.1.1, semver@^6.1.2, semver@^6.3.0: resolved "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz" integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw== -semver@^7.3.4, semver@^7.3.5, semver@^7.3.6, semver@^7.3.7, semver@^7.3.8: +semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.3.8: version "7.5.2" resolved "https://registry.npmjs.org/semver/-/semver-7.5.2.tgz" integrity sha512-SoftuTROv/cRjCze/scjGyiDtcUyxw1rgYQSZY7XTmtR5hX+dm76iDbTH8TkLPHCQmlbQVSSbNZCPM2hb0knnQ== dependencies: lru-cache "^6.0.0" +semver@^7.5.2: + version "7.5.4" + resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e" + integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA== + dependencies: + lru-cache "^6.0.0" + serialize-javascript@^4.0.0: version "4.0.0" resolved "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-4.0.0.tgz"