3 SELECT evergreen.upgrade_deps_block_check('1186', :eg_version);
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 $$
9 rec vandelay.bib_match%ROWTYPE;
14 max_copy_count INT := 0;
17 PERFORM * FROM vandelay.queued_bib_record WHERE import_time IS NOT NULL AND id = import_id;
20 -- RAISE NOTICE 'already imported, cannot auto-overlay'
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;
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
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;
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
56 WHERE id IN (SELECT * FROM UNNEST(scope_orgs))
57 AND parent_ou IS NOT NULL;
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
71 RETURN vandelay.overlay_bib_record( import_id, eg_id, merge_profile_id );