For the sake of the example, consider a table
create table foo (
contents text NOT NULL,
is_active boolean NOT NULL DEFAULT false,
dt_active date
)
I insert a record:
insert into foo (contents) values ('bar')
So far, so good. Later on, I now want to 'activate' the record:
update foo set is_active = true
What I would like to do when is_active
is changed from false
to true
, is for dt_active
is set to now()
. For bonus points it would be nice if is_active
is changed from true
to fals
e, dt_active is set to null, but I can live without that.
I'd really like to push this housekeeping into the database, it would make the client code much cleaner (since many tables (and even column tuples within tables) could benefit from this technique).
I'm stumped as to how to pull out the current record in the database in the trigger (I'm using plpgsql), in order to compare the "then" with the "now". Pointers to code examples or snippets greatly appreciated.