diff --git a/src/lib.rs b/src/lib.rs index ebd28bc..db87f0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,6 +145,7 @@ pub mod zookeeper; /// Re-exported version of `testcontainers` to avoid version conflicts pub use testcontainers; +use testcontainers::CopyDataSource; #[cfg(any(feature = "postgres", feature = "mariadb", feature = "mysql"))] #[cfg_attr( @@ -158,17 +159,20 @@ pub trait InitSql { /// # Example /// /// ``` + /// # #[cfg(feature = "postgres")] + /// # { /// # use testcontainers_modules::postgres::Postgres; /// # use testcontainers_modules::InitSql; - /// let postgres_image = - /// Postgres::default().with_init_sql("CREATE EXTENSION IF NOT EXISTS hstore;"); + /// let postgres_image = Postgres::default() + /// .with_init_sql("CREATE EXTENSION IF NOT EXISTS hstore;".to_string().into_bytes()); + /// # } /// ``` /// /// ```rust,ignore /// # use testcontainers_modules::postgres::Postgres; /// # use testcontainers_modules::rdbms::InitSql; /// let postgres_image = Postgres::default() - /// .with_init_sql(include_str!("path_to_init.sql")); + /// .with_init_sql(include_str!("path_to_init.sql").to_string().into_bytes()); /// ``` - fn with_init_sql(self, init_sql: impl ToString) -> Self; + fn with_init_sql(self, init_sql: impl Into) -> Self; } diff --git a/src/mariadb/mod.rs b/src/mariadb/mod.rs index 455b499..eb7ffb2 100644 --- a/src/mariadb/mod.rs +++ b/src/mariadb/mod.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use testcontainers::{core::WaitFor, CopyToContainer, Image}; +use testcontainers::{core::WaitFor, CopyDataSource, CopyToContainer, Image}; const NAME: &str = "mariadb"; const TAG: &str = "11.3"; @@ -27,7 +27,7 @@ const TAG: &str = "11.3"; /// [`MariaDB docker image`]: https://hub.docker.com/_/mariadb #[derive(Debug, Default, Clone)] pub struct Mariadb { - init_sqls: Vec, + copy_to_sources: Vec, } impl Image for Mariadb { @@ -55,17 +55,16 @@ impl Image for Mariadb { ] } fn copy_to_sources(&self) -> impl IntoIterator { - &self.init_sqls + &self.copy_to_sources } } impl crate::InitSql for Mariadb { - fn with_init_sql(mut self, init_sql: impl ToString) -> Self { - let init_vec = init_sql.to_string().into_bytes(); + fn with_init_sql(mut self, init_sql: impl Into) -> Self { let target = format!( "/docker-entrypoint-initdb.d/init_{i}.sql", - i = self.init_sqls.len() + i = self.copy_to_sources.len() ); - self.init_sqls.push(CopyToContainer::new(init_vec, target)); + self.copy_to_sources.push(CopyToContainer::new(init_sql.into(), target)); self } } @@ -83,7 +82,7 @@ mod tests { fn mariadb_with_init_sql() -> Result<(), Box> { use crate::InitSql; let node = MariadbImage::default() - .with_init_sql("CREATE TABLE foo (bar varchar(255));") + .with_init_sql("CREATE TABLE foo (bar varchar(255));".to_string().into_bytes()) .start()?; let connection_string = &format!( @@ -93,10 +92,10 @@ mod tests { ); let mut conn = mysql::Conn::new(mysql::Opts::from_url(connection_string).unwrap()).unwrap(); - let rows = conn.query("INSERT INTO foo(bar) VALUES ('blub')").unwrap(); + let rows:Vec = conn.query("INSERT INTO foo(bar) VALUES ('blub')").unwrap(); assert_eq!(rows.len(), 0); - let rows = conn.query("SELECT bar FROM foo").unwrap(); + let rows:Vec = conn.query("SELECT bar FROM foo").unwrap(); assert_eq!(rows.len(), 1); Ok(()) } diff --git a/src/mysql/mod.rs b/src/mysql/mod.rs index e6e06ef..510c04a 100644 --- a/src/mysql/mod.rs +++ b/src/mysql/mod.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use testcontainers::{core::WaitFor, CopyToContainer, Image}; +use testcontainers::{core::WaitFor, CopyDataSource, CopyToContainer, Image}; const NAME: &str = "mysql"; const TAG: &str = "8.1"; @@ -27,7 +27,7 @@ const TAG: &str = "8.1"; /// [`MySQL docker image`]: https://hub.docker.com/_/mysql #[derive(Debug, Default, Clone)] pub struct Mysql { - init_sqls: Vec, + copy_to_sources: Vec, } impl Image for Mysql { @@ -55,17 +55,16 @@ impl Image for Mysql { ] } fn copy_to_sources(&self) -> impl IntoIterator { - &self.init_sqls + &self.copy_to_sources } } impl crate::InitSql for Mysql { - fn with_init_sql(mut self, init_sql: impl ToString) -> Self { - let init_vec = init_sql.to_string().into_bytes(); + fn with_init_sql(mut self, init_sql: impl Into) -> Self { let target = format!( "/docker-entrypoint-initdb.d/init_{i}.sql", - i = self.init_sqls.len() + i = self.copy_to_sources.len() ); - self.init_sqls.push(CopyToContainer::new(init_vec, target)); + self.copy_to_sources.push(CopyToContainer::new(init_sql.into(), target)); self } } @@ -84,7 +83,7 @@ mod tests { fn mysql_with_init_sql() -> Result<(), Box> { use crate::InitSql; let node = crate::mysql::Mysql::default() - .with_init_sql("CREATE TABLE foo (bar varchar(255));") + .with_init_sql("CREATE TABLE foo (bar varchar(255));".to_string().into_bytes()) .start()?; let connection_string = &format!( @@ -94,10 +93,10 @@ mod tests { ); let mut conn = mysql::Conn::new(mysql::Opts::from_url(connection_string).unwrap()).unwrap(); - let rows = conn.query("INSERT INTO foo(bar) VALUES ('blub')").unwrap(); + let rows:Vec = conn.query("INSERT INTO foo(bar) VALUES ('blub')").unwrap(); assert_eq!(rows.len(), 0); - let rows = conn.query("SELECT bar FROM foo").unwrap(); + let rows:Vec = conn.query("SELECT bar FROM foo").unwrap(); assert_eq!(rows.len(), 1); Ok(()) } diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index 3642cd1..9545d62 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -1,9 +1,6 @@ use std::{borrow::Cow, collections::HashMap}; -use testcontainers::{ - core::{Mount, WaitFor}, - CopyToContainer, Image, -}; +use testcontainers::{core::WaitFor, CopyDataSource, CopyToContainer, Image}; const NAME: &str = "postgres"; const TAG: &str = "11-alpine"; @@ -33,7 +30,7 @@ const TAG: &str = "11-alpine"; #[derive(Debug, Clone)] pub struct Postgres { env_vars: HashMap, - init_sqls: Vec, + copy_to_sources: Vec, } impl Postgres { @@ -67,13 +64,12 @@ impl Postgres { } } impl crate::InitSql for Postgres { - fn with_init_sql(mut self, init_sql: impl ToString) -> Self { - let init_vec = init_sql.to_string().into_bytes(); + fn with_init_sql(mut self, init_sql: impl Into) -> Self { let target = format!( "/docker-entrypoint-initdb.d/init_{i}.sql", - i = self.init_sqls.len() + i = self.copy_to_sources.len() ); - self.init_sqls.push(CopyToContainer::new(init_vec, target)); + self.copy_to_sources.push(CopyToContainer::new(init_sql.into(), target)); self } } @@ -87,7 +83,7 @@ impl Default for Postgres { Self { env_vars, - init_sqls: Vec::new(), + copy_to_sources: Vec::new(), } } } @@ -114,7 +110,7 @@ impl Image for Postgres { &self.env_vars } fn copy_to_sources(&self) -> impl IntoIterator { - &self.init_sqls + &self.copy_to_sources } } @@ -170,7 +166,7 @@ mod tests { fn postgres_with_init_sql() -> Result<(), Box> { use crate::InitSql; let node = Postgres::default() - .with_init_sql("CREATE TABLE foo (bar varchar(255));") + .with_init_sql("CREATE TABLE foo (bar varchar(255));".to_string().into_bytes()) .start()?; let connection_string = &format!(