]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1186.schema.fix_auto_overlay_org_unit_copies.sql
LP#1845260: add additional DB update to be sure
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1186.schema.fix_auto_overlay_org_unit_copies.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('1186', :eg_version);
4
5 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_org_unit_copies ( import_id BIGINT, merge_profile_id INT, lwm_ratio_value_p NUMERIC ) RETURNS BOOL AS $$
6 DECLARE
7     eg_id           BIGINT;
8     match_count     INT;
9     rec             vandelay.bib_match%ROWTYPE;
10     v_owning_lib    INT;
11     scope_org       INT;
12     scope_orgs      INT[];
13     copy_count      INT := 0;
14     max_copy_count  INT := 0;
15 BEGIN
16
17     PERFORM * FROM vandelay.queued_bib_record WHERE import_time IS NOT NULL AND id = import_id;
18
19     IF FOUND THEN
20         -- RAISE NOTICE 'already imported, cannot auto-overlay'
21         RETURN FALSE;
22     END IF;
23
24     -- Gather all the owning libs for our import items.
25     -- These are our initial scope_orgs.
26     SELECT ARRAY_AGG(DISTINCT owning_lib) INTO scope_orgs
27         FROM vandelay.import_item
28         WHERE record = import_id;
29
30     WHILE CARDINALITY(scope_orgs) > 0 LOOP
31         FOR scope_org IN SELECT * FROM UNNEST(scope_orgs) LOOP
32             -- For each match, get a count of all copies at descendants of our scope org.
33             FOR rec IN SELECT * FROM vandelay.bib_match AS vbm
34                 WHERE queued_record = import_id
35                 ORDER BY vbm.eg_record DESC
36             LOOP
37                 SELECT COUNT(acp.id) INTO copy_count
38                     FROM asset.copy AS acp
39                     INNER JOIN asset.call_number AS acn
40                         ON acp.call_number = acn.id
41                     WHERE acn.owning_lib IN (SELECT id FROM
42                         actor.org_unit_descendants(scope_org))
43                     AND acn.record = rec.eg_record
44                     AND acp.deleted = FALSE;
45                 IF copy_count > max_copy_count THEN
46                     max_copy_count := copy_count;
47                     eg_id := rec.eg_record;
48                 END IF;
49             END LOOP;
50         END LOOP;
51
52         -- If no matching bibs had holdings, gather our next set of orgs to check, and iterate.
53         IF max_copy_count = 0 THEN 
54             SELECT ARRAY_AGG(DISTINCT parent_ou) INTO scope_orgs
55                 FROM actor.org_unit
56                 WHERE id IN (SELECT * FROM UNNEST(scope_orgs))
57                 AND parent_ou IS NOT NULL;
58         END IF;
59     END LOOP;
60
61     IF eg_id IS NULL THEN
62         -- Could not determine best match via copy count
63         -- fall back to default best match
64         IF (SELECT * FROM vandelay.auto_overlay_bib_record_with_best( import_id, merge_profile_id, lwm_ratio_value_p )) THEN
65             RETURN TRUE;
66         ELSE
67             RETURN FALSE;
68         END IF;
69     END IF;
70
71     RETURN vandelay.overlay_bib_record( import_id, eg_id, merge_profile_id );
72 END;
73 $$ LANGUAGE PLPGSQL;
74
75 COMMIT;