- Newest
- Most votes
- Most comments
Hi Paul,
Using AWS Database Migration Service (DMS), with Postgres as source and S3 as target, you can use the add-before-image-columns transformation rule to add the before image of the primary key columns to the S3 output file.
Consider the below table definition:
create table order_details (
order_id int,
product_id int,
qty int,
primary key(order_id, product_id)
);
In the DMS task, we can use the below table mapping rule:
"rules": [
{
"rule-type": "selection",
"rule-id": "1",
"rule-name": "1",
"object-locator": {
"schema-name": "public",
"table-name": "order_details"
},
"rule-action": "include"
},
{
"rule-type": "transformation",
"rule-id": "2",
"rule-name": "2",
"rule-target": "column",
"object-locator": {
"schema-name": "public",
"table-name": "order_details"
},
"rule-action": "add-before-image-columns",
"before-image-def": {
"column-prefix": "BI_",
"column-suffix": "",
"column-filter": "pk-only"
}
}
]
}
Once the task is running, perform the below changes on the source database:
INSERT INTO order_details VALUES(30004,2,12); UPDATE order_details SET product_id = 66 WHERE order_id = 30004 AND product_id = 1; INSERT INTO order_details VALUES(30004,3,13);
The csv file output is as follows:
I,30004,1,11,, I,30004,2,12,, U,30004,66,11,30004,1 I,30004,3,13,,
In the above output, we can see the before image columns named BI_order_id and BI_product_id added to the output. They contain the original primary key values before it was updated.
The add-before-image-columns transformation rule also works for single column primary keys.
The important elements in the table mappings are:
-
"rule-action": "add-before-image-columns" This attribute specifies that we are adding before image columns
-
before-image-def - This parameter defines a naming convention to identify the before-image columns and specifies a filter to identify which source columns can have before-image columns created for them on the target. Example:
"column-prefix": "BI_",
"column-suffix": "",
"column-filter": "pk-only"
For more details, more options for before-image-def and sample transformation rules, please refer to the below document: [-] Transformation rules and actions - Values - https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Tasks.CustomizingTasks.TableMapping.SelectionTransformation.Transformations.html#CHAP_Tasks.CustomizingTasks.TableMapping.SelectionTransformation.Transformations.Values
Relevant content
- asked 3 years ago

Hey thank you for this info! I've confirmed how it all works and updated my article! https://tempered.works/posts/2024/06/12/breaking-change-data-capture-with-primary-keys/#solutions