cakephp / cakephp/phinx

Automatically add update triggers for postgres updated_at columns

Open
#2,259 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
PHP
Stars
4.5k
Forks
884
PR merge metrics
No merged PRs in 30d

Description

This may be a bit complex, but there is a "standardized" way of handling an updated_at column in postgres.

The idea is you create a function like this:

          CREATE FUNCTION update_updated_at_column() RETURNS trigger
            LANGUAGE plpgsql
            AS $$
          BEGIN
            NEW.updated_at = NOW();
            RETURN NEW;
          END;

This only has to be done once per database.

And then attach it to the column when it's created like this:

CREATE TRIGGER sometable_update_trigger BEFORE UPDATE ON sometable FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();

I'm doing this manually in migrations right now but it would be really cool if Phinx could do this automatically as part of the addTimestamps and addTimestampsWithTimezone calls.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the addTimestamps and addTimestampsWithTimezone entry points and review how PostgreSQL migration statements are generated. Compare the requested function and trigger behavior with existing migration patterns; done means those calls can establish updated_at triggers without manual migration SQL.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, postgresql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.