mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-23 13:46:45 +02:00
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.
This commit is contained in:
parent
9fe638b0b7
commit
5b879d9331
68
src/migrations/20250813142433-consumption-origin.js
Normal file
68
src/migrations/20250813142433-consumption-origin.js
Normal file
@ -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
|
||||
);
|
||||
};
|
Loading…
Reference in New Issue
Block a user