Skip to main content
Bitwarden Server uses DbUp for database schema migrations. The migration system supports multiple database providers including SQL Server, MySQL, and PostgreSQL.

Migration Architecture

Migrations are managed by the Migrator class library located in util/Migrator/.

Understanding the Migrator

The core migration logic is in util/Migrator/DbMigrator.cs:
src/Migrator/DbMigrator.cs

Key Features

  • Automatic Database Creation - Creates the database if it doesn’t exist
  • Retry Logic - Handles SQL Server upgrade mode automatically
  • Transaction Support - Wraps migrations in transactions by default
  • Dry Run Mode - Preview migrations without applying them
  • Multiple Providers - SQL Server, MySQL, PostgreSQL support

Migration Folders

Migrations are organized in the util/Migrator/DbScripts/ directory:
  • DbScripts/ - Main migration scripts (SQL Server)
  • DbScripts_transition/ - Transitional scripts for major changes
  • DbScripts_finalization/ - Post-migration cleanup scripts
  • MySql/ - MySQL-specific migrations
  • Postgres/ - PostgreSQL-specific migrations

Running Migrations

Local Development

1

Using the MsSqlMigratorUtility

2

Preview migrations (dry run)

This shows which scripts will be executed without applying them.
3

Verify migration status

Check the migration journal table:

Production Deployment

In production environments, migrations typically run:
  1. During Application Startup - Via DatabaseMigrationHostedService in the Admin service:
src/Admin/HostedServices/DatabaseMigrationHostedService.cs
  1. As Part of CI/CD - Before deploying application containers

Creating New Migrations

1

Determine migration type

Migrations are named with a timestamp prefix:
Example: 2026-03-10-14-30_AddUserPreferences.sql
2

Create the migration file

Create a new .sql file in util/Migrator/DbScripts/:
Always make migrations idempotent - they should handle cases where they’ve been partially applied.
3

Set the file as an Embedded Resource

Edit util/Migrator/Migrator.csproj to include the new script:
The wildcard pattern typically already includes all .sql files.
4

Test the migration

5

Create corresponding provider-specific migrations

If supporting multiple databases, create equivalent migrations for:
  • util/MySqlMigrations/Migrations/
  • util/PostgresMigrations/Migrations/

Migration Best Practices

Make Migrations Idempotent

Always check if changes exist before applying:

Use Transactions Appropriately

Most migrations run in transactions by default. For long-running operations, consider:

Handle Large Data Migrations

For migrations affecting millions of rows:

Maintain Backward Compatibility

When removing columns:
  1. Step 1: Deploy code that no longer uses the column
  2. Step 2: After deployment, create migration to drop the column

Document Complex Migrations

Migration Execution Order

Migrations execute in this order:
  1. DbScripts/ - Main schema changes
  2. DbScripts_transition/ - Transitional logic
  3. DbScripts_finalization/ - Cleanup and finalization
Each folder’s scripts execute alphabetically by filename (timestamp-based naming ensures chronological order).

Troubleshooting

Migration Failed Mid-Execution

If a migration fails:
  1. Check the migration journal:
  2. Manually rollback changes if needed
  3. Fix the migration script
  4. The migrator will retry on next run

Script Upgrade Mode Error

The migrator automatically retries with 20-second delays. If persistent:

Multiple Database Providers Out of Sync

Ensure all provider migrations are equivalent:
  • util/Migrator/DbMigrator.cs:15 - Core migration logic
  • util/Migrator/SqlServerDbMigrator.cs:1 - SQL Server-specific implementation
  • src/Admin/HostedServices/DatabaseMigrationHostedService.cs:1 - Production migration service

See Also