Skip to content

Commit

Permalink
LANTERN-595, LANTERN-596: Removal and prevention of duplicates in hea…
Browse files Browse the repository at this point in the history
…lthit_products_map
  • Loading branch information
archita-ekkirala committed Aug 10, 2024
1 parent 317fe1b commit 78b22cc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BEGIN;

ALTER TABLE public.healthit_products_map DROP CONSTRAINT unique_id_healthit_product;


COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
BEGIN;

CREATE OR REPLACE FUNCTION delete_duplicate_data_in_healthit_products_map() RETURNS VOID as $$
BEGIN
WITH CTE AS (
SELECT
ctid,
ROW_NUMBER() OVER(PARTITION BY id, healthit_product_id ORDER BY id) AS DuplicateCount
FROM
public.healthit_products_map
)
DELETE FROM public.healthit_products_map
WHERE ctid IN (
SELECT ctid
FROM CTE
WHERE DuplicateCount > 1
);
END;
$$ LANGUAGE plpgsql;

SELECT delete_duplicate_data_in_healthit_products_map();

COMMIT;
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,7 @@ func (s *Store) DeleteLinksByProduct(ctx context.Context, productID int) error {

func prepareHealthITProductStatements(s *Store) error {
var err error
addHealthITProductMapStatement, err = s.DB.Prepare(`
INSERT INTO healthit_products_map (id, healthit_product_id)
VALUES ($1, $2)
RETURNING id;`)
addHealthITProductMapStatement, err = s.DB.Prepare(`INSERT INTO healthit_products_map (id, healthit_product_id) VALUES ($1, $2) ON CONFLICT (id, healthit_product_id) DO UPDATE SET id=EXCLUDED.id RETURNING id;`)
if err != nil {
return err
}
Expand Down

0 comments on commit 78b22cc

Please sign in to comment.