7 releases
Uses new Rust 2024
new 0.2.6 | May 8, 2025 |
---|---|
0.2.5 | May 6, 2025 |
#921 in Database interfaces
317 downloads per month
Used in 2 crates
(via brokerage-db)
42KB
864 lines
Mongodb migrations management tool.
NOTE: this is a fork of mongodb_migrator
This is a (hopefully temporary) fork of the excellent work done by the Konstantin Matsiushonak (k.matushonok@gmail.com) at kakoc/mongodb_migrator.
The primary changes:
- updated all dependencies to latest, notably the mongodb driver (2.x -> 3.x)
- updated Rust version 2024
Expect this fork to go away once the dependency updates are integrated into the authoritative repo.
Setup
[dependencies]
tfiala-mongodb-migrator = "0.2.6"
Functionality
- Execute Rust based migrations
- Execute JavaScript based migrations
- Run as library
- Run as RESTful service
How to use
use anyhow::Result;
use async_trait::async_trait;
use mongodb::Database;
use serde_derive::{Deserialize, Serialize};
use testcontainers_modules::{
mongo::Mongo,
testcontainers::{runners::AsyncRunner, ContainerAsync},
};
use tfiala_mongodb_migrator::{
migration::Migration,
migrator::{
default::DefaultMigrator,
env::Env,
},
};
#[tokio::main]
async fn main() -> Result<()> {
let node = Mongo::default().start().await.unwrap();
let host_port = node.get_host_port_ipv4(27017).await.unwrap();
let url = format!("mongodb://localhost:{}/", host_port);
let client = mongodb::Client::with_uri_str(url).await.unwrap();
let db = client.database("test");
let migrations: Vec<Box<dyn Migration>> = vec![Box::new(M0 {}), Box::new(M1 {})];
DefaultMigrator::new()
.with_conn(db.clone())
.with_migrations_vec(migrations)
.up()
.await?;
Ok(())
}
struct M0 {}
struct M1 {}
#[async_trait]
impl Migration for M0 {
async fn up(&self, env: Env) -> Result<()> {
let db = env.db().await?;
db.collection("users")
.insert_one(bson::doc! { "name": "Batman" })
.await?;
Ok(())
}
}
#[async_trait]
impl Migration for M1 {
async fn up(&self, env: Env) -> Result<()> {
let db = env.db().await?;
db.collection::<Users>("users")
.update_one(
bson::doc! { "name": "Batman" },
bson::doc! { "$set": { "name": "Superman" } },
)
.await?;
Ok(())
}
}
#[derive(Serialize, Deserialize)]
struct Users {
name: String,
}
Roadmap
- Rust based migrations
- JavaScript based migrations
- Logging
- Rollbacks
- Cli tool
- UI dashboard
- RESTful service
- As npm package
- Stragegies
- Fail first
- Try all
- Retries
Dependencies
~22–34MB
~542K SLoC