Skip to content

Commit

Permalink
#24 Support custom Docker images for Postgres (i.e. Postgis) (#32)
Browse files Browse the repository at this point in the history
Fixes #24

---------

Co-authored-by: Eddú Meléndez <eddu.melendez@gmail.com>
  • Loading branch information
zzzLobster and eddumelendez committed Oct 6, 2023
1 parent f2f8bdb commit d74e2f0
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

/** DatabaseProvider provides container instance for a given DatabaseType */
public class DatabaseProvider {
Expand All @@ -15,9 +16,12 @@ public static JdbcDatabaseContainer<?> getDatabaseContainer(DatabaseProps props)
String image = Optional.ofNullable(props.getContainerImage()).orElse(dbType.getDefaultImage());
JdbcDatabaseContainer<?> container =
switch (dbType) {
case POSTGRES -> new PostgreSQLContainer<>(image);
case MARIADB -> new MariaDBContainer<>(image);
case MYSQL -> new MySQLContainer<>(image);
case POSTGRES -> new PostgreSQLContainer<>(
DockerImageName.parse(image).asCompatibleSubstituteFor("postgres"));
case MARIADB -> new MariaDBContainer<>(
DockerImageName.parse(image).asCompatibleSubstituteFor("mariadb"));
case MYSQL -> new MySQLContainer<>(
DockerImageName.parse(image).asCompatibleSubstituteFor("mysql"));
};
if (isNotEmpty(props.getUsername())) {
container.withUsername(props.getUsername());
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/testcontainers/jooq/codegen/PluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ public void testPostgresLiquibase() throws Exception {
.hasGeneratedJooqTable("public_", "Databasechangelog.java");
}

@Test
public void testPostgisFlyway() throws Exception {
// given
MavenProject mavenProject = getMavenProject("postgis-flyway");

// when
mojoRule.lookupConfiguredMojo(mavenProject, "generate").execute();

// then
assertThatProject(mavenProject)
.hasGeneratedJooqTable("Users.java")
.hasGeneratedJooqTable("FlywaySchemaHistory.java");
}

@Test
public void testMysqlFlyway() throws Exception {
// given
Expand Down
92 changes: 92 additions & 0 deletions src/test/resources/pom/postgis-flyway/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-jooq-codegen-maven-plugin-test</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<java.version>17</java.version>
<testcontainers.version>1.19.0</testcontainers.version>
<testcontainers-jooq-codegen-maven-plugin.version>0.0.4-SNAPSHOT</testcontainers-jooq-codegen-maven-plugin.version>
<jooq.version>3.18.3</jooq.version>
<postgresql.version>42.6.0</postgresql.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-jooq-codegen-maven-plugin</artifactId>
<version>${testcontainers-jooq-codegen-maven-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
</dependencies>
<goals>
<goal>generate</goal>
</goals>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<configuration>
<database>
<type>POSTGRES</type>
<containerImage>postgis/postgis:15-3.4-alpine</containerImage>
<username>test</username>
<password>test</password>
<databaseName>test</databaseName>
</database>
<flyway>
<locations>
filesystem:${project.basedir}/src/main/resources/db/migration/postgres
</locations>
</flyway>
<jooq>
<generator>
<database>
<includes>.*</includes>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.test</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</jooq>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
create sequence user_id_seq start with 1 increment by 5;

create table users
(
id bigint DEFAULT nextval('user_id_seq') not null,
email varchar not null,
password varchar not null,
name varchar not null,
role varchar not null,
verified bool not null default false,
verification_token varchar,
created_at timestamp,
updated_at timestamp,
primary key (id),
CONSTRAINT user_email_unique UNIQUE (email)
);

0 comments on commit d74e2f0

Please sign in to comment.