Skip to content

Commit

Permalink
Refactor all Analytics Module's Postgres queries to replace UPDATE
Browse files Browse the repository at this point in the history
…queries with `INSERT` and `SELECT` (#229)
  • Loading branch information
baha-a authored May 18, 2024
1 parent 479ffc6 commit 63bd2c2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
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.

0 comments on commit 63bd2c2

Please sign in to comment.