Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Commit

Permalink
[#467] Update updated_at automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
georgiana-b authored and nightsh committed Mar 17, 2017
1 parent 44bad84 commit 795cfb0
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions migrations/20170123121324_add_trigger_for_updated_at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const Promise = require('bluebird');

const tablesWithUpdatedAt = ['users', 'data_contributions'];

exports.up = (knex) => (
knex.raw(
`CREATE FUNCTION set_updated_at()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at := now();
RETURN NEW;
END;
$$;`
).then(() =>
Promise.map(tablesWithUpdatedAt, (table) =>
knex.raw(
`CREATE TRIGGER ${table}_set_updated_at
BEFORE UPDATE ON ${table}
FOR EACH ROW EXECUTE PROCEDURE set_updated_at();`
)
)
)
);

exports.down = (knex) => (
Promise.map(tablesWithUpdatedAt, (table) =>
knex.raw(`DROP TRIGGER ${table}_set_updated_at ON ${table};`)
).then(() =>
knex.raw('DROP FUNCTION set_updated_at();')
)
);

0 comments on commit 795cfb0

Please sign in to comment.