diff --git a/DSL/Liquibase/changelog/1715774693-remove-reorder-preferences-trigger.sql b/DSL/Liquibase/changelog/1715774693-remove-reorder-preferences-trigger.sql new file mode 100644 index 00000000..2f45c4ee --- /dev/null +++ b/DSL/Liquibase/changelog/1715774693-remove-reorder-preferences-trigger.sql @@ -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; diff --git a/DSL/Resql/overview-metric-preferences.sql b/DSL/Resql/overview-metric-preferences.sql index 544e0076..b858d670 100644 --- a/DSL/Resql/overview-metric-preferences.sql +++ b/DSL/Resql/overview-metric-preferences.sql @@ -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; diff --git a/DSL/Resql/update-overview-metric-preferences.sql b/DSL/Resql/update-overview-metric-preferences.sql index 6a90f9dd..9a22a7b2 100644 --- a/DSL/Resql/update-overview-metric-preferences.sql +++ b/DSL/Resql/update-overview-metric-preferences.sql @@ -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); diff --git a/DSL/Resql/update-scheduled-report.sql b/DSL/Resql/update-scheduled-report.sql deleted file mode 100644 index 5ba0c0c5..00000000 --- a/DSL/Resql/update-scheduled-report.sql +++ /dev/null @@ -1,7 +0,0 @@ -UPDATE scheduled_reports -SET ( - "name" = :name, - "period" = :period, - cron_expression = :cron_expression - ) -WHERE id = :id;