forked from archesproject/arches
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the refrech relationshipdb function, archesproject#9944
- Loading branch information
Showing
1 changed file
with
170 additions
and
0 deletions.
There are no files selected for viewing
170 changes: 170 additions & 0 deletions
170
arches/app/models/migrations/9944_improve_refresh_relationship_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('models', '10041_limit_number_of_bulk_edit'), | ||
] | ||
|
||
update_refresh_tile_resource_relationship_function = """ | ||
CREATE OR REPLACE FUNCTION __arches_refresh_tile_resource_relationships(tile_id uuid) | ||
RETURNS boolean AS $$ | ||
DECLARE | ||
resource_id uuid; | ||
BEGIN | ||
SELECT resourceinstanceid INTO resource_id FROM tiles WHERE tileid = tile_id; | ||
DELETE FROM resource_x_resource WHERE tileid = tile_id; | ||
WITH relationships AS ( | ||
SELECT n.nodeid, | ||
jsonb_array_elements(t.tiledata->n.nodeid::text) AS relationship | ||
FROM tiles t | ||
LEFT JOIN nodes n ON t.nodegroupid = n.nodegroupid | ||
WHERE n.datatype IN ('resource-instance-list', 'resource-instance') | ||
AND t.tileid = tile_id | ||
AND t.tiledata->>n.nodeid::text IS NOT null | ||
) | ||
INSERT INTO resource_x_resource ( | ||
resourcexid, | ||
notes, | ||
relationshiptype, | ||
resourceinstanceidfrom, | ||
resourceinstanceidto, | ||
inverserelationshiptype, | ||
tileid, | ||
nodeid, | ||
created, | ||
modified, | ||
resourceinstancefrom_graphid, | ||
resourceinstanceto_graphid | ||
) SELECT | ||
CASE relationship->>'resourceXresourceId' | ||
WHEN '' THEN uuid_generate_v4() | ||
ELSE (relationship->>'resourceXresourceId')::uuid | ||
END, | ||
'', | ||
relationship->>'ontologyProperty', | ||
resource_id, | ||
(relationship->>'resourceId')::uuid, | ||
relationship->>'inverseOntologyProperty', | ||
tile_id, | ||
nodeid, | ||
now(), | ||
now(), | ||
resourcefrom.graphid, | ||
resourceto.graphid | ||
FROM relationships r | ||
LEFT JOIN resource_instances resourcefrom ON resourcefrom.resourceinstanceid = resource_id | ||
LEFT JOIN resource_instances resourceto ON resourceto.resourceinstanceid = (r.relationship ->> 'resourceId')::uuid; | ||
RETURN true; | ||
END; | ||
$$ language plpgsql; | ||
""", | ||
|
||
revert_refresh_tile_resource_relationship_function = """ | ||
CREATE OR REPLACE FUNCTION __arches_refresh_tile_resource_relationships(tile_id uuid) | ||
RETURNS boolean AS $$ | ||
DECLARE | ||
resource_id uuid; | ||
BEGIN | ||
SELECT resourceinstanceid INTO resource_id FROM tiles WHERE tileid = tile_id; | ||
DELETE FROM resource_x_resource WHERE tileid = tile_id; | ||
WITH relationships AS ( | ||
SELECT n.nodeid, | ||
jsonb_array_elements(t.tiledata->n.nodeid::text) AS relationship | ||
FROM tiles t | ||
LEFT JOIN nodes n ON t.nodegroupid = n.nodegroupid | ||
WHERE n.datatype IN ('resource-instance-list', 'resource-instance') | ||
AND t.tileid = tile_id | ||
AND t.tiledata->>n.nodeid::text IS NOT null | ||
) | ||
INSERT INTO resource_x_resource ( | ||
resourcexid, | ||
notes, | ||
relationshiptype, | ||
resourceinstanceidfrom, | ||
resourceinstanceidto, | ||
inverserelationshiptype, | ||
tileid, | ||
nodeid, | ||
created, | ||
modified | ||
) SELECT | ||
CASE relationship->>'resourceXresourceId' | ||
WHEN '' THEN uuid_generate_v4() | ||
ELSE (relationship->>'resourceXresourceId')::uuid | ||
END, | ||
'', | ||
relationship->>'ontologyProperty', | ||
resource_id, | ||
(relationship->>'resourceId')::uuid, | ||
relationship->>'inverseOntologyProperty', | ||
tile_id, | ||
nodeid, | ||
now(), | ||
now() | ||
FROM relationships; | ||
UPDATE resource_x_resource x | ||
SET resourceinstancefrom_graphid = r.graphid | ||
FROM resource_instances r | ||
WHERE r.resourceinstanceid = x.resourceinstanceidfrom | ||
AND x.resourceinstancefrom_graphid is null; | ||
UPDATE resource_x_resource x | ||
SET resourceinstanceto_graphid = r.graphid | ||
FROM resource_instances r | ||
WHERE r.resourceinstanceid = x.resourceinstanceidto | ||
AND x.resourceinstanceto_graphid is null; | ||
RETURN true; | ||
END; | ||
$$ language plpgsql; | ||
""" | ||
|
||
update_check_excess_tiles_trigger = """ | ||
create or replace procedure __arches_complete_bulk_load() AS | ||
$$ | ||
DECLARE | ||
cardinality_violations bigint; | ||
BEGIN | ||
alter table tiles enable trigger __arches_check_excess_tiles_trigger; | ||
alter table tiles enable trigger __arches_trg_update_spatial_attributes; | ||
END | ||
$$ | ||
language plpgsql; | ||
""" | ||
|
||
restore_check_excess_tiles_trigger = """ | ||
create or replace procedure __arches_complete_bulk_load() AS | ||
$$ | ||
DECLARE | ||
cardinality_violations bigint; | ||
BEGIN | ||
alter table tiles enable trigger __arches_check_excess_tiles_trigger; | ||
alter table tiles enable trigger __arches_trg_update_spatial_attributes; | ||
END | ||
$$ | ||
language plpgsql; | ||
""" | ||
|
||
|
||
operations = [ | ||
migrations.RunSQL(update_check_excess_tiles_trigger, restore_check_excess_tiles_trigger), | ||
] | ||
|
||
|
||
operations = [ | ||
migrations.RunSQL( | ||
update_refresh_tile_resource_relationship_function, | ||
revert_refresh_tile_resource_relationship_function, | ||
), | ||
migrations.RunSQL( | ||
update_check_excess_tiles_trigger, | ||
restore_check_excess_tiles_trigger | ||
), | ||
] |