Skip to content
Snippets Groups Projects
Select Git revision
  • bcda4b1d219a8fe8c68c9f3d058b1409c5608376
  • master default protected
  • L2SS-1914-fix_job_dispatch
  • TMSS-3170
  • TMSS-3167
  • TMSS-3161
  • TMSS-3158-Front-End-Only-Allow-Changing-Again
  • TMSS-3133
  • TMSS-3319-Fix-Templates
  • test-fix-deploy
  • TMSS-3134
  • TMSS-2872
  • defer-state
  • add-custom-monitoring-points
  • TMSS-3101-Front-End-Only
  • TMSS-984-choices
  • SDC-1400-Front-End-Only
  • TMSS-3079-PII
  • TMSS-2936
  • check-for-max-244-subbands
  • TMSS-2927---Front-End-Only-PXII
  • Before-Remove-TMSS
  • LOFAR-Release-4_4_318 protected
  • LOFAR-Release-4_4_317 protected
  • LOFAR-Release-4_4_316 protected
  • LOFAR-Release-4_4_315 protected
  • LOFAR-Release-4_4_314 protected
  • LOFAR-Release-4_4_313 protected
  • LOFAR-Release-4_4_312 protected
  • LOFAR-Release-4_4_311 protected
  • LOFAR-Release-4_4_310 protected
  • LOFAR-Release-4_4_309 protected
  • LOFAR-Release-4_4_308 protected
  • LOFAR-Release-4_4_307 protected
  • LOFAR-Release-4_4_306 protected
  • LOFAR-Release-4_4_304 protected
  • LOFAR-Release-4_4_303 protected
  • LOFAR-Release-4_4_302 protected
  • LOFAR-Release-4_4_301 protected
  • LOFAR-Release-4_4_300 protected
  • LOFAR-Release-4_4_299 protected
41 results

add_functions_and_triggers.sql

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    add_functions_and_triggers.sql 39.33 KiB
    --add triggers and trigger functions to radb (note, there are also the notification triggers in the add_notifications.sql file)
    
    BEGIN;
    
    -- only issue >warnings log messages. (only during this transaction)
    SET LOCAL client_min_messages=warning;
    
    CREATE OR REPLACE FUNCTION resource_allocation.on_task_updated()
      RETURNS trigger AS
    $BODY$
    DECLARE
        claim_tentative_status_id int := 0; --beware: hard coded instead of lookup for performance
        claim_claimed_status_id int := 1; --beware: hard coded instead of lookup for performance
        task_approved_status_id int := 300; --beware: hard coded instead of lookup for performance
    BEGIN
      IF NEW.status_id <> OLD.status_id THEN
        IF NEW.status_id = task_approved_status_id THEN
            UPDATE resource_allocation.resource_claim rc SET status_id=claim_tentative_status_id WHERE rc.task_id=NEW.id AND rc.status_id <> claim_tentative_status_id;
        ELSIF NEW.status_id = ANY(ARRAY[400, 500, 600, 900, 1000, 1100]) THEN
            --prevent task status to be upgraded to scheduled (or beyond) when not all its claims are claimed
            IF EXISTS (SELECT id FROM resource_allocation.resource_claim WHERE task_id = NEW.id AND status_id <> claim_claimed_status_id) THEN
                RAISE EXCEPTION 'Cannot update task status from % to % when not all its claims are claimed', OLD.status_id, NEW.status_id;
            END IF;
        END IF;
    END IF;
    RETURN NEW;
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;
    ALTER FUNCTION resource_allocation.on_task_updated()
      OWNER TO resourceassignment;
    COMMENT ON FUNCTION resource_allocation.on_task_updated()
      IS 'function which is called by task table update trigger, which updates all the tasks resource claims to tentative state.';
    
    DROP TRIGGER IF EXISTS T_on_task_updated ON resource_allocation.task CASCADE;
    CREATE TRIGGER T_on_task_updated
      AFTER UPDATE
      ON resource_allocation.task
      FOR EACH ROW
      EXECUTE PROCEDURE resource_allocation.on_task_updated();
    COMMENT ON TRIGGER T_on_task_updated ON resource_allocation.task
      IS 'task table update trigger, calls the resource_allocation.on_task_updated() function.';
    
    ---------------------------------------------------------------------------------------------------------------------
    
    CREATE OR REPLACE FUNCTION resource_allocation.delete_conflict_reasons_after_resource_claim_update()
      RETURNS trigger AS
    $BODY$
    BEGIN
      IF OLD.status_id = 2 AND NEW.status_id <> 2 THEN   --new status is not conflict
        DELETE FROM resource_allocation.resource_claim_conflict_reason rccr WHERE rccr.resource_claim_id = NEW.id;
      END IF;
    RETURN NEW;
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;
    ALTER FUNCTION resource_allocation.delete_conflict_reasons_after_resource_claim_update()
      OWNER TO resourceassignment;
    COMMENT ON FUNCTION resource_allocation.delete_conflict_reasons_after_resource_claim_update()
      IS 'function which is called by resource_claim table update trigger, which deletes resource_claim_conflict_reasons when the claim status is updated to !conflict.';
    
    DROP TRIGGER IF EXISTS T_delete_conflict_reasons_after_resource_claim_update ON resource_allocation.resource_claim CASCADE;
    CREATE TRIGGER T_delete_conflict_reasons_after_resource_claim_update
      AFTER UPDATE
      ON resource_allocation.resource_claim
      FOR EACH ROW
      EXECUTE PROCEDURE resource_allocation.delete_conflict_reasons_after_resource_claim_update();