LP#1587639 Avoid double-update of editor/edit_date user/miker/lp1587639-vand-auth-edit-date
authorMike Rylander <mrylander@gmail.com>
Thu, 4 Aug 2016 18:00:17 +0000 (14:00 -0400)
committerMike Rylander <mrylander@gmail.com>
Thu, 4 Aug 2016 18:02:02 +0000 (14:02 -0400)
Look for an editor first, and if not found just use the existing values.

Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/sql/Pg/012.schema.vandelay.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.authority-vandeley-edit-date.sql

index 2e5c248..ff57e4c 100644 (file)
@@ -1786,9 +1786,11 @@ DECLARE
     merge_profile   vandelay.merge_profile%ROWTYPE;
     dyn_profile     vandelay.compile_profile%ROWTYPE;
     editor_string   TEXT;
-    editor_id       INT;
+    new_editor      INT;
+    new_edit_date   TIMESTAMPTZ;
     source_marc     TEXT;
     target_marc     TEXT;
+    eg_marc_row     authority.record_entry%ROWTYPE;
     eg_marc         TEXT;
     v_marc          TEXT;
     replace_rule    TEXT;
@@ -1797,7 +1799,7 @@ DECLARE
     update_query    TEXT;
 BEGIN
 
-    SELECT  b.marc INTO eg_marc
+    SELECT  * INTO eg_marc_row
       FROM  authority.record_entry b
             JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id)
       LIMIT 1;
@@ -1812,11 +1814,37 @@ BEGIN
         RETURN FALSE;
     END IF;
 
+    eg_marc := eg_marc_row.marc
+
     -- Extract the editor string before any modification to the vandelay
     -- MARC occur.
     editor_string := 
         (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1];
 
+    -- If an editor value can be found, update the authority record
+    -- editor and edit_date values.
+    IF editor_string IS NOT NULL AND editor_string <> '' THEN
+
+        -- Vandelay.pm sets the value to 'usrname' when needed.  
+        SELECT id INTO new_editor
+            FROM actor.usr WHERE usrname = editor_string;
+
+        IF new_editor IS NULL THEN
+            SELECT usr INTO new_editor
+                FROM actor.card WHERE barcode = editor_string;
+        END IF;
+
+        IF new_editor IS NOT NULL THEN
+            new_edit_date := NOW();
+        ELSE -- No valid editor, use current values
+            new_editor = eg_marc_row.editor;
+            new_edit_date = eg_marc_row.edit_date;
+        END IF;
+    ELSE
+        new_editor = eg_marc_row.editor;
+        new_edit_date = eg_marc_row.edit_date;
+    END IF;
+
     dyn_profile := vandelay.compile_profile( v_marc );
 
     IF merge_profile_id IS NOT NULL THEN
@@ -1848,7 +1876,9 @@ BEGIN
     END IF;
 
     UPDATE  authority.record_entry
-      SET   marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
+      SET   marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule ),
+            editor = new_editor,
+            edit_date = new_edit_date
       WHERE id = eg_id;
 
     IF NOT FOUND THEN 
@@ -1864,34 +1894,6 @@ BEGIN
             import_time = NOW()
       WHERE id = import_id;
 
-    -- If an editor value can be found, update the authority record
-    -- editor and edit_date values.
-    IF editor_string IS NOT NULL AND editor_string <> '' THEN
-
-        -- Vandelay.pm sets the value to 'usrname' when needed.  
-        SELECT id INTO editor_id 
-            FROM actor.usr WHERE usrname = editor_string;
-
-        IF editor_id IS NULL THEN
-            SELECT usr INTO editor_id 
-                FROM actor.card WHERE barcode = editor_string;
-        END IF;
-
-        IF editor_id IS NOT NULL THEN
-            --only update the edit date if we have a valid editor
-            update_fields := ARRAY_APPEND(update_fields, 
-                'editor = ' || editor_id || ', edit_date = NOW()');
-        END IF;
-    END IF;
-
-    IF ARRAY_LENGTH(update_fields, 1) > 0 THEN
-        update_query := 'UPDATE authority.record_entry SET ' || 
-            ARRAY_TO_STRING(update_fields, ',') || 
-            ' WHERE id = ' || eg_id || ';';
-        --RAISE NOTICE 'query: %', update_query;
-        EXECUTE update_query;
-    END IF;
-
     RETURN TRUE;
 
 END;
