# Let your coding agent change the database without losing data

- Author: RomanMotion (https://wurk.fun/user/RomanMotion)
- Published: 2026-11-03
- Canonical (HTML): https://wurk.fun/blog/let-your-coding-agent-change-the-database-without-losing-data
- Cover image: https://ik.imagekit.io/wurk/browser-uploads/f1c626b5c17e7f0a61dfbbe7a20d85ae7b33a20527243aeec2ee1d0a8b87562a/6f50db28-9c59-45bd-9d9e-b7d5415244dc/cover-migr.jpg?wurkUpload=v1.hNqcPky6__SBPdV1f5wp4lxTvl0h4kl4lM2BUwC9eyU

---

You’ve asked your coding agent to update a feature, and it wants to change a database table. If it drops the wrong column, real user data can disappear even when the code looks fine. A reviewed migration and a tested backup give you a safer way to make the change.

Have the agent write migration files for review, explain how to reverse the change, and test them on an isolated copy. Back up the live database and verify the restore before an approved deployment. Keep the agent away from production until you’ve reviewed the plan and its SQL.

## Why a code rollback isn’t a data rollback

Your app’s code describes how to work with data. The database holds the actual records: accounts, messages, orders, or whatever your app stores for its users.

If an agent removes a field from a code file, you can restore the earlier file. If a migration drops a database column, putting the code back won’t recreate the values that were stored there. PostgreSQL’s [`ALTER TABLE` documentation](https://www.postgresql.org/docs/current/sql-altertable.html) describes how a column can be dropped along with related indexes and constraints.

That’s why “just edit the schema” deserves a pause. Even a change that looks like a rename can break a live app if the old code and new database no longer agree.

For the code side, our guide to [Git for vibe coders](https://wurk.fun/blog/git-for-vibe-coders-one-command-undoes-your-ai-agent) covers ways to review and undo file changes. Keep a separate recovery plan for the database: a code history is not a backup of user records.

## Put every schema change in a migration

A migration is a recorded change to your database structure. Instead of editing a live table by hand, put the change in a file that can be reviewed, tested, and applied through your project’s migration process.

Set the boundary before handing the task to your agent: **no manual schema edits and no raw SQL against production**. The agent can inspect the project and prepare files, but it shouldn’t apply changes to the live database. Use an isolated test database for experiments.

Ask for an **up** migration that makes the change and a **down** migration or explicit rollback procedure that describes how to reverse it. Read the actual SQL, not just the agent’s summary. Search for drops, renames, data updates, and changes to constraints, then ask what each one does to existing records.

A down migration is not a magic undo. If the up migration deletes values, recreating an empty column won’t restore them. Some changes are genuinely irreversible without a backup or a separate recovery process. Have the agent say so instead of inventing a reassuring down script.

PostgreSQL [transactions](https://www.postgresql.org/docs/current/tutorial-transactions.html) can group supported operations so they either commit together or roll back before commit. They don’t replace a backup, and you should check whether the specific commands and migration runner support your transaction plan.

## Expand first, contract later

Suppose you want to rename a field used by a live app. Changing the database and app code at the same moment can leave older running code looking for a field that no longer exists.

A safer pattern is **expand, then contract**:

- Add the new column while keeping the old one.
- Copy existing values and check that the copy is complete.
- Update the app to use the new column.
- Remove the old column in a later, separately reviewed migration.

That outline needs a plan for writes during the transition. If users can update the old field while you copy data, the columns can drift apart. Ask the agent how it will keep them consistent, handle missing values, and verify the result before switching reads. Depending on the app, that might mean temporarily writing both fields or pausing writes during a controlled change.

Keep the old column until you’ve checked the new code and are comfortable with the rollback plan. Dropping it later is still destructive: review and back up before that migration too.

This approach also works for retiring fields: first stop the app from relying on them, then verify and remove them later. If you’re choosing a workspace for this workflow, you can [try the zcode agent harness free](https://zcode.z.ai/invite/ALWM9JUA) through our invite.

## Back up, restore, then test the change

Before a live migration, create a fresh backup and verify that you can restore it to a separate, isolated database. A backup file that hasn’t been tested is a recovery hope, not proof that recovery will work.

For PostgreSQL, [`pg_dump`](https://www.postgresql.org/docs/current/app-pgdump.html) can export a database to a custom-format archive. A basic example is:

```
pg_dump --format=custom --file=backup.dump --dbname=appdb
```

`appdb` is a placeholder. Confirm the host, account, database, and permissions before running it; don’t paste passwords into the command. Protect the resulting file, which may contain sensitive data.

The archive is restored with `pg_restore`, as explained in PostgreSQL’s [backup and restore guide](https://www.postgresql.org/docs/current/backup-dump.html). Restore it to a separate empty test database, check for errors, and verify representative records. A `pg_dump` export covers one database, not cluster-wide roles and tablespaces, so check what else your recovery needs.

For SQLite, use a method appropriate for a live database rather than casually copying a file while it’s being written. The [SQLite backup documentation](https://www.sqlite.org/backup.html) describes its online backup API and other options.

Test the up migration on the isolated copy, check the data and app, then test the down procedure. A restore returns a snapshot, so plan how to handle writes made after the backup. If recovery or the test fails, stop before production.

## Give the agent a clear boundary

Paste this prompt and replace the brackets with your task. The agent should report its work for review, not treat the request as permission to deploy.

```
Task: [describe the database change]

Inspect the existing schema and migration process.
Create migration files for the change, including an up
and a down migration or a specific recovery procedure.

Do not connect to or modify production. Do not run raw
SQL on production or apply a live migration.

Explain:
- The exact SQL and which data it affects
- Whether any step can lose data or block the app
- How the app stays compatible during the change
- How concurrent writes stay consistent
- How to back up, test, and recover

For renames or removals, propose an expand-and-contract
plan. Test on an isolated copy and report the commands,
results, and anything you could not verify.

Stop for my review before any production deployment.
```

If the agent can’t access a safe test copy, it should say so. Don’t fill the gap by pointing it at the live database. Use sanitized data when possible, and keep access to any copy containing real user data restricted. A test run on a copy can take several rounds of agent work; if you’re picking a plan for that, our referral link gives [10% off GLM coding plans](https://z.ai/subscribe?ic=R0K78RJKNW).

## Review before you deploy

Use this checklist when the migration comes back:

```
[ ] I read the up and down SQL, not just the summary
[ ] Drops, data updates, and constraints are explained
[ ] The backup restored successfully in isolation
[ ] Up, down, app behavior, and data checks were tested
[ ] Concurrent writes and old app versions are covered
[ ] Recovery limits and post-backup writes are understood
[ ] I approved the production deployment plan
```

If any box remains unchecked, resolve it before the live change. A careful review can catch a migration that passes its test while still risking data the test didn’t cover.

There is [more on our blog](https://jackgross-five.vercel.app) about working safely with coding agents.

If this guide helped you protect a project, you can leave a tip on WURK to support more practical posts.
