-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor all Analytics Module's Postgres queries to replace
UPDATE
…
…queries with `INSERT` and `SELECT` (#229)
- Loading branch information
Showing
4 changed files
with
66 additions
and
15 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
DSL/Liquibase/changelog/1715774693-remove-reorder-preferences-trigger.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file was deleted.
Oops, something went wrong.