mongodb_migrator/lib.rs
1//! This crate provides a convinient way of how to manage migrations.
2//! It's inteded to be used as a library:
3//! You maintain all migrations by your own via implementing [`migration::Migration`] trait
4//! and pass a sequence of migrations to the [`migrator::Migrator`] on every bootstrap of your system
5//!
6//! # Example
7//!
8//! ```
9//! use anyhow::Result;
10//! use async_trait::async_trait;
11//! use mongodb::Database;
12//! use serde_derive::{Deserialize, Serialize};
13//! use testcontainers::Docker;
14//!
15//! use mongodb_migrator::{migration::Migration, migrator::Env};
16//!
17//! #[tokio::main]
18//! async fn main() -> Result<()> {
19//! let docker = testcontainers::clients::Cli::default();
20//! let node = docker.run(testcontainers::images::mongo::Mongo::default());
21//! let host_port = node.get_host_port(27017).unwrap();
22//! let url = format!("mongodb://localhost:{}/", host_port);
23//! let client = mongodb::Client::with_uri_str(url).await.unwrap();
24//! let db = client.database("test");
25//!
26//! let migrations: Vec<Box<dyn Migration>> = vec![Box::new(M0 {}), Box::new(M1 {})];
27//! mongodb_migrator::migrator::default::DefaultMigrator::new()
28//! .with_conn(db.clone())
29//! .with_migrations_vec(migrations)
30//! .up()
31//! .await?;
32//!
33//! Ok(())
34//! }
35//!
36//! struct M0 {}
37//! struct M1 {}
38//!
39//! #[async_trait]
40//! impl Migration for M0 {
41//! async fn up(&self, env: Env) -> Result<()> {
42//! env.db.expect("db is available").collection("users")
43//! .insert_one(bson::doc! { "name": "Batman" }, None)
44//! .await?;
45//!
46//! Ok(())
47//! }
48//! }
49//!
50//! #[async_trait]
51//! impl Migration for M1 {
52//! async fn up(&self, env: Env) -> Result<()> {
53//! env.db.expect("db is available").collection::<Users>("users")
54//! .update_one(
55//! bson::doc! { "name": "Batman" },
56//! bson::doc! { "$set": { "name": "Superman" } },
57//! None,
58//! )
59//! .await?;
60//!
61//! Ok(())
62//! }
63//! }
64//!
65//! #[derive(Serialize, Deserialize)]
66//! struct Users {
67//! name: String,
68//! }
69//! ```
70
71pub mod error;
72pub mod migration;
73pub mod migration_record;
74pub mod migration_status;
75pub mod migrator;
76pub mod server;