I am trying to copy a known list of MySql tables from one db to another. Because there is a large list of tables, and I will also need to insert large amounts of data into each table as part of the copy, I would like to use threads to make things a little faster. I think I need to use scoped threads in this case because I need the thread to own its table(s).
I have used scoped threads before, but in a much simpler context. In this case I am having difficulty because I am trying to use sqlx::query
which implements futures::Future
, but I cannot use .await
in a scoped thread. I also suspect my use of .clone()
is probably too liberal.
I honestly don't know how to handle this, or if what I want to do is even possible.
pub async fn make_tables_and_copy(tables: Vec<String>, connection: sqlx::MySqlPool, schema: &String) -> std::io::Result<()> {
...
thread::scope(|s| {
let mut queries = Vec::new();
for table in tables.clone() {
let conn = connection.clone();
queries.push(s.spawn(move |_| {
sqlx::query(
format!("CREATE TABLE IF NOT EXISTS {}.{} LIKE {}.{};", schema, table, HUB, table).as_str()
)
.fetch_all(&conn);
}));
}
for query in queries {
query.join().unwrap();
}
}).unwrap();
Resulting error:
warning: unused implementer of `futures::Future` that must be used
--> src/sql.rs:104:6
|
104 | / sqlx::query(
105 | | format!("CREATE TABLE IF NOT EXISTS {}.{} LIKE {}.{};", schema, table, HUB, table).as_str()
106 | | )
107 | | .fetch_all(&conn);
| |_____________________________________^
|
= note: futures do nothing unless you `.await` or poll them
= note: `#[warn(unused_must_use)]` on by default
If this is a terrible way to do this, I would greatly appreciate an alternative route. I'm still quite green in the Rust world.