Expand description
This crate provides a convinient way of how to manage migrations.
It’s inteded to be used as a library:
You maintain all migrations by your own via implementing migration::Migration trait
and pass a sequence of migrations to the migrator::Migrator on every bootstrap of your system
§Example
use anyhow::Result;
use async_trait::async_trait;
use mongodb::Database;
use serde_derive::{Deserialize, Serialize};
use tfiala_mongodb_migrator::{migration::Migration, migrator::Env};
use testcontainers_modules::{
mongo::Mongo,
testcontainers::{runners::AsyncRunner, ContainerAsync},
};
#[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 {})];
tfiala_mongodb_migrator::migrator::default::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<()> {
env.db.expect("db is available").collection("users")
.insert_one(bson::doc! { "name": "Batman" })
.await?;
Ok(())
}
}
#[async_trait]
impl Migration for M1 {
async fn up(&self, env: Env) -> Result<()> {
env.db.expect("db is available").collection::<Users>("users")
.update_one(
bson::doc! { "name": "Batman" },
bson::doc! { "$set": { "name": "Superman" } },
)
.await?;
Ok(())
}
}
#[derive(Serialize, Deserialize)]
struct Users {
name: String,
}Modules§
- error
- migration
- In order to treat the entity as migrationable it should implement
Migrationtrait - migration_
record MigrationRecorddescribes the document which will be stored in the migrations collection.
It contains all useful attributes which might be used in order to understand the current state of a particular migration- migration_
status - Describes a migration status
- migrator
- Migrator runs passed migrations - entities which implement [
Migration] trait - server