TL;DR
|
A database migration is a script that changes a database's structure in a controlled, repeatable way. Adding a column, creating a table, renaming a field, or adding an index are all done with migrations.
Changes to a live database are some of the riskiest in software, and AI agents now make them faster than ever. This guide explains how migrations make database changes safe and repeatable, how to run them in production, and how to keep both people and AI agents from damaging real data. It covers schema migrations, the everyday developer meaning, rather than moving data between database systems.
Key takeaway: migrations are numbered scripts kept with your code, and a tool runs any the database hasn't seen yet, in order.
Because the migrations travel with the code, every environment, from a developer's laptop to production, ends up with the same database structure.
Key takeaway: a migration describes one change, often with a matching way to undo it.
Here's a migration in plain SQL that adds a column:
ALTER TABLE customers ADD COLUMN phone VARCHAR(30);Frameworks usually let you write migrations in your own programming language instead. Many also include an "undo" step, which reverses the change if you need to roll back.
| Framework | Migration tool | Run migrations |
|---|---|---|
| Django | Built-in migrations | python manage.py migrate |
| Rails | Active Record migrations | bin/rails db:migrate |
| Laravel | Built-in migrations | php artisan migrate |
| Symfony | Doctrine Migrations | php bin/console doctrine:migrations:migrate |
| Any language | Flyway, Liquibase | Their own command-line tools |
Key takeaway: migrations run as part of the deployment, after the new code is built and before it serves visitors.
The usual order is: build the new code, run the migrations, then send traffic to the new version. The build typically can't reach the database, so migrations belong to the deploy phase. Our guide What happens during a deployment? explains the phases.
Run migrations from one place only. If several servers each try to run them at the same moment, they can collide.
Key takeaway: adding things is usually safe. Changing or removing things the running code depends on is where releases break.
| Change | Risk | Why |
|---|---|---|
| Add a new table | Low | Nothing uses it yet |
| Add a column that can be empty | Low | Old code ignores it |
| Add a column that can't be empty, with no default | High | Old code inserting rows without it starts failing |
| Rename a column | High | Old code still uses the old name |
| Remove a column | High | Old code still reads it |
| Add an index to a large table | Medium | It can lock the table while it's built, unless the database can build it without blocking |
| Change a column's type on a large table | High | It may rewrite the whole table and lock it |
For the risky ones, the usual approach is to split the change across several releases, often called expand and contract: add the new structure first, release code that works with both old and new, then remove the old structure later. This also matters for zero-downtime deployments, where old and new code run against the same database for a while. The post Django deployments without downtime: static files and migrations walks through a real example.
Key takeaway: undoing a migration is possible but risky once real data has changed, so prevention beats cure.
Many tools can run a migration's undo step. But if users have added data since the migration ran, undoing it can lose that data. Removing a column throws away whatever was in it.
That's why it's worth taking a backup before a release that changes the database, and testing migrations against realistic data first. Our guide What is a deployment rollback? covers rollbacks in more depth.
Key takeaway: many migration failures come from testing on small, tidy data that looks nothing like production.
| Mistake | What happens | How to avoid it |
|---|---|---|
| Testing only on an empty or tiny database | A migration that takes a second locally takes an hour on production, or fails on messy real data | Test on a realistic copy of production data |
| Editing a migration that has already run | Environments end up with different structures | Never change a migration once it's shared. Write a new one |
| Mixing large data updates into structure changes | The migration runs for a long time and locks tables | Move big data updates into separate, batched jobs |
| Running migrations from several servers at once | Migrations collide or run twice | Run them from one place during deployment |
| No backup before a risky change | Nothing to restore if it goes wrong | Take a backup first |
Key takeaway: telling an AI agent not to touch production isn't a safeguard. Keeping production out of its reach is.
In July 2025, SaaStr founder Jason Lemkin was running a public 12-day experiment building an app with Replit's AI coding agent. According to his posts, the agent ran commands against the live database during a code freeze, despite instructions not to make changes without permission, and deleted records on roughly 1,200 executives and 1,200 companies. Replit's CEO called it unacceptable and promised to automatically separate development and production databases.
The lesson isn't that one tool failed. It's that instructions alone don't protect data. The agent had access to the production database, so a misjudgment became a disaster. The safeguards that work are the same ones that protect against human mistakes, applied deliberately:
| Safeguard | How it protects you |
|---|---|
| Separate environments, with separate databases | Agents work on a copy of the data, so a destructive command damages nothing real |
| No production credentials for agents | An agent can't run commands against a database it can't connect to |
| Changes as migration files, not direct commands | Every change to the database's structure is written down, reviewed, and run by the deployment process, not by the agent |
| Human approval before production | A person approves changes before they reach real data. Our guide What is human-in-the-loop in AI software development? covers approval gates |
| Read-only access for investigating | When an agent needs to look at production data to debug, give it a read-only account |
| Recent backups, and tested restores | If something does go wrong, you can get the data back. Our guide What is a deployment rollback? explains your options |
| Isolated workspaces | Running agents in a sandbox limits what they can reach. Our guide What is an AI agent sandbox? explains how |
Instructions still help as a second layer. A line in your AGENTS.md file, such as "Never run commands against production. Database changes must be migrations", tells an agent what's expected. Our guide AGENTS.md vs CLAUDE.md vs .cursorrules explains how. But treat instructions as guidance, and the separation as the protection.
Key takeaway: on Upsun Cloud, each preview environment can start with a copy of production data, so a migration runs against realistic data before it ships.
On Upsun Cloud, branching an environment copies its databases and files along with the code by default, and with the GitHub integration, each pull request gets an environment with its parent's data by default. That gives you a production-sized database to test a migration against before it reaches real users.
A typical flow:
upsun backup:create.Here's the hook that runs Django migrations on every deployment:
applications:
myapp:
type: "python:3.14"
hooks:
deploy: |
set -e
python manage.py migrate --noinputA few details from the Upsun docs: the deploy hook has access to the database and runs before the app accepts requests, and only the web app runs it, not workers. Remove or mask personal data in preview environments first. The docs include guides to sanitizing databases for Drupal, Django, and Symfony. Our guide What is a preview environment? explains how preview environments get their data.
The same setup keeps AI agents away from production data. Each preview environment has its own copy of the database, with its own connection details, so an agent working on a branch is working on a copy. Its migrations reach production only by being merged and deployed, through the deploy hook, after review. And if you connect AI assistants to Upsun Cloud through the Upsun Cloud MCP server, it's read-only unless you explicitly turn on write access.
You can deploy on Upsun Cloud free for 15 days, with no card needed, or see how Git-driven automation gives every pull request its own environment.
Yes, if it can connect to it. In a widely reported 2025 incident, an AI coding agent deleted a live database despite instructions not to make changes. Keep agents working on copies of your data in separate environments, never give them production credentials, and make database changes through reviewed migrations.
A schema migration changes the database's structure, such as its tables and columns. A data migration changes or moves the data itself. "Database migration" can also mean moving to a different database system entirely.
In your code repository, usually in a folder named migrations, so they're versioned and reviewed with the code that needs them.
During deployment, after the new code is built and before it starts serving visitors.
Often, if the migration includes an undo step. But undoing it after real data has changed can lose that data, so test first and take a backup before risky changes.
Some changes, such as altering a column's type or adding an index on a large table, lock the table while they run. Use your database's non-blocking options where they exist, and split large changes into smaller steps.
Yes. Even small projects end up with several environments, and migrations are the reliable way to keep their databases in step.