CREATE TABLE func_new_task_status(
    change_id INTEGER NOT NULL,
    id INTEGER NOT NULL,
    project_id INTEGER,
    status VARCHAR(40) NOT NULL,
    rank INTEGER NOT NULL,
    def INTEGER,
    uuid VARCHAR(40)
);

CREATE TRIGGER
    func_new_task_status_bi_1
BEFORE INSERT ON
    func_new_task_status
FOR EACH ROW
BEGIN

    SELECT debug(
        NEW.change_id,
        NEW.id,
        NEW.project_id,
        NEW.status,
        NEW.rank,
        NEW.def,
        NEW.uuid
    );


    UPDATE
        changes
    SET
        yaml = yaml || (
            SELECT
                '-' || x'0A'
                || '  _delta: new_task_status' || x'0A'
                || '  def: ' || COALESCE(NEW.def,'~') || x'0A'
                || '  project_uuid: ' || p.uuid || x'0A'
                || '  rank: ' || NEW.rank || x'0A'
                || CASE WHEN
                    NEW.status = '-'
                THEN
                    '  status: ''-''' || x'0A'
                ELSE
                    '  status: ' || NEW.status || x'0A'
                END
            FROM
                topics p
            WHERE
                p.id = NEW.project_id
        )
    WHERE
        id = NEW.change_id
    ;

    INSERT INTO func_new_topic2(
        id,
        change_id,
        kind,
        uuid
    )
    VALUES(
        NEW.id,
        NEW.change_id,
        'task_status',
        NEW.uuid
    );
 

    INSERT INTO
        task_status(
            id,
            project_id,
            status,
            rank,
            def
        )
    VALUES(
        NEW.id,
        NEW.project_id,
        NEW.status,
        NEW.rank,
        NEW.def
    );

    INSERT INTO deltas(
        id,
        change_id,
        action
    )
    VALUES(
        nextval('deltas'),
        NEW.change_id,
        'new_task_status'
    );

    INSERT INTO
        task_status_deltas(
            id,
            change_id,
            task_status_id,
            new,
            status,
            rank,
            def
        )
    VALUES(
        currval('deltas'),
        NEW.change_id,
        NEW.id,
        1,
        NEW.status,
        NEW.rank,
        NEW.def
    );

    SELECT RAISE(IGNORE);
END;
