guide
guide9 min read

How Do You Test an AI Agent's Schema Migration Against Real Data Without Breaking Shared Staging?

Give every agent migration its own copy of the data, then gate promotion on checks

To test an AI agent's schema migration against real data without breaking shared staging, run each migration in its own isolated copy of the data, such as a database branch or a warehouse clone, then check it there before anything touches a shared environment. Snowflake's clone documentation describes clones that share storage with the source until data diverges, which is the property that makes a throwaway copy per agent run practical. This post lays out the five steps we recommend.

Key Takeaways

  • •Shared staging fails for agent-written migrations because several runs mutate one environment, so one agent's half-applied change corrupts another agent's test.
  • •Give every migration attempt its own isolated copy of the data (a database branch or a warehouse clone) and delete it when the run ends.
  • •An isolated copy only proves the migration runs. You still need contract checks, a downstream blast-radius projection and row-level parity checks before promotion.
  • •Warehouse time travel is a rollback aid, not a test environment: Snowflake's Time Travel defaults to 1 day of retention, and BigQuery's time travel window defaults to 7 days.
  • •Promotion to production should be an explicit approval step, set per domain, with the agent's evidence attached.

Why shared staging breaks when an agent writes the migration

A shared staging database assumes a small number of humans taking turns. A person proposes a change, runs it, looks at the result and tells the team. Agents break that assumption. One agent can generate several candidate migrations in a single session, retry after a failure and run in parallel with other agents working on different tickets. Every one of those runs writes to the same tables, so the state a test sees depends on who ran first.

The failure modes are specific. A migration that renames a column succeeds on the first attempt and fails on the retry because the old name no longer exists. A second agent validating an unrelated model reads the half-migrated table and reports a bogus failure. A cleanup step drops a staging table that another run still needs. None of these say anything about whether the migration is correct. They are environment noise, and engineers lose hours separating signal from noise.

There is also a data realism problem. Teams often seed staging with a small synthetic sample, because copying production is slow and expensive. Synthetic data rarely contains the awkward rows that break migrations: nulls in a column the new constraint forbids, duplicate keys, values wider than the new type. We have found that migrations pass on a clean sample and fail on the first real null. Testing against real data matters, and shared staging makes it hard to do safely.

Step 1: give each migration run its own isolated copy

The first step is to stop sharing. Each migration attempt gets its own branch or clone of the data it will touch, named after the run so you can trace it. Transactional databases offer branching, and warehouses offer clones. Snowflake's clone documentation states that a clone initially shares the source's underlying storage, so creating one does not copy every byte up front. BigQuery documents a similar mechanism in its table clones overview.

Scope the copy tightly. An agent changing one table does not need a clone of the whole warehouse. We recommend cloning the target table plus the tables a migration is expected to read or join, and nothing else. That keeps storage growth small and reduces the chance that a clone includes data the agent should never see. If the clone includes sensitive columns, apply masking policies to the copy before the agent gets access, and treat the copy with the same access rules as the source.

Give the copy a lifetime. Name it with a run identifier, set an expiry and delete it when the run ends, whether it passed or failed. Orphaned clones are the new version of the abandoned staging table. We suggest the orchestrating system, not the agent, owns creation and teardown, so a crashed agent cannot leave copies behind. Keep the agent's credentials limited to its own branch, so even a badly generated statement cannot reach shared objects.

Step 2: run the migration and check the contract

With an isolated copy in place, the agent applies the migration there and runs your existing checks. The first check is the interface: did the change alter the shape consumers depend on? dbt's model contracts let you declare column names and data types for a model and fail the build when the model no longer matches. If you already use them, the isolated run is the right place to enforce them, because a contract failure costs nothing there.

Next comes the value check. A migration can honor the declared schema and still change meaning, for example by casting a timestamp to a date and silently dropping the time. Run your not-null, uniqueness and accepted-value tests against the migrated copy. We keep these two layers separate on purpose: the contract answers whether the shape is intact, and the tests answer whether the data is still valid inside that shape.

Capture everything the run produces: the migration SQL, the statements that executed, error messages, row counts before and after, and test results. That record is the evidence a reviewer will read later. An agent that reports only 'migration succeeded' gives a human nothing to approve. An agent that attaches the exact diff, the failing and passing checks and the copy's row counts lets a reviewer decide in minutes.

Step 3: project the downstream blast radius

A migration that works on the table it targets can still break everything built on top of it. Before promotion, ask what depends on each column the migration touches: views, dbt models, dashboards, reverse-ETL syncs and other pipelines. This is a lineage question, and it is cheap to answer before the change and expensive to answer after an incident. Our Schema Agent is built for this job: it detects column-level drift, projects downstream impact and generates safe migrations.

