- Newest
- Most votes
- Most comments
To use AWS Database Migration Service (DMS) for creating an audit log where changes to the source table are only inserted as new rows in the target audit table, you can modify your table mapping rules to achieve this behavior. Here's how you can accomplish this:
-
In your table mapping rules, include the source table you want to audit.
-
Use a transformation rule to rename the target table, adding the "_aud" suffix as you've mentioned.
-
Add the three audit columns (operation, revision, update date) to the target table schema.
-
Instead of updating existing rows in the target audit table, you want to insert a new row for each change. To do this, you need to modify the primary key of the target table. Create a composite primary key that includes the original primary key column(s) plus a timestamp or sequential number column.
-
In your transformation rules, add a rule to generate a unique value for this new primary key column. You can use the 'datetime' function to create a timestamp, or use a sequence generator if your target database supports it.
-
Use the operation type flags ($operation, $action) in your column mappings to populate the 'operation' column.
By following these steps, each change in the source table (insert, update, or delete) will result in a new row being inserted into the audit table, rather than updating existing rows. This approach creates a complete history of changes over time.
Remember to ensure that your target audit table has sufficient storage capacity, as it will grow with each change recorded. You may also want to implement a retention policy or archiving strategy for older audit records if needed.
This configuration allows you to maintain a full audit trail of all changes to your source table, with each modification recorded as a new entry in the audit table, preserving the historical record of changes over time.
Sources
Using a PostgreSQL database as an AWS DMS source - AWS Database Migration Service
Step 1: Configure Your Oracle Source Database - Database Migration Guide
Relevant content
asked 3 years ago

Greetings, friends, thanks for the suggestion. Unfortunately, this is not working for me, I see updates in-place based upon old composite primary key.
create table if not exists schem.foo_aud ( one numeric(2) not null, two varchar(8) not null, three varchar(1) not null, four varchar(1) not null, -- ... aud_operation char, aud_rev serial, aud_change_date timestamp with time zone, primary key (one, two, three, four, aud_change_date) );
And here are the mapping rules (other steps left out for size:
{ "rules": [ { "rule-id": "4", "rule-type": "transformation", "rule-name": "AddChangeDate", "rule-target": "column", "object-locator": { "schema-name": "%", "table-name": "%" }, "rule-action": "add-column", "value": "aud_change_date", "expression": "datetime()", "data-type": { "type": "datetime", "precision": 6 } },
{ "rule-id": "5", "rule-type": "transformation", "rule-name": "CdcPkFOOAudit", "rule-target": "column", "object-locator": { "schema-name": postgresql_schema.schem.name, "table-name": "foo_aud" }, "rule-action": "define-primary-key", "primary-key-def": { "name": "new_composite_pk", "columns": [ "one", "two", "three", "four", "aud_change_date" ] } },
{ "rule-id": "7", "rule-type": "transformation", "rule-name": "addOperation", "rule-target": "column", "object-locator": { "schema-name": "%", "table-name": "%" }, "rule-action": "add-column", "value": "aud_operation", "expression": "operation_indicator('D', 'U', 'I')", "data-type": { "type": "string", "length": 1 } } ]}
This works but not every deploy. I have debugged this extensively and cannot reproduce why it sometimes works and sometimes does not. New rows are only created for fresh inserts, all updates go to same audit row.