1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/src/migrations/20231004120900-create-changes-stats-table-and-trigger.js
Ivar Conradi Østhus 50ddb365b9
fix: make sure we are still pg10 compatible. (#5214)
`EXECUTE FUNCTION` was introduced in Postgres v11. In Postgres v10 the
syntax was `EXECUTE PROCEDURE`. This fix changes the syntax to `EXECUTE
PROCEDURE`, which is perfectly fine sense our function does not return
anything.
2023-10-30 13:19:57 +01:00

33 lines
1.2 KiB
JavaScript

exports.up = function(db, cb) {
db.runSql(`
CREATE TABLE stat_environment_updates(
day DATE NOT NULL,
environment TEXT,
updates BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (day, environment)
);
CREATE OR REPLACE FUNCTION unleash_update_stat_environment_changes_counter() RETURNS trigger AS $unleash_update_changes_counter$
BEGIN
IF NEW.environment IS NOT NULL THEN
INSERT INTO stat_environment_updates(day, environment, updates) SELECT DATE_TRUNC('Day', NEW.created_at), NEW.environment, 1 ON CONFLICT (day, environment) DO UPDATE SET updates = stat_environment_updates.updates + 1;
END IF;
return null;
END;
$unleash_update_changes_counter$ LANGUAGE plpgsql;
CREATE TRIGGER unleash_update_stat_environment_changes
AFTER INSERT ON events
FOR EACH ROW EXECUTE PROCEDURE unleash_update_stat_environment_changes_counter();
`, cb);
};
exports.down = function(db, cb) {
db.runSql(`
DROP TRIGGER IF EXISTS unleash_update_stat_environment_changes ON events;
DROP FUNCTION IF EXISTS unleash_update_stat_environment_changes_counter;
DROP TABLE IF EXISTS stat_environment_updates;
`, cb);
};