Skip to content
Advertisement

Jooq with flyway and testconteiners creates new container per run

I want to use flyway with testcontainers for jooq generation. For this purpose I have 2 plugins

<plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>7.14.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>migrate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <url>jdbc:tc:postgresql:10://localhost:7432/test</url>
                <user>test</user>
                <password>test</password>
                <driver>
                    org.testcontainers.jdbc.ContainerDatabaseDriver
                </driver>
                <locations>
                    <location>filesystem:src/main/resources/db/migrations</location>
                </locations>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen-maven</artifactId>
            <version>3.14.15</version>

            <executions>
                <execution>
                    <id>java-generator</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <jdbc>
                    <driver>org.testcontainers.jdbc.ContainerDatabaseDriver</driver>
                    <url>jdbc:tc:postgresql:10://localhost:7432/test</url>
                    <user>test</user>
                    <password>test</password>
                </jdbc>

                <generator>
                    <database>
                        <name>org.jooq.meta.postgres.PostgresDatabase</name>
                        <includes>.*</includes>
                        <inputSchema>public</inputSchema>
                    </database>

                    <generate/>

                    <target>
                        <packageName>com.test</packageName>
                        <directory>target/generated-sources/jooq</directory>
                    </target>
                </generator>
            </configuration>
        </plugin>

So, I see that flyway started tc, applied all scripts and then jooq starts its’s own container and tried to generate entities, but there are nothing. Could you please suggest how to handle this?

Advertisement

Answer

You have to start the test container first like this:

        <!-- Start Testcontainer -->
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>groovy-maven-plugin</artifactId>
            <version>2.1.1</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>
                            db = new org.testcontainers.containers.PostgreSQLContainer("postgres:12.7")
                                .withUsername("${db.username}")
                                .withDatabaseName("jtaf4")
                                .withPassword("${db.password}")
                            db.start()
                            project.properties.setProperty('db.url', db.getJdbcUrl())
                        </source>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.testcontainers</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>${testcontainers.version}</version>
                </dependency>
                <!-- Junit seems to be a transitive dependency of testcontainers? -->
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.13.1</version>
                </dependency>
            </dependencies>
        </plugin>
        <!-- Migrate schema -->
        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>7.14.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>migrate</goal>
                    </goals>
                    <configuration>
                        <url>${db.url}</url>
                        <user>${db.username}</user>
                        <password>${db.password}</password>
                        <locations>
                            <location>filesystem:src/main/resources/db/migration</location>
                        </locations>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Generate jOOQ code -->
        <plugin>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen-maven</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jdbc>
                    <driver>${db.driver}</driver>
                    <url>${db.url}</url>
                    <user>${db.username}</user>
                    <password>${db.password}</password>
                </jdbc>
                <generator>
                    <database>
                        <inputSchema>public</inputSchema>
                    </database>
                    <target>
                        <packageName>ch.jtaf.db</packageName>
                    </target>
                </generator>
            </configuration>
        </plugin>
Advertisement