Reputation: 8353
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?
Upvotes: 0
Views: 2140
Reputation: 11
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()
Upvotes: 1
Reputation: 3800
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.
Upvotes: 0