Migrations
This page describes how database migrations are handled across services.
Migrations ensure that database schema changes are versioned, reproducible, and safely applied across environments.
Ownership
Section titled “Ownership”- Each service owns its own migrations
- Migrations must not span across service boundaries
General principles
Section titled “General principles”Database migrations must:
- Be version-controlled
- Be applied in a deterministic order
- Be safe to run across multiple environments
- Represent incremental schema changes
Manual changes directly in shared environments must be avoided.
Creating migrations
Section titled “Creating migrations”When creating a new service or changing an existing schema, migrations must be used to describe the change.
Each migration should:
- Perform a single logical change
- Be easy to understand when read in isolation
- Avoid bundling unrelated schema changes together
New services
Section titled “New services”When creating a new service with a database:
- Define the initial schema using migrations
- Ensure the schema can be fully recreated from migrations alone
- Do not rely on manual bootstrap scripts
The goal is that a new environment can be set up by applying migrations from an empty database.
Applying migrations
Section titled “Applying migrations”Migrations should be applied automatically as part of the service lifecycle.
Typical environments where migrations are applied:
- Local development
- CI pipelines
- Test and staging environments
- Production (in a controlled manner)
Migrations should be applied by automation (pipelines or runtime), not manually.
Ordering and dependencies
Section titled “Ordering and dependencies”Migrations are applied in the order they are created.
Guidelines:
- Never modify an existing migration that has been applied
- New changes must always be added as new migrations
- Avoid dependencies between migrations across services
Changing an already applied migration can lead to inconsistent environments.
Rollbacks and backward compatibility
Section titled “Rollbacks and backward compatibility”Migrations should be designed with rollback and backward compatibility in mind.
Guidelines:
- Avoid destructive changes when possible
- Consider transitional states when removing columns or tables
- Ensure application code can handle both old and new schemas during deployment
Validation and review
Section titled “Validation and review”All migrations must be:
- Reviewed as part of code review
- Tested locally before merging
- Applied in a test environment before production
Schema changes should be treated with the same care as application code.