index a8b3a91..3a2362e 100644 (file)
@@ -7,9 +7,11 @@ DECLARE
     merge_profile   vandelay.merge_profile%ROWTYPE;
     dyn_profile     vandelay.compile_profile%ROWTYPE;
     editor_string   TEXT;
-    editor_id       INT;
+    new_editor      INT;
+    new_edit_date   TIMESTAMPTZ;
     source_marc     TEXT;
     target_marc     TEXT;
+    eg_marc_row     authority.record_entry%ROWTYPE;
     eg_marc         TEXT;
     v_marc          TEXT;
     replace_rule    TEXT;
@@ -18,7 +20,7 @@ DECLARE
     update_query    TEXT;
 BEGIN
 
-    SELECT  b.marc INTO eg_marc
+    SELECT  * INTO eg_marc_row
       FROM  authority.record_entry b
             JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id)
       LIMIT 1;
@@ -33,11 +35,37 @@ BEGIN
         RETURN FALSE;
     END IF;
 
+    eg_marc := eg_marc_row.marc
+
     -- Extract the editor string before any modification to the vandelay
     -- MARC occur.
     editor_string := 
         (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1];
 
+    -- If an editor value can be found, update the authority record
+    -- editor and edit_date values.
+    IF editor_string IS NOT NULL AND editor_string <> '' THEN
+
+        -- Vandelay.pm sets the value to 'usrname' when needed.  
+        SELECT id INTO new_editor
+            FROM actor.usr WHERE usrname = editor_string;
+
+        IF new_editor IS NULL THEN
+            SELECT usr INTO new_editor
+                FROM actor.card WHERE barcode = editor_string;
+        END IF;
+
+        IF new_editor IS NOT NULL THEN
+            new_edit_date := NOW();
+        ELSE -- No valid editor, use current values
+            new_editor = eg_marc_row.editor;
+            new_edit_date = eg_marc_row.edit_date;
+        END IF;
+    ELSE
+        new_editor = eg_marc_row.editor;
+        new_edit_date = eg_marc_row.edit_date;
+    END IF;
+
     dyn_profile := vandelay.compile_profile( v_marc );
 
     IF merge_profile_id IS NOT NULL THEN
@@ -69,7 +97,9 @@ BEGIN
     END IF;
 
     UPDATE  authority.record_entry
-      SET   marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
+      SET   marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule ),
+            editor = new_editor,
+            edit_date = new_edit_date
       WHERE id = eg_id;
 
     IF NOT FOUND THEN 
@@ -85,34 +115,6 @@ BEGIN
             import_time = NOW()
       WHERE id = import_id;
 
-    -- If an editor value can be found, update the authority record
-    -- editor and edit_date values.
-    IF editor_string IS NOT NULL AND editor_string <> '' THEN
-
-        -- Vandelay.pm sets the value to 'usrname' when needed.  
-        SELECT id INTO editor_id 
-            FROM actor.usr WHERE usrname = editor_string;
-
-        IF editor_id IS NULL THEN
-            SELECT usr INTO editor_id 
-                FROM actor.card WHERE barcode = editor_string;
-        END IF;
-
-        IF editor_id IS NOT NULL THEN
-            --only update the edit date if we have a valid editor
-            update_fields := ARRAY_APPEND(update_fields, 
-                'editor = ' || editor_id || ', edit_date = NOW()');
-        END IF;
-    END IF;
-
-    IF ARRAY_LENGTH(update_fields, 1) > 0 THEN
-        update_query := 'UPDATE authority.record_entry SET ' || 
-            ARRAY_TO_STRING(update_fields, ',') || 
-            ' WHERE id = ' || eg_id || ';';
-        --RAISE NOTICE 'query: %', update_query;
-        EXECUTE update_query;
-    END IF;
-
     RETURN TRUE;
 
 END;