Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor all Service Module's Postgres queries to replace DELETE queries with INSERT and SELECT #282

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- liquibase formatted sql
-- changeset baha-a:1715683083
ALTER TABLE services
DROP CONSTRAINT IF EXISTS services_service_id_key;
6 changes: 1 addition & 5 deletions DSL/Resql/services/add.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
INSERT INTO services (name, description, service_id, ruuter_type, is_common, structure)
VALUES (:name, :description, :service_id, :ruuter_type::ruuter_request_type, :is_common, :structure::json)
ON CONFLICT (service_id) DO UPDATE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can have conflicts as service id is generated, and may conflict

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service_id is not auto-generated, it's defined as TEXT, plus, I've removed the unique constraint on it here, As a result, we can now insert multiple service_id with the same value.

SET name = EXCLUDED.name,
description = EXCLUDED.description,
ruuter_type = EXCLUDED.ruuter_type;
VALUES (:name, :description, :service_id, :ruuter_type::ruuter_request_type, :is_common, :structure::json);
18 changes: 17 additions & 1 deletion DSL/Resql/services/delete-service.sql
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
DELETE FROM services WHERE service_id = :id
INSERT INTO services (name, description, created_at, updated_at, ruuter_type, current_state, service_id, is_common, deleted, structure, endpoints)
SELECT
name,
description,
created_at,
updated_at,
ruuter_type,
current_state,
service_id,
is_common,
TRUE AS deleted,
structure,
endpoints
FROM services
WHERE service_id = :id
ORDER BY id DESC
LIMIT 1;
16 changes: 12 additions & 4 deletions DSL/Resql/services/get-service-by-id.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
WITH MaxService AS (
SELECT id AS maxId
FROM services
WHERE service_id = :id
ORDER BY id
LIMIT 1
)
SELECT
id,
name,
description,
current_state AS state,
ruuter_type AS type,
is_common AS isCommon,
structure::json,
subquery.endpoints::json AS endpoints,
description,
service_id
FROM services
JOIN MaxService ON id = maxId
JOIN (
SELECT jsonb_agg(endpoint) AS endpoints
FROM (
Expand All @@ -19,10 +27,10 @@ JOIN (
WHERE (endpoint->>'isCommon')::boolean = true
UNION
SELECT endpoint::jsonb
FROM services, json_array_elements(endpoints) AS endpoint
WHERE service_id = :id
FROM services, json_array_elements(endpoints) AS endpoint, MaxService
WHERE id = maxId
) AS combined_endpoints
) subquery
) subquery ON true
WHERE deleted = false AND service_id = :id
WHERE NOT deleted
ORDER BY id ASC;
4 changes: 3 additions & 1 deletion DSL/Resql/services/get-service-name-by-id.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
SELECT name FROM services
WHERE service_id = :id;
WHERE service_id = :id
ORDER BY id DESC
LIMIT 1;
10 changes: 9 additions & 1 deletion DSL/Resql/services/get-services-list.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
WITH MaxServices AS (
SELECT MAX(id) AS maxId
FROM services
GROUP BY service_id
)
SELECT
id,
name,
Expand All @@ -9,6 +14,7 @@ SELECT
subquery.endpoints::json AS endpoints,
service_id
FROM services
JOIN MaxServices ON id = maxId
JOIN (
SELECT jsonb_agg(endpoint) AS endpoints
FROM (
Expand All @@ -19,8 +25,10 @@ JOIN (
WHERE (endpoint->>'isCommon')::boolean = true
UNION
SELECT endpoint::jsonb
FROM services, json_array_elements(endpoints) AS endpoint
FROM services, json_array_elements(endpoints) AS endpoint, MaxServices
WHERE id = maxId
) AS combined_endpoints
) subquery
) subquery ON true
WHERE NOT deleted
ORDER BY id ASC;
4 changes: 3 additions & 1 deletion DSL/Resql/services/status.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
SELECT current_state, ruuter_type FROM services
WHERE service_id = :id;
WHERE service_id = :id
ORDER BY id DESC
LIMIT 1;