plrust icon indicating copy to clipboard operation
plrust copied to clipboard

Postgres Triggers, achieve different behaviour on INSERT, UPDATE, etc.

Open hohmannr opened this issue 3 months ago • 0 comments

In the current plrust docs, I have found the following example for triggers:

    let tg_op = trigger.op()?;

    let my_row = match tg_op {
        INSERT => trigger.new().unwrap(),
        _ => trigger.old().unwrap()
    };

This does not get you different behaviour for different trigger ops, since the matchstatement's _ option is unreachable, because INSERT is treated as a variable, that catches tg_ops value. Looking into this, I have found the following to work:

    let tg_op = trigger.op()?;

    let my_row = match tg_op {
        PgTriggerOperation::Insert => trigger.new().unwrap(),
        PgTriggerOperation::Update=> trigger.old().unwrap(),
        _ => error!("unsupported trigger operation!"),
    };

My specs: Using plrust on AWS RDS: postgres v15.5

hohmannr avatar Apr 07 '24 13:04 hohmannr