如何在 Jooq 代码生成中使用 Liquibase 上下文

How to use Liquibase contexts with Jooq code generation

我有一个 Springboot 项目,它使用 Liquibase 进行数据库迁移,使用 Jooq 进行数据库访问和相关代码生成。当 Jooq 内省应用了所有更改的数据库时,这工作正常,但现在我想转换到内存中的 H2 数据库以生成代码,以便 Jooq 不依赖于我的实际 (Postgres) 数据库。

但是,当使用 Jooq 生成源时,我现在收到一个错误,因为在我具有唯一约束的列上存在重复键异常。我注意到这是因为我正在使用 Liquibase 上下文以便在测试、开发和生产环境中插入不同的数据。 Jooq 似乎忽略了这些上下文并将所有更改应用于同一数据库,当我在测试和开发中插入相同的数据时,生成失败。那么我如何确保 Jooq 和 Liquibase 在生成源代码阶段就已经使用正确的上下文(和 maven 配置文件)?

我的设置中的一些摘录:

pom.xml

        <profile>
            <id>local</id>
            <properties>
                <activatedProperties>local</activatedProperties>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

...

            <plugin>
                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <version>${jooq.version}</version>

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

                <!-- Specify the plugin configuration.
                     The configuration format is the same as for the standalone code generator -->
                <configuration>
                <generator>
                    <database>
                        <name>org.jooq.meta.extensions.liquibase.LiquibaseDatabase</name>
                        <properties>
                            <property>
                                <key>sort</key>
                                <value>liquibase</value>
                            </property>
                            <property>
                                <key>scripts</key>
                                <value>src/main/resources/liquibase/changelog-master.xml</value>
                            </property>
                            <property>
                                <key>unqualifiedSchema</key>
                                <value>none</value>
                            </property>
                            <property>
                                <key>defaultNameCase</key>
                                <value>lower</value>
                            </property>
                        </properties>
                    </database>
                    <target>
                        <packageName>com.graphite.horses</packageName>
                        <directory>target/generated-sources/jooq</directory>
                    </target>
                    <generate>
                        <javaTimeTypes>true</javaTimeTypes>
                    </generate>
                </generator>
                </configuration>
            </plugin

Liquibase 变更文件:

    <changeSet id="addInitialCredentialsValuesLocal" author="daniel" context="local">
        <insert tableName="credentials">
            <column name="key" value="my-token"/>
            <column name="platform" value="web"/>
        </insert>
    </changeSet>

    <changeSet id="addInitialCredentialsValuesTest" author="daniel" context="test">
        <insert tableName="credentials">
            <column name="key" value="my-token"/>
            <column name="platform" value="web"/>
        </insert>
    </changeSet>

这就是它失败的地方,因为“我的令牌”再次插入到 Jooq 的内存数据库中,即使测试上下文不应该处于活动状态。

从 jOOQ 3.14.0 和 3.13.2 开始(参见 #9872),“contexts”参数可以像这样传递给 Liquibase Database 实例:

<!-- The property "changeLogParameters.contexts" will be passed on to the 
     liquibase.database.Database.update() call (jOOQ 3.13.2+).
     See https://www.liquibase.org/documentation/contexts.html -->
<property>
  <key>changeLogParameters.contexts</key>
  <value>!test</value>
</property>

另请参阅此处的示例配置: https://www.jooq.org/doc/latest/manual/code-generation/codegen-liquibase/