使用Flyway中的嵌入式数据库和Maven中的jOOQ进行持续集成

时间:2015-02-09 04:17:55

标签: maven hsqldb travis-ci flyway jooq

所以我真的想用SQL that will break at compile time using flyway and jOOQ做正确的事情。为此,我需要一个可以在continuous integration server上运行的数据库解决方案,无法访问任何基于服务器的数据库。最终,我想将其部署到亚马逊,所以我需要一个mostly compatible with postgreSQL的解决方案。 HSQLDB's file protocol似乎符合该法案。

<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
                  http://maven.apache.org/xsd/maven-4.0.0.xsd">

<properties>
    <schema></schema>
    <db.groupId>org.hsqldb</db.groupId>
    <db.artifactId>hsqldb</db.artifactId>
    <db.version>2.3.2</db.version>
    <flyway.url>jdbc:hsqldb:file:myDB/db</flyway.url>
    <flyway.driver>org.hsqldb.jdbcDriver</flyway.driver>
    <jooq.generator.database.name>org.jooq.util.hsqldb.HSQLDBDatabase</jooq.generator.database.name>
    <flyway.user></flyway.user>
    <flyway.password></flyway.password>

</properties>

<dependencies>
    <dependency>
        <groupId>${db.groupId}</groupId>
        <artifactId>${db.artifactId}</artifactId>
        <version>${db.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq</artifactId>
        <version>3.4.2</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>3.1</version>

            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>migrate</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <locations>
                    <location>filesystem:src/main/resources/db/migration</location>
                </locations>
            </configuration>
        </plugin>
        <plugin>

            <!-- Specify the maven code generator plugin -->
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen-maven</artifactId>
            <version>3.4.2</version>

            <!-- The plugin should hook into the generate goal -->
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>

            <!-- Work around Maven's classloader -->
            <dependencies>
                <dependency>
                    <groupId>${db.groupId}</groupId>
                    <artifactId>${db.artifactId}</artifactId>
                    <version>${db.version}</version>
                </dependency>
            </dependencies>

            <configuration>

                <!-- JDBC connection parameters -->
                <jdbc>
                    <driver>${flyway.driver}</driver>
                    <url>${flyway.url}</url>
                    <user>${flyway.user}</user>
                    <password>${flyway.password}</password>
                </jdbc>

                <!-- Generator parameters -->
                <generator>
                    <name>org.jooq.util.DefaultGenerator</name>
                    <database>
                        <name>${jooq.generator.database.name}</name>
                        <includes>.*</includes>
                        <excludes></excludes>
                        <inputSchema>${schema}</inputSchema>
                    </database>
                    <target>
                        <packageName>package.goes.here</packageName>
                        <directory>target/generated-sources/jooq</directory>
                    </target>
                </generator>
            </configuration>
        </plugin>
    </plugins>
</build>

问题是Flyway可以很好地创建数据库,但是当它变为jOOQ轮到使用数据库来生成代码时,它会失败:

Caused by: org.hsqldb.HsqlException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile@1096ec89[file =...db/db.lck, exists=true, locked=false, valid=false, ] method: checkHeartbeat read: 2015-02-09 03:56:15 heartbeat - read: -863 ms.
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.persist.LockFile.newLockFileLock(Unknown Source)
at org.hsqldb.persist.Logger.acquireLock(Unknown Source)
at org.hsqldb.persist.Logger.open(Unknown Source)
at org.hsqldb.Database.reopen(Unknown Source)
at org.hsqldb.Database.open(Unknown Source)
at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
at org.hsqldb.DatabaseManager.newSession(Unknown Source)

问题的核心是Maven's plugins do not share the same classloader as the rest of the POM,您必须再次指定JDBC驱动程序依赖项。因此,我得到了一个新的驱动程序实例,它与已经打开的驱动程序Flyway已经打开,而不是使用相同的驱动程序。

因此,解决方案可能存在于许多不同的路径中:

  1. Flyway可以正确关闭数据库。显然,Flyway在其插件中有一个解决方法来读取项目的类路径。
  2. 可以以某种方式配置jOOQ以读取项目的类路径。
  3. 可能有另一个数据库可以更好地工作。
  4. 此致!

1 个答案:

答案 0 :(得分:2)

感谢Thilo的指导。用exec替换Flyway插件有效,但我必须create a simpler command line client才能使它工作。