Load data into SQL tables.
This commit is contained in:
parent
5fb2cc9f55
commit
0c6cff77d5
2 changed files with 300089 additions and 12 deletions
38
src/lib.rs
38
src/lib.rs
|
@ -1,41 +1,55 @@
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use mysql_async::{Conn, prelude::*};
|
||||||
|
|
||||||
|
pub async fn load_data(conn: &mut Conn) -> Result<(), Box<dyn Error>> {
|
||||||
|
// Obviously use something better/more robust here if you're a) loading
|
||||||
|
// other data sources and b) aren't so lucky as to have a single `;` after
|
||||||
|
// each block of SQL. :)
|
||||||
|
let data = String::from_utf8_lossy(include_bytes!("../data.sql"));
|
||||||
|
for stmt in data.split(";") {
|
||||||
|
if !stmt.is_empty() {
|
||||||
|
conn.exec_drop(stmt, params::Params::Empty).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use derive_more::{Deref, DerefMut};
|
use derive_more::{Deref, DerefMut};
|
||||||
use mysql_async::Opts;
|
|
||||||
use testcontainers_modules::testcontainers::ContainerAsync;
|
use testcontainers_modules::testcontainers::ContainerAsync;
|
||||||
|
|
||||||
// This is a bit YOLO but is the quickest/cleanest way I can think of to a) get
|
// This is a bit YOLO but is the quickest/cleanest way I can think of to a) get
|
||||||
// a connection per database and b) ensure `Drop` cleans it up.
|
// a connection per database and b) ensure `Drop` cleans it up.
|
||||||
#[derive(Deref, DerefMut)]
|
#[derive(Deref, DerefMut)]
|
||||||
struct TestPool(
|
struct TestConnection(
|
||||||
#[deref]
|
#[deref]
|
||||||
#[deref_mut]
|
#[deref_mut]
|
||||||
mysql_async::Pool,
|
mysql_async::Conn,
|
||||||
// Never read, only needed for `Drop`.
|
// Never read, only needed for `Drop`.
|
||||||
#[allow(dead_code)] ContainerAsync<testcontainers_modules::mysql::Mysql>,
|
#[allow(dead_code)] ContainerAsync<testcontainers_modules::mysql::Mysql>,
|
||||||
);
|
);
|
||||||
|
|
||||||
async fn get_test_pool() -> Result<TestPool, Box<dyn Error>> {
|
async fn get_test_connection() -> Result<TestConnection, Box<dyn Error>> {
|
||||||
use testcontainers_modules::{mysql, testcontainers::runners::AsyncRunner};
|
use testcontainers_modules::{mysql, testcontainers::runners::AsyncRunner};
|
||||||
let mysql = mysql::Mysql::default().start().await?;
|
let mysql = mysql::Mysql::default().start().await?;
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"mysql://{}:{}/test",
|
"mysql://root@{}:{}/test",
|
||||||
mysql.get_host().await?,
|
mysql.get_host().await?,
|
||||||
mysql.get_host_port_ipv4(3306).await?
|
mysql.get_host_port_ipv4(3306).await?
|
||||||
);
|
);
|
||||||
let opts = Opts::from_url(&url)?;
|
let conn = mysql_async::Conn::from_url(&url).await?;
|
||||||
let pool = mysql_async::Pool::new(opts);
|
Ok(TestConnection(conn, mysql))
|
||||||
Ok(TestPool(pool, mysql))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_mysql() -> Result<(), Box<dyn std::error::Error>> {
|
async fn test_mysql() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let pool = get_test_pool().await?;
|
let mut conn = get_test_connection().await?;
|
||||||
<mysql_async::Pool as Clone>::clone(&pool)
|
crate::load_data(&mut conn).await?;
|
||||||
.disconnect()
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue