Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update campaign_contact.updated_at on change #13

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions migrations/20240503180901_campaigncontactsupdatedat.js
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);
}
};
8 changes: 8 additions & 0 deletions migrations/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ exports.redefineSqliteTable = async (knex, tableName, newTableFn) => {
await knex.schema.dropTable(tableName);
await knex.schema.createTable(tableName, newTableFn);
};


exports.onUpdateTrigger = table => `
CREATE TRIGGER ${table}_updated_at
BEFORE UPDATE ON ${table}
FOR EACH ROW
EXECUTE PROCEDURE on_update_timestamp();
`
Loading