Skip to content

Commit

Permalink
Refactor all Analytics Module's Postgres queries to replace DELETE
Browse files Browse the repository at this point in the history
…queries with `INSERT` and `SELECT` (#226)

* Replace DELETE with INSERT

* Fix sonar issues
  • Loading branch information
baha-a authored May 20, 2024
1 parent 63bd2c2 commit fb0ff57
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- liquibase formatted sql
-- changeset baha-a:1715604012
ALTER TABLE scheduled_reports
ADD COLUMN IF NOT EXISTS deleted boolean NOT NULL DEFAULT FALSE;
9 changes: 7 additions & 2 deletions DSL/Resql/delete-odp-settings.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
DELETE FROM "configuration"
WHERE "key" LIKE 'odp_%';
INSERT INTO configuration (key, value, deleted)
SELECT
key,
NULL AS value,
TRUE AS deleted
FROM configuration
WHERE key LIKE 'odp_%'
14 changes: 13 additions & 1 deletion DSL/Resql/delete-scheduled-report.sql
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
DELETE FROM scheduled_reports WHERE dataset_id = :id
INSERT INTO scheduled_reports (name, period, metrics, dataset_id, start_date, end_date, deleted)
SELECT
name,
period,
metrics,
dataset_id,
start_date,
end_date,
TRUE
FROM scheduled_reports
WHERE dataset_id = :id
ORDER BY id DESC
LIMIT 1;
4 changes: 3 additions & 1 deletion DSL/Resql/get-scheduled-report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ SELECT
start_date,
end_date
FROM scheduled_reports
WHERE dataset_id = :datasetId;
WHERE dataset_id = :datasetId
ORDER BY id DESC
LIMIT 1;
9 changes: 8 additions & 1 deletion DSL/Resql/get-scheduled-reports.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
WITH MaxReports AS (
SELECT MAX(id) AS maxId
FROM scheduled_reports
GROUP BY dataset_id
)
SELECT
id,
name,
Expand All @@ -8,4 +13,6 @@ SELECT
updated,
start_date,
end_date
FROM scheduled_reports;
FROM scheduled_reports
JOIN MaxReports ON id = maxId
WHERE NOT deleted;

0 comments on commit fb0ff57

Please sign in to comment.