From 5b879d9331715ea9deff0a570355927d9641079f Mon Sep 17 00:00:00 2001 From: Jaanus Sellin Date: Wed, 13 Aug 2025 16:16:05 +0300 Subject: [PATCH] feat: add origin column for consumption based tables (#10494) We will start separating traffic, when it comes from different origins. Also will add it as primary key so merging will work correctly. There is a problem with using normal down migration, is that if we split data by origin, when down migrating and removing column, and re adding the old migration, we will have conflicts and it will fail. This means before we down migrate, we need to aggregate/sum the data and only then we can reapply old schema. --- .../20250813142433-consumption-origin.js | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/migrations/20250813142433-consumption-origin.js diff --git a/src/migrations/20250813142433-consumption-origin.js b/src/migrations/20250813142433-consumption-origin.js new file mode 100644 index 0000000000..9b86d6a127 --- /dev/null +++ b/src/migrations/20250813142433-consumption-origin.js @@ -0,0 +1,68 @@ +exports.up = function (db, cb) { + db.runSql( + ` + ALTER TABLE request_count_consumption + ADD COLUMN origin TEXT NOT NULL DEFAULT 'sdk'; + + ALTER TABLE request_count_consumption + DROP CONSTRAINT request_count_consumption_pkey, + ADD PRIMARY KEY(day, metered_group, origin); + + ALTER TABLE connection_count_consumption + ADD COLUMN origin TEXT NOT NULL DEFAULT 'sdk'; + + ALTER TABLE connection_count_consumption + DROP CONSTRAINT connection_count_consumption_pkey, + ADD PRIMARY KEY(day, metered_group, origin); + `, + cb + ); +}; + +exports.down = function (db, cb) { + db.runSql( + ` + CREATE TEMP TABLE tmp_request_count AS + SELECT + day, + metered_group, + SUM(requests) AS requests + FROM request_count_consumption + GROUP BY day, metered_group; + + DELETE FROM request_count_consumption; + + INSERT INTO request_count_consumption (day, metered_group, requests) + SELECT day, metered_group, requests FROM tmp_request_count; + + DROP TABLE tmp_request_count; + + ALTER TABLE request_count_consumption + DROP CONSTRAINT request_count_consumption_pkey, + DROP COLUMN origin, + ADD PRIMARY KEY(day, metered_group); + + CREATE TEMP TABLE tmp_connection_count AS + SELECT + day, + metered_group, + SUM(requests) AS requests, + SUM(connections) AS connections + FROM connection_count_consumption + GROUP BY day, metered_group; + + DELETE FROM connection_count_consumption; + + INSERT INTO connection_count_consumption (day, metered_group, requests, connections) + SELECT day, metered_group, requests, connections FROM tmp_connection_count; + + DROP TABLE tmp_connection_count; + + ALTER TABLE connection_count_consumption + DROP CONSTRAINT connection_count_consumption_pkey, + DROP COLUMN origin, + ADD PRIMARY KEY(day, metered_group); + `, + cb + ); +};