# source_aligned_cdc_data_product The template for a **source-aligned data product whose source system has a database you can read**. One product per source system. It captures the system's tables by change data capture, keeps the source's own records, and turns them into what a canonical data product accepts. It runs on the **Integration Bus** (semprini-data `compose.data-bus.yml`), not inside a domain: capture is shared infrastructure and one source system often feeds several domains. The CLI is `data-bus`, and this document is the contract between what the template produces and what that CLI applies. Verified 2026-09-18 on the bus with the worked [example/](example/) — 12 smoke checks, 37 unit tests — delivering ERP customers into Canonical Party in the digital-core domain. ## What a source-aligned product is responsible for A canonical product accepts exactly two things: records on its inbound Kafka topics, in the JSON Schema registered for the topic and keyed by the entity's identifier, or calls to its GraphQL API. Nothing writes to its ODS. So this product owns both transforms: | | From | To | |---|---|---| | **Protocol** | change events in the source's wire format | Kafka records with a registered schema | | **Semantic** | the source system's names, types, codes and keys | the canonical model's entities, enums and identifiers | ``` source DB ──Debezium──▶ raw... ──Flink──▶ ...inbound │ │ s3://raw//… └──rejected──▶ .dlq (archive, for replay) (record + reason) ``` Five properties the template exists to guarantee, and the tests that hold it to them: | Property | Why | Test | |---|---|---| | **Raw records are kept** in the source's own shape | Canonical records can be rebuilt without going back to the source, and an engineer can see what was actually sent | `test_capture.py` | | **Identity is deterministic** | A re-snapshot upserts rather than duplicating | `test_transform.py` | | **Every record is validated before publishing** | A record the canonical ODS refuses fails its Flink job's JDBC sink, which takes down every job on that TaskManager | `test_dlq.py` | | **Records are keyed by the canonical identifier** | Only a key keeps changes to one entity in order | `test_transform.py` | | **The archive can be replayed** | Topics age out; source records have to stay reproducible | `test_archive.py` | ## What the CLI does with it | `data-bus` | Reads | Does | |---|---|---| | `fixture up\|down` | `capture.fixture` | Starts or stops the test stand-in for the source system | | `deploy` | the whole manifest | Creates the topics, registers the schemas in Apicurio, checks the target's inbound topics exist, registers the connector on bus-connect, submits the transform | | `test` | the whole manifest | Smoke-tests topics, schemas, connector, transform, targets, archive and dead-letter depth | | `unit` | `tests/` | Runs pytest with the bus fixtures against the deployed product | | `replay` | `archive.replay` | Puts archived raw records back on the raw topic, for a date range | | `register` / `unregister` | `odps.yaml` | Publishes the product and its topics in OpenMetadata, with lineage into the canonical product | | `undeploy` | the whole manifest | Cancels the transform, discards the capture position, deletes the connector, topics and schemas | ## Layout ``` / ├── product.yaml the manifest: the contract above, filled in ├── odps.yaml the catalogue entry (ODPS v4) ├── connectors/.json Debezium connector config; credentials as ${NAME} ├── events/schemas/ │ ├── raw....json the raw contract: the source's shape │ ├── .dlq.json the dead-letter contract │ └── .dlq.capture.json what Connect could not parse at all ├── flink/job.sql capture → canonical: identity, mapping, validation, archive ├── flink/replay.sql archive → raw topic, for `data-bus replay` ├── fixture/ a stand-in for the source system, for tests only │ ├── compose.fixture.yml │ └── init/010_.sql its schema and a seed, including rows that must be rejected └── tests/ ├── expectations.yaml everything specific to this product ├── conftest.py, test_*.py generic: copy them unchanged └── capture, transform, dead letters, delivery, archive ``` The test modules are the same in every product of this class. Only `expectations.yaml` changes. ## Placeholders | Placeholder | Meaning | Example | |---|---|---| | `{{product_name}}`, `{{slug}}` | The product | `ERP Party`, `erp-party` | | `{{system}}` | Source system id; names topics, registry group, DLQ, archive | `erp` | | `{{source_host}}`, `{{source_db_name}}`, `{{source_db_user}}` | How to reach the source database | `erp-db.internal`, `erp`, `debezium` | | `{{source_schema}}`, `{{source_object}}`, `{{source_key}}` | The object captured and its key | `public`, `customer`, `id` | | `{{slot_name}}` | The replication slot, one per connector | `erp_party` | | `{{target_domain}}`, `{{target_product}}`, `{{target_product_name}}` | The canonical product fed | `digital-core`, `canonical-party`, `Canonical Party` | | `{{target_table}}`, `{{target_key}}` | Its table and identifier | `party`, `party_id` | | `{{catalogue_domain}}`, `{{owner_email}}` | Catalogue placement and owner | `digital-core`, `integration@example.com` | `${NAME}` and `${NAME:-default}` are **not** placeholders: they are substituted when the product is deployed, from the environment or semprini-data `.env`. The connector's `${SOURCE_PASSWORD}` is the one to rename per system (`ERP_SOURCE_PASSWORD`) when several products share an `.env`. The transform gets `${BUS_KAFKA}`, `${TARGET_BOOTSTRAP}`, `${RAW_TOPIC}`, `${DLQ}` and `${ARCHIVE_PATH}` from the manifest; the replay job also gets `${SINCE}` and `${UNTIL}`. ## Generating one What the generating agent needs: 1. The **source system's schema** for the objects to capture: columns, types, keys, and the meaning of its codes. This is the part no model holds. 2. The **canonical product's contract**: the inbound topic, its JSON Schema, its key, and the rules its ODS enforces (NOT NULLs, CHECK constraints, enums). Read them from the canonical product's `events/schemas/*.json` and `ods/*.sql`. 3. The **mapping** between them, including how every source code becomes a model enum value, and what makes a record unacceptable. Then: fill the placeholders, write the mapping and the reason expression in `flink/job.sql`, write `tests/expectations.yaml` from the fixture's seed, and run ``` uv run data-bus fixture up # only if the source is not reachable uv run data-bus deploy uv run data-bus test uv run data-bus unit uv run data-bus register ``` from a semprini-data checkout, with the bus and the target domain running. ## Type mapping Debezium's JSON, with `schemas.enable=false` and the default `time.precision.mode`: | PostgreSQL | On the raw topic | Declare in Flink | Canonical side | |---|---|---|---| | `int2`, `int4` | number | `INT` | as modelled | | `int8` | number | `BIGINT` | as modelled | | `text`, `varchar`, `char` | string | `STRING` | `STRING` | | `bool` | boolean | `BOOLEAN` | `BOOLEAN` | | `date` | days since 1970-01-01 | `INT` | `DATE_FORMAT(TO_TIMESTAMP_LTZ(d * 86400000, 3), 'yyyy-MM-dd')` | | `timestamp` | epoch microseconds | `BIGINT` | `TO_TIMESTAMP_LTZ(t / 1000, 3)` | | `timestamptz` | ISO-8601 string | `STRING` | as it stands | | `numeric` | base64 string (unless `decimal.handling.mode=string`) | `STRING` | set `decimal.handling.mode=string` on the connector | | `uuid` | string | `STRING` | `STRING` | | `json`, `jsonb` | string | `STRING` | parse in SQL if fields are needed | ## Prerequisites * The bus is running with its object store: `./stack-data.sh up bus` in semprini-data. * The target domain is running, and its Kafka joins `semprini_bus` as `kafka-:39092` (every domain built on semprini-data-domain does). * The target canonical product is deployed, so its inbound topics exist. `deploy` warns rather than failing if they do not, because a product may legitimately be deployed first. * For PostgreSQL sources: `wal_level=logical`, a role that may replicate, and `REPLICA IDENTITY FULL` on the captured tables if updates and deletes must carry the old row. ## What this template does not do * **It fills one canonical table.** A canonical product's subtype and child tables (Person under Party, Contact Address under Party Role) have foreign keys to their parents, and publishing a child before its parent has arrived fails the canonical product's JDBC sink. Kafka gives no ordering across topics, so a product that must fill both waits for the canonical product to accept a composite record. Until then: one table, the parent. * **It does not delete.** A canonical product's inbound contract has no delete, so a CDC delete is sent to the dead-letter topic with that reason rather than silently dropped. Removing a party is done through the canonical product's own interface. * **It does not validate against the registered JSON Schema.** Validation is the SQL in the transform, written from the canonical product's rules. The two can drift; the tests are what catch it. * **It carries no history.** The raw topic and the archive hold what the source sent, in order; what a record looked like at a point in time is the canonical product's lake, not this. * **One replication slot per connector.** Capturing many tables from one system means one connector with several tables in `table.include.list`, not one product per table. ## Known gaps * `data-bus undeploy` discards the connector's capture position but does not drop the source's replication slot: that needs a privileged connection to the source. Drop it by hand (`SELECT pg_drop_replication_slot('')`) or set `slot.drop.on.stop` on a lab connector. * The archive is written by the same Flink job as the transform, so an object-store failure fails the product's job. Splitting them costs a second job and a second checkpoint. * Connect's own dead letters land on `.dlq.capture` with the payload unchanged, so that topic has no useful schema — its contract is "whatever could not be parsed, with the failure in the headers".