Skip to content

Commit

Permalink
Added support to specify username, password and databaseName.
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaprasadreddy committed May 16, 2023
1 parent d8bac12 commit 7e0630f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ by using [Testcontainers](https://www.testcontainers.org/) and applying Flyway d
MARIADB: mariadb:10.11
-->
<containerImage>postgres:15.2-alpine</containerImage>
<username>test</username> <!-- optional -->
<password>test</password> <!-- optional -->
<databaseName>test</databaseName> <!-- optional -->
</database>
<flyway>
<locations>
Expand Down
5 changes: 4 additions & 1 deletion examples/mariadb-flyway-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
<configuration>
<database>
<type>MARIADB</type>
<containerImage>mariadb:10.11</containerImage>
<containerImage>mariadb:10.11</containerImage> <!-- optional -->
<username>test</username> <!-- optional -->
<password>test</password> <!-- optional -->
<databaseName>test</databaseName> <!-- optional -->
</database>
<flyway>
<locations>
Expand Down
5 changes: 4 additions & 1 deletion examples/mysql-flyway-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
<configuration>
<database>
<type>MYSQL</type>
<containerImage>mysql:8.0.33</containerImage>
<containerImage>mysql:8.0.33</containerImage> <!-- optional -->
<username>test</username> <!-- optional -->
<password>test</password> <!-- optional -->
<databaseName>test</databaseName> <!-- optional -->
</database>
<flyway>
<locations>
Expand Down
5 changes: 4 additions & 1 deletion examples/postgres-flyway-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
<configuration>
<database>
<type>POSTGRES</type>
<containerImage>postgres:15-alpine</containerImage>
<containerImage>postgres:15-alpine</containerImage> <!-- optional -->
<username>test</username> <!-- optional -->
<password>test</password> <!-- optional -->
<databaseName>test</databaseName> <!-- optional -->
</database>
<flyway>
<locations>
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/testcontainers/jooq/codegen/DatabaseProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
public class DatabaseProps {
private DatabaseType type;
private String containerImage;
private String username;
private String password;
private String databaseName;

public DatabaseType getType() {
return type;
Expand All @@ -22,4 +25,28 @@ public String getContainerImage() {
public void setContainerImage(String containerImage) {
this.containerImage = containerImage;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getDatabaseName() {
return databaseName;
}

public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,51 @@ public class DatabaseProvider {
static JdbcDatabaseContainer<?> getDatabaseContainer(DatabaseProps props) {
DatabaseType dbType = props.getType();
String image = props.getContainerImage();
JdbcDatabaseContainer<?> container;
try {
switch (dbType) {
case POSTGRES:
if (image == null) {
if (isEmpty(image)) {
image = POSTGRES_IMAGE;
}
return new PostgreSQLContainer<>(image);
container = new PostgreSQLContainer<>(image);
break;
case MARIADB:
if (image == null) {
if (isEmpty(image)) {
image = MARIADB_IMAGE;
}
return new MariaDBContainer<>(image);
container = new MariaDBContainer<>(image);
break;
case MYSQL:
if (image == null) {
if (isEmpty(image)) {
image = MYSQL_IMAGE;
}
return new MySQLContainer<>(image);
container = new MySQLContainer<>(image);
break;
default:
throw new RuntimeException("Unsupported DatabaseType: " + dbType);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to instantiate database using Testcontainers", e);
}
if (isNotEmpty(props.getUsername())) {
container.withUsername(props.getUsername());
}
if (isNotEmpty(props.getPassword())) {
container.withPassword(props.getPassword());
}
if (isNotEmpty(props.getDatabaseName())) {
container.withDatabaseName(props.getDatabaseName());
}
return container;
}

private static boolean isNotEmpty(String str) {
return !isEmpty(str);
}

private static boolean isEmpty(String str) {
return str == null || str.trim().isEmpty();
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/testcontainers/jooq/codegen/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public void execute() throws MojoExecutionException {
String username;
String password;

// If jdbc details are provided, then connect to that existing database.
// Otherwise, spin up the database using Testcontainers
if (this.jdbc == null
|| this.jdbc.getUrl() == null
|| this.jdbc.getUsername() == null
Expand Down

0 comments on commit 7e0630f

Please sign in to comment.