> For the complete documentation index, see [llms.txt](https://docs.adiom.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/designing-and-testing.md).

# Designing and Testing Transforms

This page is a step-by-step process for building a transform config for a migration and proving that it is correct, first on sample documents, then on a full dataset, then on live changes. It works the same whether you write the config by hand or have an AI agent write it.

You do not need exact knowledge of the source data or a final destination schema to start. The process is built to find the surprises: legacy documents, fields that hold different types in different documents, and destination requirements that change.

| Step                                                    | Goal                                       | Tool                                                                                                           |
| ------------------------------------------------------- | ------------------------------------------ | -------------------------------------------------------------------------------------------------------------- |
| [1](#step-1-decide-whether-you-need-a-transform)        | Decide whether you need a transform        | [Checklist](https://docs.adiom.io/enterprise/running-dsynct/data-transformations.md#when-you-need-a-transform) |
| [2](#step-2-learn-what-the-source-emits)                | Learn what the source emits                | [Connector reference](https://docs.adiom.io/reference/connectors.md)                                           |
| [3](#step-3-collect-sample-documents)                   | Collect sample documents                   | `sample-ids`, Sync Tester                                                                                      |
| [4](#step-4-write-the-first-config)                     | Write the first config                     | [Templates](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/templates.md)                 |
| [5](#step-5-iterate-on-samples)                         | Iterate on samples                         | Transform Studio                                                                                               |
| [6](#step-6-copy-real-documents-end-to-end)             | Copy a few real documents end to end       | `testsync`, Sync Tester                                                                                        |
| [7](#step-7-test-cdc)                                   | Test CDC                                   | Transform Studio, a sync with `--skip-initial-sync`                                                            |
| [8](#step-8-run-the-full-dataset-in-a-test-environment) | Run the full dataset in a test environment | A full sync                                                                                                    |
| [9](#step-9-run-in-production)                          | Run in production                          | A full sync, verification                                                                                      |

Start the web-api once; steps 3 to 7 use it:

```
docker run -e DSYNCT_MODE=simple -p 8080:8080 markadiom/dsynct --host-port 0.0.0.0:8080 web-api --simple-only
```

Transform Studio and the Sync Tester are under **Tools** in the web UI. See [Web-API](https://docs.adiom.io/enterprise/running-dsynct/web-api.md#tools).

## What You Need

* Read access to the source, in a test environment and later in production.
* A destination you can write test data to and delete from.
* For each destination table or collection, a description of the shape you want. It does not have to be precise.

## Step 1: Decide Whether You Need a Transform

Go through [When You Need a Transform](https://docs.adiom.io/enterprise/running-dsynct/data-transformations.md#when-you-need-a-transform). Then read [What Transforms Cannot Do](https://docs.adiom.io/enterprise/running-dsynct/data-transformations.md#what-transforms-cannot-do) with your target shape in mind. If the target needs joins, aggregation, or an ID built from non-ID fields, settle that before writing any config.

## Step 2: Learn What the Source Emits

A transform works on what the connector emits, which is not always what you see in the database. Read the **Emitted Format** section of your source connector's [reference page](https://docs.adiom.io/reference/connectors.md). Note four things:

* The data type: JSON or BSON.
* The ID: which fields, in which order, and with which types.
* Fields the connector adds or removes.
* What CDC events look like, and whether deletes and inserts are reported as such.

## Step 3: Collect Sample Documents

Aim for about 20 documents per namespace. Three ways to get them:

* **From the source through Dsync.** For sources that support fetching by ID (MongoDB, Cosmos DB NoSQL), use the **Sample** menu in the Sync Tester, or the [`sample-ids`](https://docs.adiom.io/enterprise/running-dsynct/verification.md#sample-ids) command, to pick random IDs, then preview the documents in the Sync Tester. This shows documents exactly as the connector emits them.
* **From the database directly.** Export documents with the database's own tools. Then adjust them to the emitted format from step 2, for example by removing system fields.
* **By hand.** Write documents from what you know of the schema. Use this only to get started.

Random samples miss rare shapes. Add documents on purpose:

* The oldest and the newest documents in each namespace.
* Documents with optional fields missing, null or empty.
* For schemaless sources, one document for each type a field can hold (a number in one document, a string in another).
* Documents with the largest arrays and the deepest nesting.
* Every ID shape in use (for example ObjectIDs and strings in the same collection).

Keep the samples in files. They become your regression set in [Changing a Transform Later](#changing-a-transform-later).

## Step 4: Write the First Config

1. Start from the [template](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/templates.md#starting-points) for your source and destination. It handles the data type and the ID.
2. Add one mapping per source namespace, or a `defaultmapping` if all namespaces follow the same rule.
3. Add [field recipes](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/templates.md#field-recipes) for the differences between the source documents and the target shape.
4. Leave everything else out. Fields without a mapping are copied as they are.

Rules that prevent most first-time mistakes:

* The destination ID must be computed from the source `id` only, in both `mapid` and the `cel` entry for the ID field, with the same resulting type. See [CDC Events](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/data-types.md#cdc-events).
* Every field in `add` needs a `cel` entry.
* Numbers from a JSON source need `int(self)` or `double(self)` before arithmetic. See [Values as CEL Sees Them](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/data-types.md#values-as-cel-sees-them).
* Misspelled options are ignored without an error.

## Step 5: Iterate on Samples

Open **Tools > Transform Studio**, paste the config and one sample document, and run it. Nothing is written anywhere. The steps and field descriptions are in [Transform Studio](https://docs.adiom.io/enterprise/running-dsynct/web-api.md#transform-studio).

For each sample, check:

* The `Data` result has the fields, names and types you want. BSON output is shown as extended JSON, so `{"$numberLong": "7"}` and `{"$numberInt": "7"}` tell you the exact type.
* The ID in the `Update` and `Delete` results equals the ID field in the `Data` result, including its type.
* No mode shows an error.

Run every sample, not only the first one. A config that works for a typical document often fails on the legacy ones.

To run all samples in one go, or to let an AI agent do it, call the same function through the API. See [Transform API](https://docs.adiom.io/enterprise/running-dsynct/web-api/api-reference.md#transform).

## Step 6: Copy Real Documents End to End

Transform Studio shows what the transform produces. It does not show whether the destination accepts it, or what the destination stores. Copy a small set of real documents and read them back.

1. Pick the IDs: the sampled IDs from step 3 plus the edge cases.
2. Run **Test Sync** in the [Sync Tester](https://docs.adiom.io/enterprise/running-dsynct/web-api.md#sync-tester), or the [`testsync`](https://docs.adiom.io/enterprise/running-dsynct/running-dsynct.md#testsync) command, with the transform, into a test destination.
3. Read the documents from the destination database with its own client. Check the stored types there: dates as dates, integers as the intended width, decimals as decimals.
4. Compare the number of documents written with the number of IDs submitted.

{% hint style="warning" %}
Test Sync and `testsync` report how many documents were found on the source. They do not report documents that failed to transform or to write, and they finish successfully either way. Always confirm by counting or reading on the destination, and check the log for `filter-error` lines. See [Troubleshooting Transforms](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/troubleshooting.md).
{% endhint %}

The source must support fetching by ID. For other sources (SQL Batch, DynamoDB), skip to step 8 with a small test table.

## Step 7: Test CDC

CDC uses parts of the config that the initial sync does not: `mapid`, and the handling of deletes.

**First, in Transform Studio.** For each sample, look at the `Insert`, `Update` and `Delete` results. If your source sends partial updates, tick **Show partial update** as well.

**Then, against real databases.** Run a sync that skips the initial copy and only streams changes:

```bash
docker run -e 'DSYNCT_MODE=simple' \
-v "./transform.yaml:/transform.yaml" \
markadiom/dsynct sync \
--skip-initial-sync \
--namespace <SOURCE_NAMESPACE> \
--transform \
<SOURCE> <DESTINATION> dsync-transform://transform.yaml
```

With Temporal, pass `--skip-initial-sync` to `dsynct run` instead.

While it runs, make these changes on the source and check the destination after each one:

| Change on the source                                                       | Expected on the destination                                                                      |
| -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| Insert a new document                                                      | The document appears, transformed.                                                               |
| Update a field that the transform converts or renames                      | The same document changes. No second document appears.                                           |
| Update a document that was copied earlier by `testsync` or an initial sync | The existing document changes. A duplicate here means `mapid` and the ID field mapping disagree. |
| Delete a document                                                          | The document is removed. If it stays, the delete ID does not match the stored ID.                |
| Insert a document that the transform filters out                           | Nothing appears.                                                                                 |

Check the source connector's reference page for event types it does not produce. For example, SQL Batch reports inserts as updates.

## Step 8: Run the Full Dataset in a Test Environment

Samples cannot cover every document. A full run in a test environment finds the rest.

**Survey run.** The goal is a complete list of problems in one pass, without stopping at the first one.

1. Set `filtererrors: true` at the top of the transform config. Documents that fail to transform are skipped and logged, one line per document, with the ID and the reason.
2. Start the sync with `--ignore-write-failures`, so that documents the destination rejects do not stop the sync.
3. Let the initial sync finish.
4. Collect the results:
   * Transform failures: the `transform.conversion_error` counter per namespace gives the count. The `filter-error` log lines give the ID and the reason for each document.
   * Write failures: check the `dsynct.written` metric with `success=false`, and compare document counts per namespace between source and destination.
   * Destination shape: inspect the fields and types that were actually stored, for example with a schema analysis tool on the destination.
5. Fix the config, add the failing documents to your samples, and repeat steps 5 to 8 until the survey run is clean.

What each error means and where to find it is in [Troubleshooting Transforms](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/troubleshooting.md).

**Strict run.** Remove `filtererrors` and `--ignore-write-failures`, clear the destination and run again. This is the configuration to use in production: any failure stops the sync and is visible, instead of silently skipping data.

**Verify.** Run [verification](https://docs.adiom.io/enterprise/running-dsynct/verification.md) on the result. With a transform, use `sample-ids` and `verify-ids` with the same transform config to compare a sample of documents.

## Step 9: Run in Production

Production data often contains shapes that the test environment does not.

1. If you can, repeat the survey run from step 8 against production data into a non-production destination.
2. If a full survey is too expensive, run it for the namespaces with the oldest or least uniform data, and use `sample-ids` with a large `--count` and `testsync` for the others.
3. Run the real migration with the strict configuration.
4. Watch the Progress view in the [Web-API](https://docs.adiom.io/enterprise/running-dsynct/web-api.md#progress) for activity errors, the `transform.conversion_error` counter, and the `dsynct.get_transform` and `dsynct.written` metrics with `success=false`. See [Observability](https://docs.adiom.io/enterprise/running-dsynct/observability.md).
5. Before cutover, verify as in step 8.

## Changing a Transform Later

Destination schemas change during a project. To change a transform without breaking what already works:

1. Keep the sample documents from step 3 and the output you accepted for each, as files.
2. After editing the config, run all samples again through the [Transform API](https://docs.adiom.io/enterprise/running-dsynct/web-api/api-reference.md#transform) and compare with the accepted output. Only the fields you meant to change should differ.
3. Repeat step 6 for the affected namespaces.
4. Restart every worker on the queue with the new file. Workers on the same queue must not run different configs.

Documents already on the destination were written with the old config. If the change affects stored fields, plan to re-sync the affected namespaces.

## Reverse Sync

A reverse sync copies changes from the new database back to the old one, usually as a fallback after cutover. It is a separate sync with source and destination swapped, and it needs its own transform file that undoes the forward mapping. Dsynct has no `--reverse` option: start the reverse sync as a new `sync` (or `run` with Temporal) with the connectors swapped, the reverse transform file as the third argument, and `--skip-initial-sync`, so that only changes made after the cutover are copied back.

1. Write the reverse config. For each forward mapping, swap `namespace` and `mapnamespace`, invert the ID mapping, and convert types back. See [Reverse Sync Pairs](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/templates.md#reverse-sync-pairs).
2. Test it with steps 5 to 7, like any other transform. The "source" is now the new database.
3. Check the round trip: take a sample document from the old database, run it through the forward config, run the result through the reverse config, and compare with the original. Differences in types (a number that returns as a string) need an explicit conversion in the reverse config.

Things that do not reverse on their own:

* Fields the forward transform deleted are gone. The reverse sync cannot restore them.
* One-way conversions, such as hashes and fake data.
* IDs where the forward mapping dropped a part. If a two-part source ID was collapsed by keeping only one part, the reverse mapping cannot rebuild the other. Keep all parts in the destination ID (for example joined with a separator) if you plan to reverse.
* New documents created on the new database need an ID that is valid on the old one.

## Working With an AI Agent

An agent can do steps 4, 5 and the regression check on its own if it has the right inputs:

* This page, the [Config Reference](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/config-reference.md), [IDs, Data Types and CDC Events](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/data-types.md), [Templates](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/templates.md), the [CEL Reference](https://docs.adiom.io/enterprise/running-dsynct/data-transformations/cel-reference.md) and the reference page of your source connector. Add `.md` to any page URL to get Markdown.
* The sample documents from step 3, per namespace, in the emitted format.
* The target shape for each destination namespace.
* The address of a running web-api, so that it can test each config with the [Transform API](https://docs.adiom.io/enterprise/running-dsynct/web-api/api-reference.md#transform) and read the errors.

Ask it to return, for each namespace, the config, the output for every sample, and a list of samples it could not handle. Review the ID mapping and every type conversion yourself, then continue with step 6.
