# pkgforge Model

## Schema public

standard public schema

### Table: build_log

Structure of build_log

id

  serial `PRIMARY KEY`

task

  integer `REFERENCES`

builder

  integer `REFERENCES`

modtime

  timestamp with time zone `NOT NULL` `DEFAULT now()`

### Table: builder

Structure of builder

id

  serial `PRIMARY KEY`

name

  character varying(50) `UNIQUE` `NOT NULL`

platform

  integer `NOT NULL` `REFERENCES`

current

  integer `UNIQUE` `REFERENCES`

modtime

  timestamp with time zone `NOT NULL` `DEFAULT now()`

Constraints on builder

task_platform

  CHECK (check_task(current, platform))

Tables referencing build_log via Foreign Key Constraints

### Table: job

Structure of job

id

  serial `PRIMARY KEY`

uuid

  character varying(50) `UNIQUE` `NOT NULL`

submitter

  character varying(50)

status

  integer `NOT NULL` `REFERENCES`

size

  integer

modtime

  timestamp with time zone `NOT NULL` `DEFAULT now()`

Tables referencing task via Foreign Key Constraints

### Table: job_status

Structure of job_status

id

  serial `PRIMARY KEY`

name

  character varying(20) `UNIQUE` `NOT NULL`

Tables referencing job via Foreign Key Constraints

### Table: platform

Structure of platform

id

  serial `PRIMARY KEY`

name

  character varying(10) `UNIQUE#1` `NOT NULL`

arch

  character varying(10) `UNIQUE#1` `NOT NULL`

active

  boolean `NOT NULL` `DEFAULT false`

Tables referencing builder via Foreign Key Constraints

### Table: task

Structure of task

id

  serial `PRIMARY KEY`

job

  integer `UNIQUE#1` `NOT NULL` `REFERENCES`

platform

  integer `UNIQUE#1` `NOT NULL` `REFERENCES`

status

  integer `NOT NULL` `REFERENCES`

modtime

  timestamp with time zone `NOT NULL` `DEFAULT now()`

Tables referencing build_log via Foreign Key Constraints

### Table: task_status

Structure of task_status

id

  serial `PRIMARY KEY`

name

  character varying(20) `UNIQUE` `NOT NULL`

Tables referencing task via Foreign Key Constraints

### check_task(p integer, t integer)

check_task(p integer, t integer)

Function Properties

Language Return Type
- PLPGSQL boolean

    DECLARE correct BOOLEAN;
    BEGIN
            SELECT  (platform = $2) INTO correct
            FROM    task
            WHERE   id = $1;

            RETURN correct;
    END;

### update_job_status()

update_job_status()

Function Properties

Language Return Type
- PLPGSQL trigger

    DECLARE
       total_count      INTEGER := 0;
       fail_count       INTEGER := 0;
       success_count    INTEGER := 0;
       cancel_count     INTEGER := 0;
       task_id          INTEGER := NEW.id;
       job_id           INTEGER;
       job_status_name  VARCHAR(20) := NULL;
    BEGIN

       SELECT job INTO job_id
           FROM task
           WHERE id = task_id
           LIMIT 1;

       IF NOT FOUND THEN
         RAISE NOTICE 'Could not find a job for task %', task_id;
         RETURN NEW;
       END IF;

       SELECT COUNT(t.id) INTO total_count
           FROM task AS t
           WHERE t.job = job_id;

       SELECT COUNT(t.id) INTO fail_count
           FROM task AS t
           JOIN task_status AS s ON t.status = s.id
           WHERE t.job = job_id AND s.name = 'fail';

       SELECT COUNT(t.id) INTO success_count
           FROM task AS t
           JOIN task_status AS s ON t.status = s.id
           WHERE t.job = job_id AND s.name = 'success';

       SELECT COUNT(t.id) INTO cancel_count
           FROM task AS t
           JOIN task_status AS s ON t.status = s.id
           WHERE t.job = job_id AND s.name = 'cancelled';

       IF total_count > 0 THEN

         IF fail_count > 0 THEN

           IF fail_count = total_count THEN
             job_status_name := 'fail';
           ELSE
             job_status_name := 'partial fail';
           END IF;

         ELSIF success_count > 0 THEN

           IF success_count = total_count THEN
             job_status_name := 'success';
           ELSE
             job_status_name := 'partial success';
           END IF;

         ELSIF cancel_count = total_count THEN
             job_status_name := 'cancelled';
         END IF;

         IF job_status_name IS NOT NULL THEN

           UPDATE job SET status =
             ( SELECT id FROM job_status WHERE name = job_status_name LIMIT 1)
             WHERE id = job_id;

         END IF;

       END IF;

       RETURN NEW;
    END;

### update_modification_time()

update_modification_time()

Function Properties

Language Return Type
- PLPGSQL trigger

    BEGIN
       NEW.modtime = now();
       RETURN NEW;
    END;