使用Scala的testContainers Postgres执行初始脚本

发布于 2025-02-12 09:59:09 字数 290 浏览 1 评论 0 原文

我正在尝试使用 testContainers-scala-postgresql 使用TestContainers,PostgreSQL和Scala旋转一些测试。我想在容器启动期间运行一个初始化脚本,以创建和填充表。

但是, com.dimafeng.testcontainers.postgresqlcontainer type类型不包含 interscript 方法,该方法中存在于Java版本中。

在启动期间,我还有其他方法可以配置init脚本的执行吗?

I'm trying to use the testcontainers-scala-postgresql to spin up some tests using Testcontainers, PostgreSQL and Scala. I want to run an init script during container startup to create and populate the table.

However, the com.dimafeng.testcontainers.PostgreSQLContainer type doesn't contain the withInitScript method, which is present in the Java version.

Is there any other way I can configure the execution of an init script during startup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2025-02-19 09:59:09

您可以使用postgresqlcontainer.def,在其中可以通过InitscriptPath通过CommonjDbcParams:

  val initScriptParam = JdbcDatabaseContainer.CommonParams(initScriptPath = Option("init_script.sql"))
  val postgresqlContainer: PostgreSQLContainer = PostgreSQLContainer.Def(commonJdbcParams = initScriptParam).createContainer()

You could use PostgreSQLContainer.Def, where are an ability to pass commonJdbcParams with initScriptPath:

  val initScriptParam = JdbcDatabaseContainer.CommonParams(initScriptPath = Option("init_script.sql"))
  val postgresqlContainer: PostgreSQLContainer = PostgreSQLContainer.Def(commonJdbcParams = initScriptParam).createContainer()
栩栩如生 2025-02-19 09:59:09

Postgres Image可以选择“启动”,这是/docker-entrypoint-initdb.d )。

简而言之,*。sql *。sql.gz ,或*。/docker-endrypoint-initdb下的脚本。 D 在启动时执行。

在Java版本中,您可以使用 withcopyfiletocontainer 方法,我认为Scala中应该有类似的东西吗?

因此,类似:

new PostgreSQLContainer<>("postgres:14-alpine")
  .withCopyFileToContainer(
              MountableFile.forClasspathResource("/schema.sql"),
              "/docker-entrypoint-initdb.d/"
  ); 

如果您有更多文件,则将按词典顺序执行。

Postgres image has the option to init the database on start, here's the docs on DockerHub (look for the /docker-entrypoint-initdb.d).

In short, *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d are executed on startup.

In the Java version you can use the withCopyFileToContainer method, I think something similar should be in Scala right?

So something like:

new PostgreSQLContainer<>("postgres:14-alpine")
  .withCopyFileToContainer(
              MountableFile.forClasspathResource("/schema.sql"),
              "/docker-entrypoint-initdb.d/"
  ); 

And if you have more files, they will be executed in lexicographic order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文