Treat the projection as a list of consumers to verify, not a score. For each downstream model, run its tests against the migrated copy where you can. Where you cannot, flag it for a named owner. If a dropped or renamed column appears in the lineage of a dashboard used by finance, that is a different conversation from one that appears only in an unused staging model. The projection tells the reviewer where to look.

Pair the projection with usage data. A column that no downstream consumer has read recently is a safer change than one read hourly, and warehouse query history can show that. We described the technique in a separate post on joining dbt manifests to read logs. The combination of lineage and usage keeps the agent from either over-warning on harmless changes or waving through a change that quietly feeds a critical report.

Step 4: verify parity with row-level checks

Passing tests do not prove the migration preserved the data. A parity check compares the pre-migration and post-migration states directly. Count rows, compare per-column null counts and distinct counts, and hash or checksum key columns to find changed values. For a type change, sample rows and compare original and converted values. The isolated copy makes this easy because the untouched source and the migrated copy sit side by side.

Define what a difference means before you run the check. A column split into two should show every original value represented in the pair. A backfill should change exactly the rows you intended to change and no others. For example, suppose a hypothetical table has a nullable column and the migration makes it required with a default. A parity check should show that rows with nulls received the default and every other row is unchanged. If the changed-row count is not the null count, something else moved. The numbers in this scenario are illustrative only.

Be honest about what the copy cannot show. Performance on a branch can differ from production because of concurrent load, caching and data volume if you cloned only a subset. Treat the branch's runtime as a rough signal and not a promise. Locking behavior on a large production table, for instance, is better checked against the database's own documentation, such as PostgreSQL's notes on which ALTER TABLE forms rewrite the table. We would rather the agent flag that uncertainty than hide it.

Step 5: gate promotion and plan the rollback

Promotion is where autonomy should be a deliberate choice. Whether an agent may apply a verified migration to production without a human is a policy question, and the answer can differ by domain. A low-risk internal sandbox schema might allow it. A finance schema should not. We call this a per-domain autonomy dial: the same agent, the same evidence, a different approval requirement depending on what the data feeds.

Before promoting, write the rollback. For a reversible change, that is the inverse migration, tested on the isolated copy the same way as the forward one. For an irreversible change such as dropping a column, rollback means restoring from a retained copy. That is where warehouse time travel helps, but its window is finite: Snowflake's Time Travel retains data for 1 day by default, with longer periods available on higher editions, and BigQuery's time travel covers 7 days by default. Do not assume you can recover from a mistake you discover next month.

After promotion, watch for the first full run of each downstream consumer and compare against the evidence from the branch. If a downstream test fails, the agent should have the branch results and the lineage projection to hand, which narrows diagnosis. Then delete the isolated copy. A run is finished when the production change is verified and the temporary resources are gone.

Where this fits with the tools you already use

None of this requires a new platform. The pattern works with whatever branching or cloning your database offers and the tests you already run in CI. What changes is who orchestrates it. Data Workers is open source and its agents run as MCP servers, so you can invoke them from Claude Code, Cursor or another MCP-compatible client; the MCP specification defines the protocol they speak. The open-source edition is read-only by default, which suits the diagnose-and-propose half of this workflow.

Writes, such as applying a promoted migration, belong to the Pro edition, which adds approval workflows and audit trails. For teams moving off legacy pipelines, our Migration Agent analyzes legacy SQL and ETL and emits modern targets such as dbt or SQLMesh models, and the same isolate-test-verify loop applies to its output. We think the loop matters more than any one tool: isolate, check, project, verify, gate.

Frequently Asked Questions

Can I just use a smaller synthetic dataset in shared staging?

You can, but it tests the wrong thing. Synthetic samples rarely include the nulls, duplicates and oversized values that break real migrations, so a pass tells you little. An isolated copy of real data, with masking applied where needed, gives you realistic failures without exposing other runs to them. Keep synthetic data for unit-style tests of the migration logic itself.

Does a database branch or clone replace time travel?

No, they do different jobs. A branch or clone is a place to test before you change anything. Time travel is a way to look at or recover earlier table states after a change, within a retention window. Snowflake's documentation lists a 1-day default for Time Travel, so we use clones for testing and treat time travel as a short-term safety net.

How do I stop an agent from touching shared environments?

Scope its credentials. Give each run a role that can read from the source for cloning and write only to its own branch. Have the orchestrator create and delete the branch, not the agent. Production writes then require a separate, approved step. This keeps a badly generated statement from reaching anything beyond its own sandbox.

Who should approve a verified migration?

The owner of the data being changed, with the agent's evidence attached: the SQL diff, contract and test results, the blast-radius projection and the parity numbers. How much approval is needed depends on the domain. We suggest setting it per domain, not as one policy for the whole platform.