| name | servicestack-schema-migrations |
| description | Manages schema evolution using OrmLite migrations and deployment-safe patterns. |
ServiceStack Schema Migrations
ServiceStack recommended approach for database schema changes is code-first migrations.
Basic Migration Structure
- Create classes that inherit from
MigrationBase.
- Implement the
Up and Down methods.
- Use
Db.CreateTable<T>, Db.DropTable<T>, Db.AddColumn<T>, etc.
public class Migration1000 : MigrationBase
{
public override void Up()
{
Db.CreateTable<Customer>();
}
public override void Down()
{
Db.DropTable<Customer>();
}
}
Running Migrations
- Migrations can be run from the command line using
dotnet run -- AppTasks=migrate.
- They are typically stored in a
Migrations folder in the AppHost project.
Conventions
- Naming: Use a sequential or timestamp-based naming convention (e.g.,
Migration1000, Migration20231027_Initial).
- Declarative: Use declarative migrations (
Db.CreateTable<T>) where possible to keep POCOs as the source of truth.
- Imperative: Use
Db.ExecuteSql("...") if you need complex transformations or custom SQL that OrmLite doesn't support directly.
Best Practices
- Never Update Old Migrations: Once a migration is committed and shared, never change it. Create a new one.
- Test Rollbacks: Always implement and test the
Down method.
- Idempotency: Ensure your migrations are safe to run even if some parts of the schema already exist (though ServiceStack's migration runner handles the tracking).
- Deployment: Integrate migration execution into your CI/CD pipeline or application startup (with care).