Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package com.dimafeng.testcontainers

import org.testcontainers.containers.{PostgreSQLContainer => OTCPostgreSQLContainer}

class PostgreSQLContainer(dockerImageNameOverride: Option[String] = None) extends SingleContainer[OTCPostgreSQLContainer[_]] {
class PostgreSQLContainer(dockerImageNameOverride: Option[String] = None,
databaseName: Option[String] = None,
pgUsername: Option[String] = None,
pgPassword: Option[String] = None,
mountPostgresDataToTmpfs: Boolean = false) extends SingleContainer[OTCPostgreSQLContainer[_]] {

type OTCContainer = OTCPostgreSQLContainer[T] forSome {type T <: OTCPostgreSQLContainer[T]}

Expand All @@ -15,6 +19,19 @@ class PostgreSQLContainer(dockerImageNameOverride: Option[String] = None) extend
new OTCPostgreSQLContainer()
}

databaseName.map(container.withDatabaseName)
pgUsername.map(container.withUsername)
pgPassword.map(container.withPassword)

// as suggested in https://github.com/testcontainers/testcontainers-java/issues/1256
// mounting the postgres data directory to an in-memory docker volume (https://docs.docker.com/storage/tmpfs/)
// can improve performance
if (mountPostgresDataToTmpfs){
val tmpfsMount = new java.util.HashMap[String, String]()
tmpfsMount.put("/var/lib/postgresql/data", "rw")
container.withTmpFs(tmpfsMount)
}

def driverClassName: String = container.getDriverClassName

def jdbcUrl: String = container.getJdbcUrl
Expand All @@ -27,5 +44,17 @@ class PostgreSQLContainer(dockerImageNameOverride: Option[String] = None) extend
}

object PostgreSQLContainer {
def apply(dockerImageNameOverride: String = null): PostgreSQLContainer = new PostgreSQLContainer(Option(dockerImageNameOverride))
def apply(dockerImageNameOverride: String = null,
databaseName: String = null,
username: String = null,
password: String = null,
mountPostgresDataToTmpfs: Boolean = false
): PostgreSQLContainer =
new PostgreSQLContainer(
Option(dockerImageNameOverride),
Option(databaseName),
Option(username),
Option(password),
mountPostgresDataToTmpfs
)
}