MoosicBox Schema
Database migration system for the MoosicBox ecosystem, providing automated SQL migration management for PostgreSQL and SQLite databases with version tracking and rollback support.
Features
- Database Migrations: Run SQL migration files automatically
- Version Tracking: Track applied migrations in a dedicated table
- Multi-Database Support: Support for both PostgreSQL and SQLite
- Migration Organization: Organize migrations by library/config categories
- Partial Migration: Run migrations up to a specific version
- Error Handling: Basic error handling for migration failures
Installation
Add this to your Cargo.toml:
[]
= "0.1.1"
# Enable database-specific features
= { = "0.1.1", = ["sqlite", "postgres"] }
Usage
Running Library Migrations
use ;
use Database;
async
Running Config Migrations
use migrate_config;
// Run configuration-related migrations
migrate_config.await?;
Partial Migrations
use migrate_library_until;
// Run migrations up to a specific version
migrate_library_until.await?;
Migration Structure
Migrations are organized in a specific directory structure:
migrations/
├── sqlite/
│ ├── library/
│ │ ├── 20231201_initial_schema/
│ │ │ └── up.sql
│ │ └── 20231202_add_indexes/
│ │ └── up.sql
│ └── config/
│ └── 20231201_config_tables/
│ └── up.sql
└── postgres/
├── library/
│ └── 20231201_initial_schema/
│ └── up.sql
└── config/
└── 20231201_config_tables/
└── up.sql
Custom Migration Runner
use Migrations;
use ;
// Include your migration directory at compile time
static MY_MIGRATIONS: Dir = include_dir!;
// Create a migrations instance
let migrations = Migrations ;
// Run all migrations
migrations.run.await?;
// Or run up to a specific migration
migrations.run_until.await?;
Error Handling
use MigrateError;
match migrate_library.await
Migration Tracking
The system automatically creates a __moosicbox_schema_migrations table to track which migrations have been applied:
(
name TEXT NOT NULL PRIMARY KEY,
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Supported Databases
- SQLite: Via the
sqlitefeature flag - PostgreSQL: Via the
postgresfeature flag
Each database type has its own set of migration files optimized for that specific database system.
Dependencies
switchy_database: Database abstraction layerinclude_dir: Compile-time directory inclusion for migration filesthiserror: Error handling utilities
Error Types
MigrateError: Wraps database errors that occur during migration execution
The migration system ensures your database schema stays up-to-date and consistent across different environments.