From 5c8ee66f432585db7bd72103fa814558bf406bcc Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Thu, 17 Jun 2021 22:01:18 +0200 Subject: [PATCH] feat(PostgreSQL): alter trigger support --- src/common/customizations/defaults.js | 2 +- src/common/customizations/postgresql.js | 2 +- src/main/libs/clients/PostgreSQLClient.js | 30 +++++++++++------------ 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/common/customizations/defaults.js b/src/common/customizations/defaults.js index a67a0797..422fb46c 100644 --- a/src/common/customizations/defaults.js +++ b/src/common/customizations/defaults.js @@ -62,7 +62,7 @@ module.exports = { functionSql: false, functionContext: false, functionLanguage: false, - triggerMiltipleEvents: false, + triggerMultipleEvents: false, triggerTableInName: false, triggerUpdateColumns: false, triggerOnlyRename: false, diff --git a/src/common/customizations/postgresql.js b/src/common/customizations/postgresql.js index a4f0ff7e..605e4e47 100644 --- a/src/common/customizations/postgresql.js +++ b/src/common/customizations/postgresql.js @@ -40,6 +40,6 @@ module.exports = { functionLanguage: true, triggerMultipleEvents: true, triggerTableInName: true, - triggerOnlyRename: true, + triggerOnlyRename: false, languages: ['sql', 'plpgsql', 'c', 'internal'] }; diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index ebfbd50d..86e9cf10 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -535,7 +535,7 @@ export class PostgreSQLClient extends AntaresCore { */ async dropTrigger (params) { const triggerParts = params.trigger.split('.'); - const sql = `DROP TRIGGER ${triggerParts[1]} ON ${triggerParts[0]}`; + const sql = `DROP TRIGGER "${triggerParts[1]}" ON "${triggerParts[0]}"`; return await this.raw(sql); } @@ -547,21 +547,18 @@ export class PostgreSQLClient extends AntaresCore { */ async alterTrigger (params) { const { trigger } = params; - // const tempTrigger = Object.assign({}, trigger); - // tempTrigger.name = `Antares_${tempTrigger.name}_tmp`; + const tempTrigger = Object.assign({}, trigger); + tempTrigger.name = `Antares_${tempTrigger.name}_tmp`; - // try { - // await this.createTrigger(tempTrigger); - // await this.dropTrigger({ trigger: tempTrigger.name }); - // await this.dropTrigger({ trigger: trigger.oldName }); - // await this.createTrigger(trigger); - // } - // catch (err) { - // return Promise.reject(err); - // } - - const sql = `ALTER TRIGGER ${trigger.oldName} ON ${trigger.table} RENAME TO ${trigger.name}`; - return await this.raw(sql); + try { + await this.createTrigger(tempTrigger); + await this.dropTrigger({ trigger: `${tempTrigger.table}.${tempTrigger.name}` }); + await this.dropTrigger({ trigger: `${trigger.table}.${trigger.oldName}` }); + await this.createTrigger(trigger); + } + catch (err) { + return Promise.reject(err); + } } /** @@ -571,7 +568,8 @@ export class PostgreSQLClient extends AntaresCore { * @memberof PostgreSQLClient */ async createTrigger (trigger) { - const sql = `CREATE ${trigger.definer ? `DEFINER=${trigger.definer} ` : ''}TRIGGER \`${trigger.name}\` ${trigger.event} ${trigger.activation} ON \`${trigger.table}\` FOR EACH ROW ${trigger.sql}`; + const eventsString = Array.isArray(trigger.event) ? trigger.event.join(' OR ') : trigger.event; + const sql = `CREATE TRIGGER "${trigger.name}" ${trigger.activation} ${eventsString} ON "${trigger.table}" FOR EACH ROW ${trigger.sql}`; return await this.raw(sql, { split: false }); }