Skip to main content
Follow these guidelines to write maintainable, safe, and effective database migrations.

Version Numbering Strategies

Format: YYYYMMDDHHmmss Advantages:
  • ✅ No merge conflicts with parallel development
  • ✅ Chronological ordering
  • ✅ Supports distributed teams
Disadvantages:
  • ⚠️ Less human-readable
  • ⚠️ Doesn’t convey significance
Best for: Teams with frequent parallel development

Strategy 2: Semantic Versioning

Format: v<major>.<minor>.<patch> Advantages:
  • ✅ Conveys change significance
  • ✅ Aligns with application versioning
  • ✅ Clear breaking change indication
Disadvantages:
  • ⚠️ Requires coordination
  • ⚠️ Merge conflicts possible
Best for: Teams with coordinated releases

Strategy 3: Sequential with Milestones

Advantages:
  • ✅ Simple and clear
  • ✅ Milestones mark releases
Disadvantages:
  • ⚠️ Merge conflicts in parallel work
  • ⚠️ Gaps can be confusing
Best for: Small teams, sequential development

Migration File Best Practices

Keep Migrations Small

  • Single, clear purpose
  • Fast execution
  • Easy to review
  • Simple to rollback
Guideline: One logical change per file

Include Rollback Planning

Document rollback approach in comments:

Transaction Handling

Bytebase Automatic Transactions: Bytebase automatically wraps all SQL statements in a migration file within a single transaction. You typically don’t need to add explicit BEGIN/COMMIT statements.
When Bytebase’s automatic transactions are sufficient:
  • Single-file migrations with multiple statements
  • Standard DDL operations (CREATE, ALTER, DROP)
  • Simple DML operations (INSERT, UPDATE, DELETE)
When you might need explicit transaction control:
  • Batched operations requiring commits between chunks
  • Long-running data migrations that need progress checkpoints
  • Statements that cannot run in a transaction block — Bytebase auto-detects the common PostgreSQL ones (CREATE INDEX CONCURRENTLY, DROP INDEX CONCURRENTLY, VACUUM, DROP DATABASE) and runs them outside the transaction for you. For others, disable transaction mode. See Statements that cannot run in a transaction block
Example of batched migration (when needed):
Database-specific transaction support:
  • ✅ PostgreSQL: DDL in transactions (except CONCURRENTLY operations)
  • ❌ MySQL: DDL commits immediately (implicit commit)
  • ✅ SQL Server: DDL in transactions

Add Comments for Complex Logic

Documentation

Maintain Migration Changelog

Document significant schema changes:

Document Schema Dependencies

Track dependencies between application and schema:
This helps coordinate schema cleanup with application deployments.

Common Anti-Patterns to Avoid

❌ Modifying Applied Migrations

Don’t:
Do:

❌ Skipping Version Numbers

Don’t:
Do:

❌ Mixing DDL and DML Without Suffix

Don’t:
Do:

❌ Long-Running Migrations in Production

Don’t:
Do:

Next Steps

Git and CI/CD

Learn branching strategies and testing patterns

Performance

Optimize migration performance