torii-migration
Database migration management for the Torii authentication framework.
This crate provides traits and utilities for managing SQL database migrations in Torii. It defines a common interface for writing migrations that can be used across different SQL database backends like SQLite, PostgreSQL, and MySQL.
Features
- Generic Migration Interface: Common traits for all database backends
- Migration Tracking: Automatic tracking of applied migrations
- Async/Await Support: Fully async migration operations
- Database Agnostic: Works with any sqlx-supported database
- Rollback Support: Bidirectional migrations with up/down operations
- Timestamp Tracking: Records when migrations were applied
Usage
This crate is primarily used internally by Torii storage backends, but can be used directly if you need custom migration management.
Add this to your Cargo.toml:
[]
= "0.4.0"
Basic Migration
use ;
use async_trait;
use Sqlite;
;
Migration Manager
use ;
use SqlitePool;
// Implement MigrationManager for your specific database
Core Traits
Migration
The Migration trait defines a single database migration:
MigrationManager
The MigrationManager trait manages the execution and tracking of migrations:
Migration Tracking
Migrations are tracked in a database table (default name: _torii_migrations) that records:
- version: The migration version number
- name: Human-readable migration name
- applied_at: Timestamp when the migration was applied
Version Numbering
It's recommended to use timestamp-based version numbers for migrations:
// Format: YYYYMMDD_HHMMSS
20250101_000001 // 2025-01-01 00:00:01
20250101_120000 // 2025-01-01 12:00:00
This ensures migrations are applied in the correct order and prevents conflicts.
Database Support
This crate works with any database supported by sqlx:
- SQLite: Great for development and smaller deployments
- PostgreSQL: Production-ready with full feature support
- MySQL: Production-ready with full feature support
Error Handling
The crate provides comprehensive error handling with the MigrationError type that integrates with Torii's error system:
Integration with Storage Backends
Storage backends like torii-storage-sqlite and torii-storage-postgres use this crate to provide automatic schema management. Users typically don't need to interact with this crate directly unless they're implementing custom storage backends.