This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#467] Update updated_at automatically
- Loading branch information
1 parent
44bad84
commit 795cfb0
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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();') | ||
) | ||
); | ||
|