DB Migrations for Local Dev: A Practical Guide
In this article
Database migrations are version-controlled changes to your database schema, and handling them well in local development prevents the classic “works on my machine” problem. This guide walks you through a safe, repeatable workflow for applying, testing, and rolling back migrations locally, whether you use Alembic, Flyway, Prisma, or Django’s built-in tools. You’ll learn how to keep your local database in sync with your team, avoid destructive mistakes, and debug common failures.
What You'll Need
- A local database (PostgreSQL, MySQL, SQLite, etc.) running on your machine or in a local container.
- A migration tool installed and configured for your project (e.g., Alembic, Flyway, Prisma, Django migrations).
- Access to your project’s migration files (usually in a `migrations/` or `db/` folder).
- A way to run CLI commands (terminal or IDE terminal).
- Optional but recommended: a clean database snapshot or a way to reset your local DB quickly (e.g., `docker-compose down -v`).
Step-by-Step
- Pull the latest code from your repository. Before touching anything, make sure you have the latest migration files. Run `git pull` (or your VCS equivalent) to get new migration scripts from teammates.
- Check your current migration state. Run the command your tool provides to see which migrations are applied. For example: Alembic: `alembic current`; Flyway: `flyway info`; Prisma: `npx prisma migrate status`; Django: `python manage.py showmigrations`. This tells you where your local DB stands versus the migration history.
- Compare with the latest migration. Look at the output and note the latest migration file’s revision ID or version number. If your local DB is behind, you’ll see a list of pending migrations.
- Apply the pending migrations. Run the upgrade command: Alembic: `alembic upgrade head`; Flyway: `flyway migrate`; Prisma: `npx prisma migrate dev` (this also generates new migrations if you changed schema); Django: `python manage.py migrate`. This will bring your schema up to date.
- Verify the schema. After migration, check that tables, columns, and indexes exist as expected. You can use `\d` in psql, `DESCRIBE` in MySQL, or a GUI tool. Also run a quick smoke test: insert a test row, query it, then delete it.
- Test your application against the new schema. Run your app locally and exercise the features that depend on the changed tables. This catches issues like missing columns or wrong data types before you push code.
- If you need to make further changes, create a new migration. Do not edit an already-applied migration file (unless it’s a brand-new one you haven’t shared). Instead, use your tool’s autogenerate or manual creation: Alembic: `alembic revision --autogenerate -m "description"`; Prisma: `npx prisma migrate dev --name description`; Django: `python manage.py makemigrations`. Review the generated SQL before applying.
- Apply the new migration and test again. Run the upgrade command again, then repeat the verification step. This ensures your new migration works on a clean state too.
- Reset your database to a clean state periodically. To avoid drift, it’s good practice to drop and recreate your local DB, then apply all migrations from scratch. This simulates a fresh setup and catches missing dependencies. For Docker: `docker-compose down -v && docker-compose up -d` then run migrations.
- Commit your migration files. Once you’re confident, commit the new migration file and any related code changes. Never commit local database data—only the migration scripts.
Common Mistakes
- Editing an already-applied migration. This causes checksum mismatches for your team. Always create a new migration to fix issues.
- Forgetting to pull latest code before migrating. You might apply an old migration that conflicts with new ones.
- Running migrations on a production-like database locally. Use a separate local DB, never your real prod data.
- Ignoring the down/rollback path. Many tools generate downgrade scripts automatically, but you should test them occasionally.
- Using autogenerate blindly. Autogenerated migrations can miss renames or data changes. Always review the generated SQL.
- Not resetting your DB after schema changes. Old data can hide issues that only appear on a fresh database.
Tips
- Use a consistent naming convention for migration files, like `YYYYMMDD_short_description`, so they sort chronologically.
- Keep migrations small and focused. One logical change per migration makes debugging easier.
- Run migrations in a transaction where your tool supports it (e.g., PostgreSQL with Alembic). This ensures atomicity—if something fails, nothing is partially applied.
- Use a local database that matches your production engine (e.g., PostgreSQL locally if prod is PostgreSQL). SQLite may behave differently.
- Automate the reset-and-migrate process with a script or Makefile target, like `make db-reset` that drops, recreates, and migrates.
- Keep a separate test database for running automated tests, so your dev DB doesn’t get polluted.
Troubleshooting
Migration fails with a “duplicate column” or “table already exists” error. This usually means your local DB is partially out of sync. Check `alembic current` or equivalent—if it shows the migration is applied but the schema doesn’t match, you may need to manually fix the schema or reset the DB and re-migrate.
Checksum mismatch errors. This happens when a migration file was edited after being applied. The safest fix is to reset your local DB and re-run all migrations from scratch. If that’s not possible, you can manually update the checksum in the migration tracking table, but only if you’re sure the file change is intentional and you’ll share it with the team.
Autogenerate produces an empty migration. This often means your tool doesn’t detect changes because you’re comparing against an outdated model. Make sure your ORM models are up to date, and try generating with the `--autogenerate` flag again after saving changes.
Migration takes too long. Large data changes can be slow. Consider batching updates or doing them in separate steps. For local dev, you can also just reset and re-migrate if the data isn’t important.
“Target database is not up to date” (Prisma) or similar. Run `npx prisma migrate dev` which will apply pending migrations and regenerate the client. If that fails, check for conflicting migration history.
You accidentally applied a migration to the wrong database. If it’s a local DB, you can safely reset it. If it’s a shared dev DB, coordinate with your team—you may need to roll back using the downgrade script, but only if no one else has applied newer migrations on top.
Frequently Asked Questions
How often should I run migrations locally?
Every time you pull new code. That’s the simplest rule—if your teammates have added a migration, you need to apply it before your app works correctly.
Can I skip a migration if I don’t need it?
No. Migrations are sequential; skipping one will cause conflicts. Always apply all pending migrations to keep your schema in sync.
What’s the difference between `upgrade` and `downgrade`?
Upgrade applies forward changes (e.g., from revision 1 to 2). Downgrade reverses them (from 2 to 1). You rarely need to downgrade locally, but it’s good to know how.
Should I commit my local database file (like SQLite)?
No. Database files contain data and should be in .gitignore. Only migration scripts are committed.
How do I handle a migration that fails halfway?
Most tools wrap migrations in transactions. If it fails, the transaction rolls back, leaving your DB unchanged. If it doesn’t, you may need to manually revert the changes or reset the DB.
Why does my migration work on my machine but not on my teammate’s?
Usually because of differences in database versions or existing data. Make sure everyone uses the same DB engine and version, and test on a clean database to catch environment-specific issues.