From 226d16a8ec52affef14c542df037ac1acf463c09 Mon Sep 17 00:00:00 2001 From: baha-a Date: Wed, 15 May 2024 12:06:58 +0300 Subject: [PATCH 1/5] Replace UPDATE with INSERT --- DSL/Resql/overview-metric-preferences.sql | 12 ++++++++---- DSL/Resql/update-overview-metric-preferences.sql | 6 ++---- DSL/Resql/update-scheduled-report.sql | 7 ------- 3 files changed, 10 insertions(+), 15 deletions(-) delete mode 100644 DSL/Resql/update-scheduled-report.sql 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..0d2dbb9c 100644 --- a/DSL/Resql/update-overview-metric-preferences.sql +++ b/DSL/Resql/update-overview-metric-preferences.sql @@ -1,4 +1,2 @@ -UPDATE user_overview_metric_preference -SET "ordinality" = :ordinality, active = :active -WHERE user_id_code = :user_id_code - AND metric = :metric :: overview_metric; +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; From 8dc853b2e6963f245da5fe11ce876bf390d24303 Mon Sep 17 00:00:00 2001 From: baha-a Date: Wed, 15 May 2024 15:04:01 +0300 Subject: [PATCH 2/5] Fis trigger related to UPDATE --- ...934-create-reorder-preferences-trigger.sql | 60 +++++++++++++++---- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/DSL/Liquibase/changelog/1674138934-create-reorder-preferences-trigger.sql b/DSL/Liquibase/changelog/1674138934-create-reorder-preferences-trigger.sql index 0023ebde..ab603ce3 100644 --- a/DSL/Liquibase/changelog/1674138934-create-reorder-preferences-trigger.sql +++ b/DSL/Liquibase/changelog/1674138934-create-reorder-preferences-trigger.sql @@ -7,19 +7,53 @@ DECLARE BEGIN IF new.ORDINALITY < old.ORDINALITY THEN - UPDATE user_overview_metric_preference - SET ORDINALITY = ORDINALITY + 1 - WHERE ORDINALITY >= new.ORDINALITY - AND ORDINALITY < old.ORDINALITY - AND metric <> old.metric - AND user_id_code = old.user_id_code; + INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) + SELECT + metric, + user_id_code, + active, + CASE + WHEN ordinality >= new.ordinality THEN ordinality + 1 + ELSE ordinality + END + FROM user_overview_metric_preference + WHERE ordinality >= new.ordinality + AND ordinality < old.ordinality + AND metric <> old.metric + AND user_id_code = old.user_id_code; + + -- INSERT INTO user_overview_metric_preference (user_id_code, metric, ordinality, active) + -- SELECT + -- old.user_id_code, + -- old.metric, + -- ordinality + 1, + -- old.active + -- FROM user_overview_metric_preference + -- WHERE id IN ( + -- SELECT MAX(id) AS maxId + -- FROM user_overview_metric_preference + -- WHERE user_id_code = old.user_id_code + -- AND ordinality >= new.ordinality + -- AND ordinality < old.ordinality + -- AND metric <> old.metric + -- GROUP BY metric + -- ) + ELSE - UPDATE user_overview_metric_preference - SET ORDINALITY = ORDINALITY - 1 - WHERE ORDINALITY > old.ORDINALITY - AND ORDINALITY <= new.ORDINALITY - AND metric <> old.metric - AND user_id_code = old.user_id_code; + INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) + SELECT + metric, + user_id_code, + active, + CASE + WHEN ordinality >= new.ordinality THEN ordinality - 1 + ELSE ordinality + END + FROM user_overview_metric_preference + WHERE ordinality >= new.ordinality + AND ordinality < old.ordinality + AND metric <> old.metric + AND user_id_code = old.user_id_code; END IF; RETURN new; @@ -28,5 +62,5 @@ END; $$ LANGUAGE plpgsql; CREATE TRIGGER reorder_metric_preferences_on_update -AFTER UPDATE ON user_overview_metric_preference +AFTER INSERT ON user_overview_metric_preference FOR EACH ROW EXECUTE PROCEDURE reorder_metric_preferences(); From e701f0f76ad091b491e69e1ceee5612726b91fd8 Mon Sep 17 00:00:00 2001 From: baha-a Date: Wed, 15 May 2024 15:05:15 +0300 Subject: [PATCH 3/5] Fis trigger related to UPDATE --- ...934-create-reorder-preferences-trigger.sql | 60 ++++------------- ...693-modify-reorder-preferences-trigger.sql | 66 +++++++++++++++++++ 2 files changed, 79 insertions(+), 47 deletions(-) create mode 100644 DSL/Liquibase/changelog/1715774693-modify-reorder-preferences-trigger.sql diff --git a/DSL/Liquibase/changelog/1674138934-create-reorder-preferences-trigger.sql b/DSL/Liquibase/changelog/1674138934-create-reorder-preferences-trigger.sql index ab603ce3..0023ebde 100644 --- a/DSL/Liquibase/changelog/1674138934-create-reorder-preferences-trigger.sql +++ b/DSL/Liquibase/changelog/1674138934-create-reorder-preferences-trigger.sql @@ -7,53 +7,19 @@ DECLARE BEGIN IF new.ORDINALITY < old.ORDINALITY THEN - INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) - SELECT - metric, - user_id_code, - active, - CASE - WHEN ordinality >= new.ordinality THEN ordinality + 1 - ELSE ordinality - END - FROM user_overview_metric_preference - WHERE ordinality >= new.ordinality - AND ordinality < old.ordinality - AND metric <> old.metric - AND user_id_code = old.user_id_code; - - -- INSERT INTO user_overview_metric_preference (user_id_code, metric, ordinality, active) - -- SELECT - -- old.user_id_code, - -- old.metric, - -- ordinality + 1, - -- old.active - -- FROM user_overview_metric_preference - -- WHERE id IN ( - -- SELECT MAX(id) AS maxId - -- FROM user_overview_metric_preference - -- WHERE user_id_code = old.user_id_code - -- AND ordinality >= new.ordinality - -- AND ordinality < old.ordinality - -- AND metric <> old.metric - -- GROUP BY metric - -- ) - + UPDATE user_overview_metric_preference + SET ORDINALITY = ORDINALITY + 1 + WHERE ORDINALITY >= new.ORDINALITY + AND ORDINALITY < old.ORDINALITY + AND metric <> old.metric + AND user_id_code = old.user_id_code; ELSE - INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) - SELECT - metric, - user_id_code, - active, - CASE - WHEN ordinality >= new.ordinality THEN ordinality - 1 - ELSE ordinality - END - FROM user_overview_metric_preference - WHERE ordinality >= new.ordinality - AND ordinality < old.ordinality - AND metric <> old.metric - AND user_id_code = old.user_id_code; + UPDATE user_overview_metric_preference + SET ORDINALITY = ORDINALITY - 1 + WHERE ORDINALITY > old.ORDINALITY + AND ORDINALITY <= new.ORDINALITY + AND metric <> old.metric + AND user_id_code = old.user_id_code; END IF; RETURN new; @@ -62,5 +28,5 @@ END; $$ LANGUAGE plpgsql; CREATE TRIGGER reorder_metric_preferences_on_update -AFTER INSERT ON user_overview_metric_preference +AFTER UPDATE ON user_overview_metric_preference FOR EACH ROW EXECUTE PROCEDURE reorder_metric_preferences(); diff --git a/DSL/Liquibase/changelog/1715774693-modify-reorder-preferences-trigger.sql b/DSL/Liquibase/changelog/1715774693-modify-reorder-preferences-trigger.sql new file mode 100644 index 00000000..061b3285 --- /dev/null +++ b/DSL/Liquibase/changelog/1715774693-modify-reorder-preferences-trigger.sql @@ -0,0 +1,66 @@ +-- liquibase formatted sql +-- changeset baha-a:1715774693 +CREATE OR REPLACE FUNCTION reorder_metric_preferences() RETURNS TRIGGER AS +$$ +DECLARE + ORDINALITY VARCHAR2(150) := 'ordinality'; +BEGIN + +IF new.ORDINALITY < old.ORDINALITY THEN + INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) + SELECT + metric, + user_id_code, + active, + CASE + WHEN ordinality >= new.ordinality THEN ordinality + 1 + ELSE ordinality + END + FROM user_overview_metric_preference + WHERE ordinality >= new.ordinality + AND ordinality < old.ordinality + AND metric <> old.metric + AND user_id_code = old.user_id_code; + + -- INSERT INTO user_overview_metric_preference (user_id_code, metric, ordinality, active) + -- SELECT + -- old.user_id_code, + -- old.metric, + -- ordinality + 1, + -- old.active + -- FROM user_overview_metric_preference + -- WHERE id IN ( + -- SELECT MAX(id) AS maxId + -- FROM user_overview_metric_preference + -- WHERE user_id_code = old.user_id_code + -- AND ordinality >= new.ordinality + -- AND ordinality < old.ordinality + -- AND metric <> old.metric + -- GROUP BY metric + -- ) + +ELSE + INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) + SELECT + metric, + user_id_code, + active, + CASE + WHEN ordinality >= new.ordinality THEN ordinality - 1 + ELSE ordinality + END + FROM user_overview_metric_preference + WHERE ordinality >= new.ordinality + AND ordinality < old.ordinality + AND metric <> old.metric + AND user_id_code = old.user_id_code; +END IF; + +RETURN new; + +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER reorder_metric_preferences_on_update +AFTER INSERT ON user_overview_metric_preference +FOR EACH ROW EXECUTE PROCEDURE reorder_metric_preferences(); From 18881b6790d65717dbe5725796b60746597ff390 Mon Sep 17 00:00:00 2001 From: baha-a Date: Wed, 15 May 2024 16:33:33 +0300 Subject: [PATCH 4/5] Remove trigger and fix insert to user_overview_metric_preference --- ...693-modify-reorder-preferences-trigger.sql | 66 ----------------- ...693-remove-reorder-preferences-trigger.sql | 2 + .../update-overview-metric-preferences.sql | 70 +++++++++++++++++++ 3 files changed, 72 insertions(+), 66 deletions(-) delete mode 100644 DSL/Liquibase/changelog/1715774693-modify-reorder-preferences-trigger.sql create mode 100644 DSL/Liquibase/changelog/1715774693-remove-reorder-preferences-trigger.sql diff --git a/DSL/Liquibase/changelog/1715774693-modify-reorder-preferences-trigger.sql b/DSL/Liquibase/changelog/1715774693-modify-reorder-preferences-trigger.sql deleted file mode 100644 index 061b3285..00000000 --- a/DSL/Liquibase/changelog/1715774693-modify-reorder-preferences-trigger.sql +++ /dev/null @@ -1,66 +0,0 @@ --- liquibase formatted sql --- changeset baha-a:1715774693 -CREATE OR REPLACE FUNCTION reorder_metric_preferences() RETURNS TRIGGER AS -$$ -DECLARE - ORDINALITY VARCHAR2(150) := 'ordinality'; -BEGIN - -IF new.ORDINALITY < old.ORDINALITY THEN - INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) - SELECT - metric, - user_id_code, - active, - CASE - WHEN ordinality >= new.ordinality THEN ordinality + 1 - ELSE ordinality - END - FROM user_overview_metric_preference - WHERE ordinality >= new.ordinality - AND ordinality < old.ordinality - AND metric <> old.metric - AND user_id_code = old.user_id_code; - - -- INSERT INTO user_overview_metric_preference (user_id_code, metric, ordinality, active) - -- SELECT - -- old.user_id_code, - -- old.metric, - -- ordinality + 1, - -- old.active - -- FROM user_overview_metric_preference - -- WHERE id IN ( - -- SELECT MAX(id) AS maxId - -- FROM user_overview_metric_preference - -- WHERE user_id_code = old.user_id_code - -- AND ordinality >= new.ordinality - -- AND ordinality < old.ordinality - -- AND metric <> old.metric - -- GROUP BY metric - -- ) - -ELSE - INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) - SELECT - metric, - user_id_code, - active, - CASE - WHEN ordinality >= new.ordinality THEN ordinality - 1 - ELSE ordinality - END - FROM user_overview_metric_preference - WHERE ordinality >= new.ordinality - AND ordinality < old.ordinality - AND metric <> old.metric - AND user_id_code = old.user_id_code; -END IF; - -RETURN new; - -END; -$$ LANGUAGE plpgsql; - -CREATE TRIGGER reorder_metric_preferences_on_update -AFTER INSERT ON user_overview_metric_preference -FOR EACH ROW EXECUTE PROCEDURE reorder_metric_preferences(); 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/update-overview-metric-preferences.sql b/DSL/Resql/update-overview-metric-preferences.sql index 0d2dbb9c..2c72f100 100644 --- a/DSL/Resql/update-overview-metric-preferences.sql +++ b/DSL/Resql/update-overview-metric-preferences.sql @@ -1,2 +1,72 @@ +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 + CROSS JOIN OldValue + WHERE user_id_code = :user_id_code + AND ordinality >= :ordinality + AND 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 + CROSS JOIN OldValue + WHERE user_id_code = :user_id_code + AND ordinality > OldValue.ordinality + AND 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); + + +-- INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) +-- SELECT +-- metric, +-- user_id_code, +-- active, +-- CASE +-- WHEN ordinality >= :ordinality THEN ordinality + 1 +-- ELSE ordinality +-- END +-- FROM user_overview_metric_preference +-- WHERE ordinality >= :ordinality +-- AND ordinality < old.ordinality +-- AND metric <> :metric::overview_metric +-- AND user_id_code = :user_id_code; From b3d7c1ebfa573c17bdeafcc2c6d25356ab005ed0 Mon Sep 17 00:00:00 2001 From: baha-a Date: Wed, 15 May 2024 16:42:16 +0300 Subject: [PATCH 5/5] Add alias and remove comment --- .../update-overview-metric-preferences.sql | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/DSL/Resql/update-overview-metric-preferences.sql b/DSL/Resql/update-overview-metric-preferences.sql index 2c72f100..9a22a7b2 100644 --- a/DSL/Resql/update-overview-metric-preferences.sql +++ b/DSL/Resql/update-overview-metric-preferences.sql @@ -8,11 +8,11 @@ WITH OldValue AS( ), MetricToBeChanged AS ( SELECT MAX(id) AS maxId - FROM user_overview_metric_preference + FROM user_overview_metric_preference AS u CROSS JOIN OldValue WHERE user_id_code = :user_id_code - AND ordinality >= :ordinality - AND ordinality < OldValue.ordinality + AND u.ordinality >= :ordinality + AND u.ordinality < OldValue.ordinality AND metric <> :metric::overview_metric GROUP BY metric ) @@ -35,11 +35,11 @@ WITH OldValue AS( ), MetricToBeChanged AS ( SELECT MAX(id) AS maxId - FROM user_overview_metric_preference + FROM user_overview_metric_preference AS u CROSS JOIN OldValue WHERE user_id_code = :user_id_code - AND ordinality > OldValue.ordinality - AND ordinality <= :ordinality + AND u.ordinality > OldValue.ordinality + AND u.ordinality <= :ordinality AND metric <> :metric::overview_metric GROUP BY metric ) @@ -54,19 +54,3 @@ 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); - - --- INSERT INTO user_overview_metric_preference (metric, user_id_code, active, ordinality) --- SELECT --- metric, --- user_id_code, --- active, --- CASE --- WHEN ordinality >= :ordinality THEN ordinality + 1 --- ELSE ordinality --- END --- FROM user_overview_metric_preference --- WHERE ordinality >= :ordinality --- AND ordinality < old.ordinality --- AND metric <> :metric::overview_metric --- AND user_id_code = :user_id_code;