Skip to content

Commit

Permalink
Merge pull request #363 from geovistory/pgwar-community-entity-labels…
Browse files Browse the repository at this point in the history
…-review

add review changes
  • Loading branch information
dferhod authored Jun 20, 2024
2 parents ce2259e + 635ab49 commit b37792c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ DROP FUNCTION IF EXISTS pgwar.update_entity_label_on_entity_label_config_change;
* Functions
***/

-- Drop function pgwar.update_project_entity_label
DROP FUNCTION IF EXISTS pgwar.update_project_entity_label;
-- Drop function pgwar.update_entity_label_of_entity_preview
DROP FUNCTION IF EXISTS pgwar.update_entity_label_of_entity_preview;

-- Drop function pgwar.get_project_entity_label(entity_id int, project_id int, label_config jsonb)
DROP FUNCTION IF EXISTS pgwar.get_project_entity_label(entity_id int, project_id int, label_config jsonb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,14 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

-- update entity label or project entity
CREATE OR REPLACE FUNCTION pgwar.update_project_entity_label(entity_id int, project_id int, new_label text)
-- update entity label of entity preview, if distinct
CREATE OR REPLACE FUNCTION pgwar.update_entity_label_of_entity_preview(entity_id int, project_id int, new_label text)
RETURNS void AS $$
BEGIN
UPDATE pgwar.entity_preview
SET entity_label = new_label
WHERE pk_entity = entity_id
AND fk_project = project_id
AND fk_project != 0
AND entity_label IS DISTINCT FROM new_label;
END;
$$ LANGUAGE plpgsql;
Expand All @@ -204,12 +203,12 @@ BEGIN
subject_entity_id := COALESCE(NEW.fk_subject_info, OLD.fk_subject_info);

-- Update the label for the subject entity
PERFORM pgwar.update_project_entity_label(subject_entity_id, project_id, pgwar.get_project_entity_label(subject_entity_id, project_id));
PERFORM pgwar.update_entity_label_of_entity_preview(subject_entity_id, project_id, pgwar.get_project_entity_label(subject_entity_id, project_id));

-- Check if the object is an entity (object_label IS NULL)
IF COALESCE(NEW.object_label, OLD.object_label) IS NULL THEN
object_entity_id := COALESCE(NEW.fk_object_info, OLD.fk_object_info);
PERFORM pgwar.update_project_entity_label(object_entity_id, project_id, pgwar.get_project_entity_label(object_entity_id, project_id));
PERFORM pgwar.update_entity_label_of_entity_preview(object_entity_id, project_id, pgwar.get_project_entity_label(object_entity_id, project_id));
END IF;

RETURN NULL;
Expand All @@ -234,7 +233,7 @@ BEGIN
entity_id := COALESCE(NEW.pk_entity, OLD.pk_entity);

-- Update the entity label in pgwar.entity_preview
PERFORM pgwar.update_project_entity_label(entity_id, project_id, pgwar.get_project_entity_label(entity_id, project_id));
PERFORM pgwar.update_entity_label_of_entity_preview(entity_id, project_id, pgwar.get_project_entity_label(entity_id, project_id));

RETURN NULL;
END;
Expand All @@ -257,14 +256,14 @@ BEGIN
entity_id := COALESCE(NEW.pk_entity, OLD.pk_entity);

-- Update the entity labels of the related object entities
PERFORM pgwar.update_project_entity_label(fk_object_info, project_id, pgwar.get_project_entity_label(fk_object_info, project_id))
PERFORM pgwar.update_entity_label_of_entity_preview(fk_object_info, project_id, pgwar.get_project_entity_label(fk_object_info, project_id))
FROM pgwar.project_statements
WHERE fk_subject_info = entity_id
AND object_label IS NULL
AND fk_project = project_id;

-- Update the entity labels of the related subject entities
PERFORM pgwar.update_project_entity_label(fk_subject_info, project_id, pgwar.get_project_entity_label(fk_subject_info, project_id))
PERFORM pgwar.update_entity_label_of_entity_preview(fk_subject_info, project_id, pgwar.get_project_entity_label(fk_subject_info, project_id))
FROM pgwar.project_statements
WHERE fk_object_info = entity_id
AND fk_project = project_id;
Expand All @@ -273,9 +272,16 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER on_upsert_entity_preview_entity_label
AFTER DELETE OR INSERT OR UPDATE OF entity_label ON pgwar.entity_preview
CREATE TRIGGER after_upsert_entity_preview_entity_label_01
AFTER INSERT OR UPDATE OF entity_label ON pgwar.entity_preview
FOR EACH ROW
WHEN (NEW.fk_project IS DISTINCT FROM 0)
EXECUTE FUNCTION pgwar.update_entity_label_on_entity_label_change();

CREATE TRIGGER after_delete_entity_preview_01
AFTER DELETE ON pgwar.entity_preview
FOR EACH ROW
WHEN (OLD.fk_project IS DISTINCT FROM 0)
EXECUTE FUNCTION pgwar.update_entity_label_on_entity_label_change();


Expand All @@ -294,7 +300,7 @@ BEGIN
IF project_id = 375669 THEN

-- perform update of entity labels that depend on the default config of project 375669
PERFORM pgwar.update_project_entity_label(ep.pk_entity, ep.fk_project, pgwar.get_project_entity_label(ep.pk_entity, ep.fk_project))
PERFORM pgwar.update_entity_label_of_entity_preview(ep.pk_entity, ep.fk_project, pgwar.get_project_entity_label(ep.pk_entity, ep.fk_project))
FROM pgwar.entity_preview ep
LEFT JOIN projects.entity_label_config c
ON c.fk_class = class_id
Expand All @@ -305,7 +311,7 @@ BEGIN

ELSE

PERFORM pgwar.update_project_entity_label(pk_entity, fk_project, pgwar.get_project_entity_label(pk_entity, fk_project))
PERFORM pgwar.update_entity_label_of_entity_preview(pk_entity, fk_project, pgwar.get_project_entity_label(pk_entity, fk_project))
FROM pgwar.entity_preview
WHERE fk_class = class_id
AND fk_project = project_id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
/* Replace with your SQL commands */
/***
* Revert Functions and Triggers
***/

/***
* Triggers
***/

-- Drop trigger on_modify_project_statement
DROP TRIGGER IF EXISTS after_delete_entity_preview_02 ON pgwar.entity_preview;

-- Drop trigger on_modify_project_statement
DROP TRIGGER IF EXISTS after_upsert_entity_preview_entity_label_02 ON pgwar.entity_preview;

/***
* Functions
***/

-- Drop function pgwar.update_community_entity_label_on_project_entity_label_change
DROP FUNCTION IF EXISTS pgwar.update_community_entity_label_on_project_entity_label_change;

-- Drop function pgwar.get_and_update_community_entity_label
DROP FUNCTION IF EXISTS pgwar.get_and_update_community_entity_label;

-- Drop function pgwar.get_most_frequent_entity_label
DROP FUNCTION IF EXISTS pgwar.get_most_frequent_entity_label;

Original file line number Diff line number Diff line change
Expand Up @@ -19,132 +19,56 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

-- Function to upsert on pgwar.entity_preview
-----------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION pgwar.upsert_community_entity_preview(ep pgwar.entity_preview)
RETURNS VOID
AS $$
-- get and update community entity label
CREATE OR REPLACE FUNCTION pgwar.get_and_update_community_entity_label(entity_id int)
RETURNS void AS $$
DECLARE
label text;
BEGIN
INSERT INTO pgwar.entity_preview(
pk_entity,
fk_project,
fk_class,
entity_type,
class_label,
entity_label,
full_text,
ts_vector,
type_label,
fk_type,
time_span,
first_second,
last_second
)
VALUES(
ep.pk_entity,
0,
ep.fk_class,
ep.entity_type,
ep.class_label,
ep.entity_label,
ep.full_text,
ep.ts_vector,
ep.type_label,
ep.fk_type,
ep.time_span,
ep.first_second,
ep.last_second
)
ON CONFLICT(pk_entity, fk_project)
DO UPDATE SET
-- ... or update the pgwar.entity_preview
fk_class = EXCLUDED.fk_class,
entity_type = EXCLUDED.entity_type,
class_label = EXCLUDED.class_label,
entity_label = EXCLUDED.entity_label,
full_text = EXCLUDED.full_text,
ts_vector = EXCLUDED.ts_vector,
type_label = EXCLUDED.type_label,
fk_type = EXCLUDED.fk_type,
time_span = EXCLUDED.time_span,
first_second = EXCLUDED.first_second,
last_second = EXCLUDED.last_second
WHERE
-- ... where it is distinct from previous value
entity_preview.fk_class IS DISTINCT FROM EXCLUDED.fk_class OR
entity_preview.entity_type IS DISTINCT FROM EXCLUDED.entity_type OR
entity_preview.class_label IS DISTINCT FROM EXCLUDED.class_label OR
entity_preview.entity_label IS DISTINCT FROM EXCLUDED.entity_label OR
entity_preview.full_text IS DISTINCT FROM EXCLUDED.full_text OR
entity_preview.ts_vector IS DISTINCT FROM EXCLUDED.ts_vector OR
entity_preview.type_label IS DISTINCT FROM EXCLUDED.type_label OR
entity_preview.fk_type IS DISTINCT FROM EXCLUDED.fk_type OR
entity_preview.time_span IS DISTINCT FROM EXCLUDED.time_span OR
entity_preview.first_second IS DISTINCT FROM EXCLUDED.first_second OR
entity_preview.last_second IS DISTINCT FROM EXCLUDED.last_second;

-- check if communty entity exists in pgwar.entity_preview
IF EXISTS(
SELECT pk_entity
FROM pgwar.entity_preview
WHERE pk_entity = entity_id
AND fk_project = 0
) THEN
-- get community entity label
label := pgwar.get_most_frequent_entity_label(entity_id);

-- update entity preview
PERFORM pgwar.update_entity_label_of_entity_preview(entity_id, 0, label);

END IF;

END;
$$
LANGUAGE plpgsql;
$$ LANGUAGE plpgsql;



-- Update community entity labels on change on projects entity labels
CREATE OR REPLACE FUNCTION pgwar.update_community_entity_label_on_project_entity_label_change()
RETURNS TRIGGER AS $$
DECLARE
entity_id int;
label text;
BEGIN
IF NEW.fk_project != 0 OR OLD.fk_project !=0 THEN

IF tg_op = 'UPDATE' AND OLD.entity_label != NEW.entity_label THEN
SELECT pgwar.get_most_frequent_entity_label(NEW.pk_entity) INTO label;
PERFORM
pgwar.upsert_community_entity_preview((
NEW.pk_entity,
0,
NEW.fk_class,
NEW.entity_type,
NEW.class_label,
label,
NEW.full_text,
NEW.ts_vector,
NEW.type_label,
NEW.fk_type,
NEW.time_span,
NEW.first_second,
NEW.last_second,
NEW.tmsp_last_modification)::pgwar.entity_preview
);
ELSEIF tg_op = 'INSERT' THEN
SELECT pgwar.get_most_frequent_entity_label(NEW.pk_entity) INTO label;
PERFORM
pgwar.upsert_community_entity_preview((
NEW.pk_entity,
0,
NEW.fk_class,
NEW.entity_type,
NEW.class_label,
label,
NEW.full_text,
NEW.ts_vector,
NEW.type_label,
NEW.fk_type,
NEW.time_span,
NEW.first_second,
NEW.last_second,
NEW.tmsp_last_modification)::pgwar.entity_preview
);
ELSE
SELECT pgwar.get_most_frequent_entity_label(OLD.pk_entity) INTO label;
UPDATE pgwar.entity_preview
SET entity_label = label
WHERE fk_project = 0 AND pk_entity = OLD.pk_entity;
END IF;
END IF;
entity_id := COALESCE(NEW.pk_entity, OLD.pk_entity);

PERFORM pgwar.get_and_update_community_entity_label(entity_id);

RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER on_modify_project_entity_preview
AFTER INSERT OR UPDATE OR DELETE ON pgwar.entity_preview
FOR EACH ROW
CREATE TRIGGER after_upsert_entity_preview_entity_label_02
AFTER INSERT OR UPDATE OF entity_label ON pgwar.entity_preview
FOR EACH ROW
WHEN (NEW.fk_project IS DISTINCT FROM 0)
EXECUTE FUNCTION pgwar.update_community_entity_label_on_project_entity_label_change();

CREATE TRIGGER after_delete_entity_preview_02
AFTER DELETE ON pgwar.entity_preview
FOR EACH ROW
WHEN (OLD.fk_project IS DISTINCT FROM 0)
EXECUTE FUNCTION pgwar.update_community_entity_label_on_project_entity_label_change();
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ VALUES (
);

-- Add entity 1
INSERT INTO information.resource (fk_class, notes)
VALUES (77, '_1') RETURNING pk_entity;
INSERT INTO information.resource (fk_class, community_visibility, notes)
VALUES (77, '{"toolbox":true}', '_1') RETURNING pk_entity;

INSERT INTO projects.info_proj_rel (fk_entity, fk_project, is_in_project)
SELECT pk_entity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ SELECT is(
);

SELECT * FROM finish();
--ROLLBACK;
ROLLBACK;
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ VALUES IN (1);
INSERT INTO pgwar.entity_preview (pk_entity, fk_project, fk_class, entity_label)
VALUES (31, 1, 88, 'Entity 31');

SELECT pgwar.update_project_entity_label(31, 1, 'Foo');
SELECT pgwar.update_entity_label_of_entity_preview(31, 1, 'Foo');

-- Test 2: Check if function update entity label
SELECT is(
entity_label,
'Foo',
'update_project_entity_label updates entity label'
'update_entity_label_of_entity_preview updates entity label'
)
FROM pgwar.entity_preview
WHERE pk_entity = 31
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "geovistory-root",
"description": "root package.json of the monorepo",
"version": "0.7.1-pr-359.4",
"version": "0.7.1-pr-363.2",
"scripts": {},
"private": true
}

0 comments on commit b37792c

Please sign in to comment.