/* Updating a datapoint name also updates the jsonb array entry */
CREATE OR REPLACE FUNCTION update_triggers_datapoint_name()
	RETURNS TRIGGER
	LANGUAGE PLPGSQL
	AS
$$
BEGIN
UPDATE "trigger"
	SET
		datapoints = (datapoints - OLD.name) || jsonb_build_array(NEW.name)
	WHERE
		datapoints ? OLD.name;

	RETURN NEW;
END;
$$;

CREATE TRIGGER datapoint_renamed
	AFTER UPDATE
	ON public.datapoint
	FOR EACH ROW
	EXECUTE PROCEDURE update_triggers_datapoint_name();
