1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use crate::MongoDb;

use super::AbstractMigrations;

mod init;
mod scripts;

#[async_trait]
impl AbstractMigrations for MongoDb {
    #[cfg(test)]
    /// Drop the database
    async fn drop_database(&self) {
        self.db().drop(None).await.ok();
    }

    /// Migrate the database
    async fn migrate_database(&self) -> Result<(), ()> {
        info!("Migrating the database.");

        let list = self
            .list_database_names(None, None)
            .await
            .expect("Failed to fetch database names.");

        if list.iter().any(|x| x == &self.1) {
            scripts::migrate_database(self).await;
        } else {
            init::create_database(self).await;
        }

        Ok(())
    }
}