Skip to content

Commit

Permalink
Merge pull request #384 from onc-healthit/LANTERN-595-remove-duplicat…
Browse files Browse the repository at this point in the history
…es-healthit_products_map

Lantern 595 remove duplicates healthit products map
  • Loading branch information
vishnu-mettles authored Aug 22, 2024
2 parents 2aaa14d + 2b1f76b commit 42f5f94
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func GetAndSendVersionsResponse(ctx context.Context, args *map[string]interface{
time.Sleep(time.Duration(500 * time.Millisecond))
req, err := http.NewRequest("GET", versionsURL, nil)
if err != nil {
log.Errorf("unable to create new GET request from URL: " + versionsURL)
log.Errorf("unable to create new GET request from URL: %s", versionsURL)
} else {
req.Header.Set("User-Agent", qa.UserAgent)
trace := &httptrace.ClientTrace{}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BEGIN;

ALTER TABLE IF EXISTS public.healthit_products_map DROP CONSTRAINT IF EXISTS unique_id_healthit_product;


COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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();

ALTER TABLE IF EXISTS public.healthit_products_map ADD CONSTRAINT unique_id_healthit_product UNIQUE (id, healthit_product_id);

COMMIT;
3 changes: 2 additions & 1 deletion db/sql/dbsetup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ CREATE TABLE healthit_products (

CREATE TABLE healthit_products_map (
id SERIAL,
healthit_product_id INT REFERENCES healthit_products(id) ON DELETE SET NULL
healthit_product_id INT REFERENCES healthit_products(id) ON DELETE SET NULL,
CONSTRAINT unique_id_healthit_product UNIQUE(id, healthit_product_id)
);

CREATE TABLE certification_criteria (
Expand Down
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 42f5f94

Please sign in to comment.