BrainGrid
How-tos

What Is a Database Migration? The One Change Your AI Agent Makes That Can Lose Real Data

What is a database migration, in plain language for AI builders: the one change your coding agent makes that can drop a column or lose rows for real, and the criterion that keeps it safe.

BrainGrid Team
14 min read
What Is a Database Migration? The One Change Your AI Agent Makes That Can Lose Real Data

Most of what your coding agent does is safe to get wrong. A broken button, a bad layout, a feature that does the wrong thing, all of it is a quick re-prompt away from fixed, because the only thing that changed was code, and code is cheap to redo. A database migration is the exception. It is the one change your agent makes that touches the thing you cannot regenerate: your users' actual data.

That asymmetry is the whole reason this post exists. When an agent says it "ran a migration" or "updated the schema," it just did something categorically different from everything else it does, and almost nobody building with AI knows it. A bad migration does not throw an error you can shrug off and retry. It can drop a column, rename a field the app still expects, or delete rows that were never backed up, and there is no undo button for data that is already gone.

If you have been building with Cursor, Claude Code, Lovable, or Replit and your agent has ever "changed the schema" or "added a migration" while you nodded along, this post is for you. The hypothesis is simple: a database migration is a change to your product's memory, and "this migration loses no existing user data and can be reversed" is an acceptance criterion you specify and verify, not a default you trust the agent to get right.

#What a database migration actually is

Search the term and you will get two different answers, which is the first thing that trips people up. One meaning is the enterprise one: moving a whole database from one place to another, say an on-premises server to the cloud, or from MySQL to PostgreSQL. That is real, but it is not what your agent is doing when it builds your app.

The meaning that matters to you is the software one, sometimes called a schema migration. The developer community has a clean definition for it. As one widely shared explainer put it:

Database migrations are a way to incrementally modify your database schema.

Unpack that in plain language. Your database is where your app keeps everything it has to remember: users, their settings, their orders, their posts. The shape of that memory, which tables exist and what columns each one has, is the schema. A migration is a single, ordered instruction that changes the shape. Add a phone_number column to the users table. Create a new invoices table. Rename full_name to name. Each of those is a migration, and they run in order so that every copy of your app, the one on your laptop, the one your teammate runs, the one live on the internet, ends up with the exact same structure.

That is the useful part, and it is genuinely good engineering. Migrations are how a database changes safely over time without someone logging in and hand-editing the live data. The catch is in the word "shape." Changing the shape of a container that already has things in it is not the same as changing an empty one. Rename a column and every row's data has to come along for the ride. Drop a column and whatever was in it is gone. Add a required field to a table that already has a million rows and the migration has to decide what every existing row gets. These are the moments where a migration stops being a harmless structural tweak and starts being a change to data people are counting on.

#Why AI builders trip over this specifically

In the old world, the person who wrote a migration understood what it would do, because writing it required understanding it. You had to know your schema, know which rows already existed, and reason about what happens to them when the shape changes. The knowledge and the action came bundled together. You did not drop a column without knowing a column was about to disappear.

AI severed that link, same as it did for every other part of building. Now the migration can be written, and run, by someone who has never heard the word "schema." The agent generates the instruction, mentions it ran, and moves on to the next thing. The feature works in the demo. What does not come bundled in is the one question that actually matters here: what happens to the data that already exists when this runs.

This is not a hypothetical risk, and it is not rare. In July 2026, the team at Prisma, who build one of the most popular database tools for exactly this kind of work, wrote a post titled Stop Your AI Agent Dropping Your Database because the pattern had become common enough to need a company-wide fix. Their framing is worth sitting with:

That's how one schema-field request became 87 dropped tables: the .env pointed at production, and nothing in the tool knew or checked.

One field request. Eighty-seven tables gone. The same post catalogs an agent wiping a production database during a code freeze, and a developer watching every table in their live database turn empty ten minutes into an agent session. The now-infamous version of this was the builder whose Replit agent deleted the entire production database and then apologized for it. These are not stories about bad code. The code ran perfectly. It did exactly what it said. The problem was that nobody decided, out loud and in advance, that the data had to survive.

Prisma's own conclusion names the real issue precisely:

These catastrophic failures aren't really agent failures. They're tool failures.

That is the reframe the whole industry is circling. The agent is not malicious and it is not even buggy. It is optimizing for the goal you gave it, "fix the schema," "add the field," and it will take the shortest path there, including paths that run through your live data, unless something or someone drew a line it cannot cross. The line is the part you have to supply.

#The reframe: a migration is a criterion, not a command you rubber-stamp

Here is the move, and it is the same one we made when we wrote about API endpoints and row-level security: the dangerous thing is not the code the agent wrote, it is the rule nobody stated. A migration that destroys data is not a coding mistake. It is a missing criterion.

Think about the difference between two ways of asking for the same change.

Vague: "Add a name field and clean up the users table."

Specified: "Add a name column to the users table, populated from the existing full_name column so no current user loses their name. Do not drop full_name in this migration. This migration must run without deleting any existing rows, and I need to be able to reverse it. Run it against a copy first and show me the row count before and after."

Same intent. The first version hands the agent a goal and lets it pick the path, and "clean up" is exactly the kind of instruction that ends with a dropped column. The second version turns the two things that actually matter, no data lost and reversible, from invisible assumptions into stated rules. And notice you did not need to know a single line of SQL to write the second version. You needed to know what you refused to lose.

That is the part that changes what you are responsible for. You are not responsible for writing the migration; the agent is good at that. You are responsible for stating what the migration is not allowed to do to your data, and then checking that it obeyed. Two questions cover almost every dangerous case. Does this migration delete or overwrite anything that already exists, and if something goes wrong, can I get back to where I was? If the answer to the first is "yes" or "I don't know," that is the migration to slow down on. If the answer to the second is "no," that is the migration to not run until it is.

