Skip to content

Commit

Permalink
0003061: Sync triggers will not create trigger if function already
Browse files Browse the repository at this point in the history
exists (Postgres)
  • Loading branch information
mmichalek committed Apr 19, 2017
1 parent 1c2bf42 commit c09dc22
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
Expand Up @@ -154,10 +154,7 @@ public boolean requiresAutoCommitFalseToSetFetchSize() {
protected boolean doesTriggerExistOnPlatform(String catalogName, String schema, String tableName, String triggerName) {
return platform.getSqlTemplate().queryForInt("select count(*) from information_schema.triggers where trigger_name = ? "
+ "and event_object_table = ? and trigger_schema = ?", new Object[] { triggerName.toLowerCase(),
tableName, schema == null ? platform.getDefaultSchema() : schema }) > 0 ||
platform.getSqlTemplate().queryForInt("select count(*) from information_schema.routines where routine_name = ? "
+ "and routine_schema = ?", new Object[] { "f" + triggerName.toLowerCase(),
schema == null ? platform.getDefaultSchema() : schema }) > 0;
tableName, schema == null ? platform.getDefaultSchema() : schema }) > 0;
}

@Override
Expand All @@ -168,27 +165,22 @@ public void removeTrigger(StringBuilder sqlBuffer, String catalogName, String sc
String quoteChar = platform.getDatabaseInfo().getDelimiterToken();
schemaName = table.getSchema() == null ? "" : (quoteChar + table.getSchema()
+ quoteChar + ".");
final String dropSql = "drop trigger " + triggerName + " on " + schemaName + quoteChar
final String dropSql = "drop trigger IF EXISTS " + triggerName + " on " + schemaName + quoteChar
+ table.getName() + quoteChar;
logSql(dropSql, sqlBuffer);
final String dropFunction = "drop function " + schemaName + "f" + triggerName
final String dropFunction = "drop function IF EXISTS " + schemaName + "f" + triggerName
+ "() cascade";
logSql(dropFunction, sqlBuffer);
if (parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
String sql = null;
try {
sql = dropSql;
platform.getSqlTemplate().update(dropSql);
} catch (Exception e) {
log.warn("Tried to remove trigger using: {} and failed because: {}", sql,
e.getMessage());
log.warn("Failed to remove trigger using: " + dropSql, e);
}
try {
sql = dropFunction;
platform.getSqlTemplate().update(dropFunction);
} catch (Exception e) {
log.warn("Tried to remove function using: {} and failed because: {}", sql,
e.getMessage());
log.warn("Failed to remove function using: " + dropFunction, e);
}
}
}
Expand Down
Expand Up @@ -49,6 +49,7 @@
import org.jumpmind.db.sql.ISqlRowMapper;
import org.jumpmind.db.sql.Row;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.SymmetricException;
import org.jumpmind.symmetric.Version;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.ParameterConstants;
Expand Down Expand Up @@ -1488,6 +1489,10 @@ private boolean containsExactMatchForSourceTableName(Table table, List<Trigger>

public void syncTriggers(Table table, boolean force) {
boolean ignoreCase = this.parameterService.is(ParameterConstants.DB_METADATA_IGNORE_CASE);

if (table == null) {
throw new SymmetricException("'table' cannot be null, check that the table exists.");
}

/* Re-lookup just in case the table was just altered */
platform.resetCachedTableModel();
Expand Down

0 comments on commit c09dc22

Please sign in to comment.