/// Execute SQL queries in a transaction for MySQL.
Execute SQL queries in a transaction for MySQL.
Execute SQL queries in a transaction for MySQL .
async fn with_transaction(
        &self,
        pool: &MySqlPool,
        fixture_files: &[FixtureFile<Tz>],
    ) -> anyhow::Result<()> {
        let mut tx = pool.begin().await?;
        let mut queries = vec![];
        let delete_queries: Vec<String> = fixture_files.iter().map(|x| (x.delete())).collect();
        let mut delete_queries: Vec<Query<'_, M>> =
            delete_queries.iter().map(|x| sqlx::query(x)).collect();
        queries.append(&mut delete_queries);
        for fixtures_file in fixture_files {
            for sql in &fixtures_file.insert_sqls {
                let mut args = MySqlArguments::default();
                for param in &sql.params {
                    match param {
                        SqlParam::String(param) => args.add(param),
                        SqlParam::Integer(param) => args.add(param),
                        SqlParam::Datetime(param) => args.add(param.naive_local()),
                        SqlParam::Float(param) => args.add(param),
                        SqlParam::Boolean(param) => args.add(param),
                    }
                }
                queries.push(sqlx::query(sql.sql.as_str()).bind_all(args))
            }
        }
        let result: Result<u64, Error> = async {
            sqlx::query("SET FOREIGN_KEY_CHECKS = 0")
                .execute(&mut tx)
                .await?;
            for query in queries {
                let result = query.execute(&mut tx).await;
                if result.is_err() {
                    return result;
                }
            }
            sqlx::query("SET FOREIGN_KEY_CHECKS = 1")
                .execute(&mut tx)
                .await?;
            Ok(1)
        }
        .await;
        match result {
            Ok(_) => {
                tx.commit().await?;
            }
            Err(err) => {
                tx.rollback().await?;
                return Err(anyhow::anyhow!("testfixtures: {}", err));
            }
        };
        Ok(())
    }
async fn with_transaction ( & self , pool : & MySqlPool , fixture_files : & [ FixtureFile < Tz > ] , ) -> anyhow :: Result < ( ) > { let mut tx = pool . begin ( ) . await ? ; let mut queries = vec ! [ ] ; let delete_queries : Vec < String > = fixture_files . iter ( ) . map ( | x | ( x . delete ( ) ) ) . collect ( ) ; let mut delete_queries : Vec < Query < ' _ , M > > = delete_queries . iter ( ) . map ( | x | sqlx :: query ( x ) ) . collect ( ) ; queries . append ( & mut delete_queries ) ; for fixtures_file in fixture_files { for sql in & fixtures_file . insert_sqls { let mut args = MySqlArguments :: default ( ) ; for param in & sql . params { match param { SqlParam :: String ( param ) => args . add ( param ) , SqlParam :: Integer ( param ) => args . add ( param ) , SqlParam :: Datetime ( param ) => args . add ( param . naive_local ( ) ) , SqlParam :: Float ( param ) => args . add ( param ) , SqlParam :: Boolean ( param ) => args . add ( param ) , } } queries . push ( sqlx :: query ( sql . sql . as_str ( ) ) . bind_all ( args ) ) } } let result : Result < u64 , Error > = async { sqlx :: query ( " " ) . execute ( & mut tx ) . await ? ; for query in queries { let result = query . execute ( & mut tx ) . await ; if result . is_err ( ) { return result ; } } sqlx :: query ( " " ) . execute ( & mut tx ) . await ? ; Ok ( 1 ) } . await ; match result { Ok ( _ ) => { tx . commit ( ) . await ? ; } Err ( err ) => { tx . rollback ( ) . await ? ; return Err ( anyhow :: anyhow ! ( " " err ) ) ; } } ; Ok ( ( ) ) }
Execute SQL queries in a transaction for MySQL.
Execute SQL queries in a transaction for MySQL .
&self
pool
&MySqlPool
fixture_files
&[FixtureFile<Tz>]