Mongodb migrations management tool.
toml
[dependencies]
mongodb-migrator = "0.1.5"
```rust use anyhow::Result; use asynctrait::asynctrait; use mongodb::Database; use serde_derive::{Deserialize, Serialize}; use testcontainers::Docker;
use mongodb_migrator::migration::Migration;
async fn main() -> Result<()> { let docker = testcontainers::clients::Cli::default(); let node = docker.run(testcontainers::images::mongo::Mongo::default()); let hostport = node.gethostport(27017).unwrap(); let url = format!("mongodb://localhost:{}/", hostport); let client = mongodb::Client::withuristr(url).await.unwrap(); let db = client.database("test");
let migrations: Vec<Box<dyn Migration>> = vec![Box::new(M0 {}), Box::new(M1 {})];
mongodb_migrator::migrator::DefaultMigrator::new()
.with_conn(db.clone())
.with_migrations_vec(migrations)
.up()
.await?;
Ok(())
}
struct M0 {} struct M1 {}
impl Migration for M0 { async fn up(&self, db: Database) -> Result<()> { db.collection("users") .insert_one(bson::doc! { "name": "Batman" }, None) .await?;
Ok(())
}
}
impl Migration for M1 {
async fn up(&self, db: Database) -> Result<()> {
db.collection::
Ok(())
}
}
struct Users { name: String, } ```