Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor all Analytics Module's Postgres queries to replace UPDATE queries with INSERT and SELECT #229

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TRIGGER IF EXISTS reorder_metric_preferences_on_update ON user_overview_metric_preference;
DROP FUNCTION IF EXISTS reorder_metric_preferences;
12 changes: 8 additions & 4 deletions DSL/Resql/overview-metric-preferences.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
SELECT metric,
"ordinality",
active
WITH MaxMetrics AS (
SELECT MAX(id) AS maxId
FROM user_overview_metric_preference
WHERE user_id_code = :user_id_code
GROUP BY metric
)
SELECT metric, ordinality, active
FROM user_overview_metric_preference
WHERE user_id_code = :user_id_code
JOIN MaxMetrics ON id = maxId
ORDER BY "ordinality" ASC;
60 changes: 56 additions & 4 deletions DSL/Resql/update-overview-metric-preferences.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
UPDATE user_overview_metric_preference
SET "ordinality" = :ordinality, active = :active
WHERE user_id_code = :user_id_code
AND metric = :metric :: overview_metric;
WITH OldValue AS(
SELECT ordinality
FROM user_overview_metric_preference
WHERE user_id_code = :user_id_code
AND metric = :metric::overview_metric
ORDER BY id DESC
LIMIT 1
),
MetricToBeChanged AS (
SELECT MAX(id) AS maxId
FROM user_overview_metric_preference AS u
CROSS JOIN OldValue
WHERE user_id_code = :user_id_code
AND u.ordinality >= :ordinality
AND u.ordinality < OldValue.ordinality
AND metric <> :metric::overview_metric
GROUP BY metric
)
INSERT INTO user_overview_metric_preference (user_id_code, metric, ordinality, active)
SELECT
user_id_code,
metric,
ordinality + 1,
active
FROM user_overview_metric_preference
JOIN MetricToBeChanged ON id = maxId;

WITH OldValue AS(
SELECT ordinality
FROM user_overview_metric_preference
WHERE user_id_code = :user_id_code
AND metric = :metric::overview_metric
ORDER BY id DESC
LIMIT 1
),
MetricToBeChanged AS (
SELECT MAX(id) AS maxId
FROM user_overview_metric_preference AS u
CROSS JOIN OldValue
WHERE user_id_code = :user_id_code
AND u.ordinality > OldValue.ordinality
AND u.ordinality <= :ordinality
AND metric <> :metric::overview_metric
GROUP BY metric
)
INSERT INTO user_overview_metric_preference (user_id_code, metric, ordinality, active)
SELECT
user_id_code,
metric,
ordinality - 1,
active
FROM user_overview_metric_preference
JOIN MetricToBeChanged ON id = maxId;

INSERT INTO user_overview_metric_preference (user_id_code, metric, ordinality, active)
VALUES (:user_id_code, :metric::overview_metric, :ordinality, :active);
7 changes: 0 additions & 7 deletions DSL/Resql/update-scheduled-report.sql

This file was deleted.