Skip to main content

ADR 0016 — Managed destination tables: manage_table and on_schema_change

  • Status: Accepted 2026-10-28 (owner confirmed the proposals — see "Decisions").
  • Issue: #760 (epic #1227).
  • Relates to: ADR 0005 (warehouse writes are opt-in; cursor incremental stays first-class); ADR 0017 (declared columns inform the types); #317 (introspect_schema()); #542 (boundary tests documenting today's failures); #695 (privilege docs).

Context​

SQL/warehouse destinations need the target table to exist with the right columns, and the INSERT column list comes from the first record's keys. Two consequences: every new sync starts with hand-written DDL, and when the model adds or drops a column drt fails inside the driver (or loads a subset) — there is no drt-level drift policy. #760 asks for an opt-in create-if-missing and an explicit policy for new columns.

Two commitments from ADR 0005 must not break: the read-only, cursor-based path stays first-class (this is an addition), and turning the option back off must keep working (no one-way door).

Decision​

1. Two independent, opt-in keys on SQL destinations​

destination:
type: postgres
table: public.users_enriched
manage_table: true # default false: CREATE TABLE IF NOT EXISTS before the first load
on_schema_change: fail # fail (default) | ignore | append_new_columns

manage_table absent or false is exactly today's behaviour. on_schema_change is meaningful with or without manage_table. Non-SQL destinations reject both keys at validate time (an error, not a warning: silently ignoring a drift policy is how drift gets through).

2. The check runs before any record is loaded​

For each new batch shape (at the same point ADR 0017's contract check uses) drt compares the destination-facing record keys with the table's columns and decides before writing that batch. The known column set is retained across the run, so a sparse late column triggers reconciliation once without making stable batches repeat the probe:

Situationfailignoreappend_new_columns
Table missing, manage_table: truecreatecreatecreate
Table missing, manage_table: falseerror naming the tablesamesame
Source has a column the table lackserror naming the columnsload the shared columns, warnALTER TABLE ADD COLUMN (nullable), then load
Table has a column the source lacksallowed if nullable/defaulted, else errorsamesame
Column type incompatibleerror with guidancenot checkederror with guidance

Removals and type migrations are never performed by drt: they fail with guidance. That is the line that keeps this safe enough to be opt-in rather than dangerous.

3. Types​

Created columns take their type from the declared contract (ADR 0017) when there is one, otherwise from the source cursor metadata, otherwise a portable fallback (TEXT / VARIANT / STRING). Nothing is inferred by sampling values. Added columns are always nullable.

Compatibility remains a coarse type-family check. Width, precision/scale, and timestamp timezone semantics are intentionally not compared; the operator and database retain responsibility for narrower pre-provisioned types.

4. Dry-run, plan and privileges​

  • --dry-run and drt plan report the DDL they would run and never execute it. A plan whose destination table is missing is reported as such, not as an error.
  • Required privileges (CREATE, ALTER) are documented per warehouse; a missing privilege is a pre-load error naming it, extending the #695 guide.
  • Executed DDL is recorded on the run (additive history field, omit-when-absent, ADR 0013 style) so "who added this column, when" is answerable.
  • Exact replay can reproduce contract-declared types because they remain in config. It refuses typed DDL that depended only on ephemeral source cursor metadata, which is not stored in the replay payload.

5. Phasing​

Postgres and MySQL first, then Snowflake, BigQuery, Databricks (the order #317 and #340 used). plan_coverage gets a line per destination as it lands. Each warehouse's DDL dialect is behind the existing sql_base seam; no new abstraction.

Non-goals​

  • Dropping or renaming columns, type migrations, index/constraint management (upsert_key must already be a constraint the engine can use; creating one is out of scope).
  • DDL for SaaS destinations (HubSpot properties etc.) — future per-connector work.
  • Atomicity of DDL with the load: most warehouses auto-commit DDL, so the order is check → DDL → load, and a failed load leaves the (compatible) new columns in place.

Consequences​

  • One config surface, default off; existing syncs unchanged.
  • The pre-load error for drift replaces driver stack traces with a message naming columns.
  • drt can now alter a customer's table when asked: the opt-in, the nullable-only additions and the recorded DDL exist to keep that auditable.

Decisions​

Confirmed by the maintainer on 2026-10-28, all as proposed:

  1. on_schema_change defaults to fail.
  2. A missing table with manage_table: false is a clear pre-load error.
  3. First release: Postgres + MySQL.
  4. Executed DDL is recorded in run history (additive).