Avoid duplicate row issues in biblio.extract_located_uris
authorDan Scott <dan@coffeecode.net>
Wed, 15 Jun 2011 01:34:15 +0000 (21:34 -0400)
committerDan Scott <dan@coffeecode.net>
Wed, 15 Jun 2011 01:34:15 +0000 (21:34 -0400)
As there is currently no unique constraint on the non-ID columns of
asset.uri, ensure that we only select a single ID from the tables until
we dedupe the tables. Use an ORDER BY to grab the matching row with the
lowest ID so we can clear out the rows with higher IDs.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Open-ILS/src/sql/Pg/030.schema.metabib.sql

index 7e86d47..17a2383 100644 (file)
@@ -880,16 +880,26 @@ BEGIN
 
                 -- look for a matching uri
                 IF uri_use IS NULL THEN
-                    SELECT id INTO uri_id FROM asset.uri WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active;
+                    SELECT id INTO uri_id
+                        FROM asset.uri
+                        WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active
+                        ORDER BY id LIMIT 1;
                     IF NOT FOUND THEN -- create one
                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
-                        SELECT id INTO uri_id FROM asset.uri WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active;
+                        SELECT id INTO uri_id
+                            FROM asset.uri
+                            WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active;
                     END IF;
                 ELSE
-                    SELECT id INTO uri_id FROM asset.uri WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
+                    SELECT id INTO uri_id
+                        FROM asset.uri
+                        WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active
+                        ORDER BY id LIMIT 1;
                     IF NOT FOUND THEN -- create one
                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
-                        SELECT id INTO uri_id FROM asset.uri WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
+                        SELECT id INTO uri_id
+                            FROM asset.uri
+                            WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
                     END IF;
                 END IF;