There is a real trade-off here, because pretending every migration is a crisis would make you slow and would be dishonest. Most migrations are genuinely safe. Adding a brand-new table touches no existing data. Adding an optional column breaks nothing. The skill is not treating every schema change like defusing a bomb. It is telling the safe ones from the destructive ones, renames, drops, type changes, anything that adds a required field to a table that already has rows, and spending your attention there. The new-table migration can run on a nod. The drop-column migration against your live database cannot. Knowing which is which is judgment, and judgment is the part the agent does not do for you.

#Where this fits in the loop

This is the gap BrainGrid is built to close. You describe the change in plain language, and the Planning Agent turns it into a requirement with explicit acceptance criteria, including the ones you would never think to write down yourself: that a migration touching existing data preserves every current row, that a destructive change is called out before it runs, that there is a path back if it goes wrong. "No existing user data is lost and the change is reversible" becomes part of the standard the Builder Agent builds against, whether that build runs in BrainGrid Cloud or in your own GitHub repo through Claude Code, Cursor, or Codex over MCP.

That is the loop: Plan the data rules along with the change, Build against them, Verify each one with evidence, Repeat. A migration that would drop a column full of real data does not quietly pass, because "loses no existing data" is written down as a criterion, and a change is not done until every criterion is verified with evidence. The evidence for a migration is concrete and anyone can read it: run it on a copy of the data, show the row count before and after, confirm the rollback works. Code review tells you the migration is well-written. Verification tells you the data is still there. For the one change that can lose real data, only the second question keeps your users whole.

#What this means if you are building right now

If your agent has a migration queued up, or just ran one, do one thing before you let it near your live data: ask it, in plain words, whether this migration deletes, renames, or overwrites anything that already exists, and whether it can be undone. For any change where the answer is yes, it deletes or renames, make two rules the migration has to satisfy, no existing rows are lost and the change is reversible, and verify them the only way that counts. Run the migration against a copy of your real data first, check that the rows you care about survived, and confirm you can roll it back. If you cannot, you found that out on a copy instead of on your users.

The uncomfortable truth here is the same one that runs through every honest conversation about building with AI. The agent is not destroying your data recklessly. It is doing exactly what you asked, in favor of whatever reaches the goal fastest, every time you leave the data rules out of the ask. A lost column is not a mysterious bug in code you cannot read. It is a rule you never wrote. Write the rule, make it a criterion, and check it against a copy. For the git side of undoing a bad change, we wrote the non-engineer's guide to pull requests, and for the broader skill, how to write acceptance criteria an AI agent can actually verify.

#FAQ

#What is a database migration, in simple terms?

A database migration is a single, ordered instruction that changes the shape of your database, the tables it has and the columns in each one, without someone hand-editing the live data. Common examples are adding a column, creating a new table, or renaming a field. Migrations run in order so every copy of your app ends up with the same structure. The reason they matter more than most changes is that some migrations touch data that already exists: renaming or dropping a column affects every row that already has a value there, so a bad one can lose real user data that cannot be regenerated.

#What is an example of a database migration?

The textbook example is adding a new column to an existing table, for instance adding an email column to a users table so the app can store email addresses it did not track before. Other everyday examples: creating a brand-new orders table when you add a checkout feature, renaming full_name to name, or changing a column's type from text to number. The safe ones add something new and touch no existing data. The risky ones, renames, drops, type changes, and adding a required field to a table that already has rows, change or remove data that is already there, which is why those are the ones to verify on a copy first.

#Can a database migration delete data?

Yes, and this is the part that surprises non-technical builders. A migration that drops a column deletes everything stored in that column. A migration that drops or replaces a table deletes its rows. A badly scoped one, or one accidentally pointed at your live database instead of a safe copy, can wipe far more than intended, which is how real teams have lost entire production databases to a single agent-run command. Additive migrations (new table, new optional column) do not delete anything. The rule of thumb: any migration that removes, renames, or overwrites something that already exists can lose data, so treat "no existing data is lost" as a requirement you state and check, not an outcome you assume.

#How do I safely run a database migration an AI agent wrote?

Four steps, none of which require reading the code. First, ask the agent plainly whether the migration deletes, renames, or overwrites anything that already exists. Second, make sure you have a current backup of your real data. Third, run the migration against a copy of your data, not the live one, and check the row counts and the specific records you care about before and after. Fourth, confirm you can reverse it. Only after it passes on a copy should it touch your live database. The underlying fix is to state "this migration loses no existing data and can be rolled back" as an acceptance criterion up front, so the agent builds toward that standard instead of toward whatever runs fastest.

#What is the difference between a database migration and data migration?

They sound identical but mean different things. A schema (or database) migration changes the structure of your database, adding a column, creating a table, renaming a field, and is the everyday thing your coding agent does as your app grows. A data migration is moving the actual data from one place to another, for example from an old system to a new one, or from one database product to another. The enterprise world usually means the second when it says "database migration"; your coding agent almost always means the first. Both can lose data if done carelessly, but the one you encounter building an app day to day is the schema migration.

BrainGrid is the AI Product Planner that turns your idea into a requirement with acceptance criteria, including that a schema change preserves every existing row and can be reversed, so your agent ships migrations that keep your data instead of losing it. Try it at braingrid.ai.

About the Author

The BrainGrid team building tools to help developers ship better software with AI.

Want to discuss AI coding workflows or share your experiences? Find me on X or connect on LinkedIn.

Get Started

Ready to build without the back-and-forth?

Turn messy thoughts into engineering-grade prompts that coding agents can nail the first time.

Describe what you want to build