qwat / QWAT

TEKSI Water module (project QWAT) - QGIS project
https://www.teksi.ch
GNU General Public License v2.0
58 stars 31 forks source link

_valve_count not updated with valve insert #244

Closed varrieta closed 6 years ago

varrieta commented 6 years ago

The pipe attribute __valvecount doesn't get updated when a new valve is inserted but does when a valve is updated (done by tr_valve_update_trigger).

haubourg commented 6 years ago

Confirmed here. Moving a valve and updating a pipe geometry didn't update the count value here. IMO, maintaining all those computed fields is a pain, we should move pipes to a view with calculated fields, following thick database good practices

3nids commented 6 years ago

There would be an issue on delete too I guess.

Here is an untested delta.

DROP TRIGGER tr_valve_update_trigger ON qwat_od.valve;
DROP FUNCTION qwat_od.ft_valve_update();

CREATE TRIGGER tr_valve_trigger
    AFTER INSERT OR UPDATE OR DELETE 
    ON qwat_od.valve
    FOR EACH ROW
    EXECUTE PROCEDURE qwat_od.ft_valve();

COMMENT ON TRIGGER tr_valve_trigger ON qwat_od.valve
    IS 'Trigger: when updating a valve, reevaluate old and new pipes for number of valves on them.';

CREATE FUNCTION qwat_od.ft_valve()
    RETURNS trigger
AS $BODY$
    BEGIN
                IF TG_OP <> 'INSERT' THEN
              PERFORM qwat_od.fn_pipe_update_valve(OLD.id);
                END IF;
                IF TG_OP <> 'DELETE' THEN
              PERFORM qwat_od.fn_pipe_update_valve(NEW.id);
                END IF;
        RETURN NEW;
    END;
$BODY$;

COMMENT ON FUNCTION qwat_od.ft_valve()
    IS 'Trigger: when updating a valve, reevaluate old and new pipes for number of valves on them.';

I think it would be great to add a test case for this. Let me know if you want me to do it and provide a proper delta.

haubourg commented 6 years ago

ping @lbartoletti can you update the status of the issue?