pub async fn migrate(&mut self, version: u64) -> Result<MigrationSummary, Error> {
        self.local_migration(version)?;

        self.check_migrations().await?;

        let db_migrations = self.conn.list_migrations(&self.table).await?;

        let to_apply = self
            .migrations
            .iter()
            .enumerate()
            .skip_while(|(idx, _)| *idx < db_migrations.len())
            .take_while(|(idx, _)| *idx < version as _);

        let mut tx = self.conn.begin().await?;

        let version = version.max(db_migrations.len() as _);

        for (idx, mig) in to_apply {
            let version = idx as u64 + 1;

            let start = Instant::now();

            tracing::info!(
                version,
                name = %mig.name,
                "applying migration"
            );

            (*mig.up)(&mut tx).await.map_err(|error| Error::Migration {
                name: mig.name.clone(),
                version,
                error,
            })?;

            let execution_time = Instant::now() - start;

            DB::Connection::add_migration(
                &self.table,
                AppliedMigration {
                    version,
                    name: mig.name.clone(),
                    checksum: mig.checksum.clone(),
                    execution_time,
                },
                &mut tx,
            )
            .await?;

            tracing::info!(
                version,
                name = %mig.name,
                execution_time = %humantime::Duration::from(execution_time),
                "migration applied"
            );
        }

        tracing::info!("committing changes");
        tx.commit().await?;

        Ok(MigrationSummary {
            old_version: if db_migrations.is_empty() {
                None
            } else {
                Some(db_migrations.len() as _)
            },
            new_version: Some(version),
        })
    }
/// Apply all migrations to the given version.
///
/// Migration versions start at 1 and migrations are ordered
/// the way they were added to the migrator.
///
/// # Errors
///
/// Whenever a migration fails, and error is returned and no database
/// changes will be made.
Apply all migrations to the given version.
Migration versions start at 1 and migrations are ordered
the way they were added to the migrator.
Apply all migrations to the given version . Migration versions start at 1 and migrations are ordered the way they were added to the migrator .
pub async fn migrate(&mut self, version: u64) -> Result<MigrationSummary, Error> {
        self.local_migration(version)?;
        self.check_migrations().await?;
        let db_migrations = self.conn.list_migrations(&self.table).await?;
        let to_apply = self
            .migrations
            .iter()
            .enumerate()
            .skip_while(|(idx, _)| *idx < db_migrations.len())
            .take_while(|(idx, _)| *idx < version as _);
        let mut tx = self.conn.begin().await?;
        let version = version.max(db_migrations.len() as _);
        for (idx, mig) in to_apply {
            let version = idx as u64 + 1;
            let start = Instant::now();
            tracing::info!(
                version,
                name = %mig.name,
                "applying migration"
            );
            (*mig.up)(&mut tx).await.map_err(|error| Error::Migration {
                name: mig.name.clone(),
                version,
                error,
            })?;
            let execution_time = Instant::now() - start;
            DB::Connection::add_migration(
                &self.table,
                AppliedMigration {
                    version,
                    name: mig.name.clone(),
                    checksum: mig.checksum.clone(),
                    execution_time,
                },
                &mut tx,
            )
            .await?;
            tracing::info!(
                version,
                name = %mig.name,
                execution_time = %humantime::Duration::from(execution_time),
                "migration applied"
            );
        }
        tracing::info!("committing changes");
        tx.commit().await?;
        Ok(MigrationSummary {
            old_version: if db_migrations.is_empty() {
                None
            } else {
                Some(db_migrations.len() as _)
            },
            new_version: Some(version),
        })
    }
pub async fn migrate ( & mut self , version : u64 ) -> Result < MigrationSummary , Error > { self . local_migration ( version ) ? ; self . check_migrations ( ) . await ? ; let db_migrations = self . conn . list_migrations ( & self . table ) . await ? ; let to_apply = self . migrations . iter ( ) . enumerate ( ) . skip_while ( | ( idx , _ ) | * idx < db_migrations . len ( ) ) . take_while ( | ( idx , _ ) | * idx < version as _ ) ; let mut tx = self . conn . begin ( ) . await ? ; let version = version . max ( db_migrations . len ( ) as _ ) ; for ( idx , mig ) in to_apply { let version = idx as u64 + 1 ; let start = Instant :: now ( ) ; tracing :: info ! ( version name mig name " " ) ; ( * mig . up ) ( & mut tx ) . await . map_err ( | error | Error :: Migration { name : mig . name . clone ( ) , version , error , } ) ? ; let execution_time = Instant :: now ( ) - start ; DB :: Connection :: add_migration ( & self . table , AppliedMigration { version , name : mig . name . clone ( ) , checksum : mig . checksum . clone ( ) , execution_time , } , & mut tx , ) . await ? ; tracing :: info ! ( version name mig name execution_time humantime Duration from ( execution_time ) " " ) ; } tracing :: info ! ( " " ) ; tx . commit ( ) . await ? ; Ok ( MigrationSummary { old_version : if db_migrations . is_empty ( ) { None } else { Some ( db_migrations . len ( ) as _ ) } , new_version : Some ( version ) , } ) }
Apply all migrations to the given version.
Apply all migrations to the given version .
&mut self
version
u64
&mut self
version
u64
errors
Whenever a migration fails, and error is returned and no database
changes will be made.
Whenever a migration fails and error is returned and no database changes will be made .