mobc_sqlx/
migrator.rs

1use mobc::{Pool, async_trait};
2use sqlx::{
3    Database,
4    migrate::{Migrate, Migrator},
5};
6
7use crate::SqlxConnectionManager;
8
9#[async_trait]
10pub trait SqlxMigrationExt<DB>
11where
12    DB: Database + Sync,
13    <DB as Database>::Connection: Migrate,
14{
15    async fn migrate(
16        &self,
17        migrator: Migrator,
18    ) -> Result<(), mobc::Error<sqlx::Error>>;
19}
20
21#[async_trait]
22impl<DB> SqlxMigrationExt<DB> for Pool<SqlxConnectionManager<DB>>
23where
24    DB: Database + Sync,
25    <DB as Database>::Connection: Migrate,
26{
27    async fn migrate(
28        &self,
29        migrator: Migrator,
30    ) -> Result<(), mobc::Error<sqlx::Error>> {
31        let mut connection = self.get().await?;
32
33        migrator
34            .run_direct(&mut *connection)
35            .await
36            .map_err(sqlx::Error::from)?;
37
38        Ok(())
39    }
40}