tcdi / plrust

A Rust procedural language handler for PostgreSQL
PostgreSQL License
1.1k stars 32 forks source link

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

Open hohmannr opened 5 months ago

hohmannr commented 5 months ago

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