forked from StateVoicesNational/Spoke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from MoveOnOrg/updatedat
Update campaign_contact.updated_at on change
- Loading branch information
Showing
2 changed files
with
43 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,35 @@ | ||
|
||
const { onUpdateTrigger } = require('./helpers/index') | ||
const ON_UPDATE_TIMESTAMP_FUNCTION = ` | ||
CREATE OR REPLACE FUNCTION on_update_timestamp() | ||
RETURNS trigger AS $$ | ||
BEGIN | ||
NEW.updated_at = now(); | ||
RETURN NEW; | ||
END; | ||
$$ language 'plpgsql'; | ||
` | ||
|
||
const DROP_ON_UPDATE_TIMESTAMP_FUNCTION = `DROP FUNCTION on_update_timestamp` | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
*/ | ||
exports.up = async function(knex) { | ||
const isSqlite = /sqlite/.test(knex.client.config.client); | ||
if (!isSqlite) { | ||
await knex.raw(ON_UPDATE_TIMESTAMP_FUNCTION); | ||
await knex.raw(onUpdateTrigger('campaign_contact')); | ||
} | ||
}; | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
*/ | ||
exports.down = async function(knex) { | ||
const isSqlite = /sqlite/.test(knex.client.config.client); | ||
if (!isSqlite) { | ||
await knex.raw("DROP TRIGGER campaign_contact_updated_at on campaign_contact"); | ||
await knex.raw(DROP_ON_UPDATE_TIMESTAMP_FUNCTION); | ||
} | ||
}; |
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