Skip to content

Commit

Permalink
Merge pull request #3 from spencerswagger/feat/drop_create_trigger
Browse files Browse the repository at this point in the history
[Compatibility] Trigger driver CREATE OR REPLACE command
  • Loading branch information
alancolant authored Jul 15, 2024
2 parents 1071eb6 + 586ecd0 commit d3fe20e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions drivers/trigger/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (d *Driver) getCreateTriggerSqlForOperation(listenerUid string, l *flash.Li
}

if rawConditionSql == "" {

statement = fmt.Sprintf(`
CREATE OR REPLACE FUNCTION "%s"."%s"() RETURNS trigger AS $trigger$
BEGIN
Expand All @@ -132,13 +133,17 @@ func (d *Driver) getCreateTriggerSqlForOperation(listenerUid string, l *flash.Li
}

if operation != "TRUNCATE" {
// Keep drop + create instead of 'create or replace' for Pgsql13 compatibility
statement += fmt.Sprintf(`
CREATE OR REPLACE TRIGGER "%s" AFTER %s ON %s FOR EACH ROW EXECUTE PROCEDURE "%s"."%s"();`,
triggerName, operation, d.sanitizeTableName(l.Table), d.Config.Schema, triggerFnName)
DROP TRIGGER IF EXISTS "%s" ON %s;
CREATE TRIGGER "%s" AFTER %s ON %s FOR EACH ROW EXECUTE PROCEDURE "%s"."%s"();`,
triggerName, d.sanitizeTableName(l.Table), triggerName, operation, d.sanitizeTableName(l.Table), d.Config.Schema, triggerFnName)
} else {
// Keep drop + create instead of 'create or replace' for Pgsql13 compatibility
statement += fmt.Sprintf(`
CREATE OR REPLACE TRIGGER "%s" BEFORE TRUNCATE ON %s FOR EACH STATEMENT EXECUTE PROCEDURE "%s"."%s"();`,
triggerName, d.sanitizeTableName(l.Table), d.Config.Schema, triggerFnName)
DROP TRIGGER IF EXISTS "%s" ON %s;
CREATE TRIGGER "%s" BEFORE TRUNCATE ON %s FOR EACH STATEMENT EXECUTE PROCEDURE "%s"."%s"();`,
triggerName, d.sanitizeTableName(l.Table), triggerName, d.sanitizeTableName(l.Table), d.Config.Schema, triggerFnName)
}

return statement, eventName, nil
Expand Down

0 comments on commit d3fe20e

Please sign in to comment.