Skip to content

Commit

Permalink
Experiment with transfering via tar archives
Browse files Browse the repository at this point in the history
  • Loading branch information
Jb DOYON committed Apr 26, 2024
1 parent 713aa59 commit ac0fc34
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/testcontainers/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def start(self) -> "DbContainer":
self._configure()
super().start()
self._connect()
self._transfer_seed()
return self

def _configure(self) -> None:
raise NotImplementedError

def _transfer_seed(self) -> None:
pass
14 changes: 13 additions & 1 deletion modules/mysql/testcontainers/mysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import re
import tarfile
from io import BytesIO
from os import environ
from pathlib import Path
from typing import Optional

from testcontainers.core.generic import DbContainer
Expand Down Expand Up @@ -85,7 +88,7 @@ def __init__(
if self.username == "root":
self.root_password = self.password
if seed is not None:
self.with_volume_mapping(seed, "/docker-entrypoint-initdb.d/")
self.seed = seed

def _configure(self) -> None:
self.with_env("MYSQL_ROOT_PASSWORD", self.root_password)
Expand All @@ -105,3 +108,12 @@ def get_connection_url(self) -> str:
return super()._create_connection_url(
dialect="mysql+pymysql", username=self.username, password=self.password, dbname=self.dbname, port=self.port
)

def _transfer_seed(self) -> None:
src_path = Path(self.seed)
dest_path = "/docker-entrypoint-initdb.d/"
with BytesIO() as archive, tarfile.TarFile(fileobj=archive, mode="w") as tar:
for filename in src_path.iterdir():
tar.add(filename.absolute(), arcname=filename.relative_to(src_path))
archive.seek(0)
self.get_wrapped_container().put_archive(dest_path, archive)

0 comments on commit ac0fc34

Please sign in to comment.