54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Migration helper methods
|
|
* @type {Object}
|
|
*/
|
|
module.exports = {
|
|
|
|
getDbType(knex) {
|
|
switch (knex.client.config.client) {
|
|
case 'postgresql':
|
|
case 'postgres':
|
|
case 'pg':
|
|
return 'pg';
|
|
|
|
default:
|
|
return knex.client.config.client;
|
|
}
|
|
},
|
|
|
|
pgTimestampUpdateFunction() {
|
|
return `CREATE OR REPLACE FUNCTION progblog_update_modified_column()
|
|
RETURNS TRIGGER AS $$
|
|
BEGIN
|
|
NEW.updated_at = now();
|
|
RETURN NEW;
|
|
END;
|
|
$$ language 'plpgsql';`;
|
|
},
|
|
|
|
pgCreateTimestampUpdateTrigger(table) {
|
|
return `CREATE TRIGGER update_${table}_modtime
|
|
BEFORE UPDATE ON "${table}"
|
|
FOR EACH ROW
|
|
EXECUTE PROCEDURE progblog_update_modified_column();`;
|
|
},
|
|
|
|
pgDeleteTimestampUpdateTrigger(table) {
|
|
return `DROP TRIGGER IF EXISTS update_${table}_modtime ON "${table}";`;
|
|
},
|
|
|
|
sqlite3CreateTimestampUpdateTrigger(table) {
|
|
return `CREATE TRIGGER IF NOT EXISTS [UpdateModified]
|
|
BEFORE UPDATE ON "${table}"
|
|
FOR EACH ROW
|
|
BEGIN
|
|
UPDATE "${table}" SET updated_at = CURRENT_TIMESTAMP WHERE id = old.id;
|
|
END`;
|
|
},
|
|
|
|
sqlite3DeleteTimestampUpdateTrigger() {
|
|
return `DROP TRIGGER IF EXISTS [UpdateModified]`;
|
|
},
|
|
}; |