diff --git a/symmetric-assemble/src/docbook/configuration.xml b/symmetric-assemble/src/docbook/configuration.xml index 8b807af97c..96755e5939 100644 --- a/symmetric-assemble/src/docbook/configuration.xml +++ b/symmetric-assemble/src/docbook/configuration.xml @@ -980,7 +980,7 @@ values as the original table with the same data types only each column is nullable with no default values. - Three extra "AUDIT" columns are added: + Three extra "AUDIT" columns are added to the table: AUDIT_ID - the primary key of the table. AUDIT_TIME - the time at which the change occurred. AUDIT_EVENT - the DML type that happened to the row. diff --git a/symmetric-core/src/main/java/org/jumpmind/symmetric/route/AuditTableDataRouter.java b/symmetric-core/src/main/java/org/jumpmind/symmetric/route/AuditTableDataRouter.java index b55062c0d3..3282ad3673 100644 --- a/symmetric-core/src/main/java/org/jumpmind/symmetric/route/AuditTableDataRouter.java +++ b/symmetric-core/src/main/java/org/jumpmind/symmetric/route/AuditTableDataRouter.java @@ -14,6 +14,7 @@ import org.jumpmind.db.sql.ISqlTemplate; import org.jumpmind.symmetric.ISymmetricEngine; import org.jumpmind.symmetric.common.ParameterConstants; +import org.jumpmind.symmetric.io.data.DataEventType; import org.jumpmind.symmetric.model.DataMetaData; import org.jumpmind.symmetric.model.Node; import org.jumpmind.symmetric.model.TriggerHistory; @@ -37,44 +38,47 @@ public AuditTableDataRouter(ISymmetricEngine engine) { public Set routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set nodes, boolean initialLoad) { - IParameterService parameterService = engine.getParameterService(); - IDatabasePlatform platform = engine.getDatabasePlatform(); - TriggerHistory triggerHistory = dataMetaData.getTriggerHistory(); - Table table = dataMetaData.getTable().copyAndFilterColumns( - triggerHistory.getParsedColumnNames(), triggerHistory.getParsedPkColumnNames(), - true); - String tableName = table.getFullyQualifiedTableName(); - Table auditTable = auditTables.get(tableName); - if (auditTable == null) { - auditTable = toAuditTable(table); - auditTables.put(tableName, auditTable); - if (parameterService.is(ParameterConstants.AUTO_CONFIGURE_DATABASE)) { - platform.alterTables(true, auditTable); + DataEventType eventType = dataMetaData.getData().getDataEventType(); + if (eventType == DataEventType.INSERT || eventType == DataEventType.UPDATE + || eventType == DataEventType.DELETE) { + IParameterService parameterService = engine.getParameterService(); + IDatabasePlatform platform = engine.getDatabasePlatform(); + TriggerHistory triggerHistory = dataMetaData.getTriggerHistory(); + Table table = dataMetaData.getTable().copyAndFilterColumns( + triggerHistory.getParsedColumnNames(), triggerHistory.getParsedPkColumnNames(), + true); + String tableName = table.getFullyQualifiedTableName(); + Table auditTable = auditTables.get(tableName); + if (auditTable == null) { + auditTable = toAuditTable(table); + auditTables.put(tableName, auditTable); + if (parameterService.is(ParameterConstants.AUTO_CONFIGURE_DATABASE)) { + platform.alterTables(true, auditTable); + } } - } - String auditTableName = auditTable.getFullyQualifiedTableName(platform.getDatabaseInfo() - .getDelimiterToken()); + String auditTableName = auditTable.getFullyQualifiedTableName(platform + .getDatabaseInfo().getDelimiterToken()); - ISqlTemplate template = platform.getSqlTemplate(); - Map values = new HashMap(getNewDataAsObject(null, - dataMetaData, engine.getSymmetricDialect())); - Long sequence = (Long)context.get(auditTableName); - if (sequence == null) { - sequence = 1l + template.queryForLong(String.format("select max(%s) from %s", COLUMN_AUDIT_ID, - auditTableName)); - } else { - sequence = 1l + sequence; + ISqlTemplate template = platform.getSqlTemplate(); + Map values = new HashMap(getNewDataAsObject(null, + dataMetaData, engine.getSymmetricDialect())); + Long sequence = (Long) context.get(auditTableName); + if (sequence == null) { + sequence = 1l + template.queryForLong(String.format("select max(%s) from %s", + COLUMN_AUDIT_ID, auditTableName)); + } else { + sequence = 1l + sequence; + } + context.put(auditTableName, sequence); + values.put(COLUMN_AUDIT_ID, sequence); + values.put(COLUMN_AUDIT_TIME, new Date()); + values.put(COLUMN_AUDIT_EVENT, dataMetaData.getData().getDataEventType().getCode()); + DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, auditTable); + int[] types = statement.getTypes(); + Object[] args = statement.getValueArray(values); + String sql = statement.getSql(); + template.update(sql, args, types); } - context.put(auditTableName, sequence); - values.put(COLUMN_AUDIT_ID, - sequence); - values.put(COLUMN_AUDIT_TIME, new Date()); - values.put(COLUMN_AUDIT_EVENT, dataMetaData.getData().getDataEventType().getCode()); - DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, auditTable); - int[] types = statement.getTypes(); - Object[] args = statement.getValueArray(values); - String sql = statement.getSql(); - template.update(sql, args, types); return null; } diff --git a/symmetric-core/src/main/resources/symmetric-schema.xml b/symmetric-core/src/main/resources/symmetric-schema.xml index 24db608d20..bca25de89b 100644 --- a/symmetric-core/src/main/resources/symmetric-schema.xml +++ b/symmetric-core/src/main/resources/symmetric-schema.xml @@ -49,7 +49,7 @@ - +