| Crates.io | migratex |
| lib.rs | migratex |
| version | 0.2.2 |
| created_at | 2025-11-16 01:46:09.087133+00 |
| updated_at | 2025-11-25 02:17:50.046198+00 |
| description | Agnostic migration toolkit library. |
| homepage | |
| repository | https://github.com/nicolab/migratex |
| max_upload_size | |
| id | 1935081 |
| size | 141,735 |
Migratex is an agnostic migration toolkit library.
Migrate anything! Anywhere! 🚀
💪 It can be used to migrate database / data / files / binaries, etc from one version to another.
Simple and intuitive API: migrate_up, migrate_down, migrate_to, migrate_to_latest, migrate_to_zero, latest_version, metadata, etc.
use migratex::{JsonMetadata, Migratex};
// Load or initialize metadata
let mut meta = JsonMetadata::load_or_init("metadata.json")?;
// Run migrations
let mut mx = Migratex::new(&mut ctx, &mut meta, migrations);
mx.migrate_to_latest().await?;
// Save metadata
meta.save("metadata.json")?;
use std::sync::Arc;
use std::path::PathBuf;
use migratex::{SqliteMetadata, SqliteStorage, connect_to_sqlite, Migratex};
// Connect to database
let pool = connect_to_sqlite(PathBuf::from("app.db")).await?;
let storage = SqliteStorage::new(Arc::new(pool));
// Load or initialize metadata
let mut meta = SqliteMetadata::load_or_init(&storage).await?;
// Run migrations
let mut mx = Migratex::new(&mut ctx, &mut meta, migrations);
mx.migrate_to_latest().await?;
// Save metadata
meta.save(&storage).await?;
Look at the examples:
Add this to your Cargo.toml:
[dependencies]
migratex = "*"
Put the latest version of
migratexin yourCargo.toml!
Enable the json feature for JSON file-based metadata storage:
[dependencies]
migratex = { version = "*", features = ["json"] }
This provides the JsonMetadata struct for storing metadata in a JSON file.
Enable the sqlx feature for SQLite database metadata storage:
[dependencies]
migratex = { version = "*", features = ["sqlx"] }
Put the latest version of
migratexin yourCargo.toml!
This provides:
SqliteMetadata - Metadata stored in a SQLite tableSqliteStorage - Storage configurationconnect_to_sqlite() - Helper function to connect to SQLite databaseNote: Other database drivers can be implemented by implementing the
Metadatatrait (look at SQLite implementation for inspiration).
You can implement your own metadata storage by implementing the Metadata trait:
use migratex::{Metadata, MetaStatus};
#[derive(Debug, Clone)]
pub struct CustomMetadata {
pub version: i32,
pub status: MetaStatus,
pub app_version: String,
pub created_at: String,
pub updated_at: String,
}
impl CustomMetadata {
pub fn load_or_init(path: impl AsRef<Path>) -> Result<Self> {
// Your custom implementation
}
pub fn save(&self, path: impl AsRef<Path>) -> Result<()> {
// Your custom implementation
}
}
impl Metadata for CustomMetadata {
migratex::metadata_accessors!();
}
See the custom example for a complete implementation.
Run all tests:
cargo test --all-features
okerr is used for error handling (100% compatible with anyhow), this should work properly with any error-handling library.MIT (c) 2025, Nicolas Talle.
Buy me a coffee ☕ via PayPal!