← Workflows

阅读中文版本

Database Migration Create Workflow

Create a new database migration file with up and down scripts, test it locally, and verify rollback works.

beginner~25 mindatabasemigrationworkflow

Objective

Create and test a reversible database migration before applying it to staging or production.

Environment

  • A project with a migration tool
  • A local database for testing
  • Migration tool CLI available

Steps

1. Generate migration

Use the migration tool to create a new migration file with a descriptive name.

Expected state: A new migration file is created with up and down functions.

2. Apply migration

Run the migration tool to apply the new migration to the local database.

Expected state: The local database schema is updated.

3. Test rollback

Run the rollback command to revert the migration and confirm the database returns to the previous state.

Expected state: The database schema is restored to the pre-migration state.

4. Re-apply

Apply the migration again to confirm it is idempotent.

Expected state: The migration applies cleanly after rollback.

Decision points

Should migrations be reversible?

  • Yes, always include down
  • No, only for schema changes

Always include a down migration; data migrations may need special handling.

Expected outcome

A tested migration that can be safely applied and rolled back in production.

Verification

Apply the migration, rollback, and re-apply; confirm the database state is correct at each step.

Cleanup

Roll back the migration if you do not want it applied locally.

Always test rollback

A migration that cannot be rolled back is a production risk. Test the down path every time.

npx prisma migrate dev --name add_users
npx prisma migrate rollback

Implementation Notes

This workflow provides a structured approach to ensure consistent results across team members and environments.

Common Pitfalls

Skipping verification steps or rushing through the process can lead to incomplete results. Always follow each step in order and verify before proceeding.