【问题标题】:How to use Liquibase contexts with Jooq code generation如何在 Jooq 代码生成中使用 Liquibase 上下文
【发布时间】:2021-09-15 14:39:39
【问题描述】:

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

但是当使用 Jooq 生成源时,我现在收到一个错误,因为在我有唯一约束的列上出现重复键异常。我注意到这是因为我使用 Liquibase 上下文以便在测试、开发和生产环境中插入不同的数据。 Jooq 似乎忽略了这些上下文,并将所有更改应用于同一个数据库,当我在 test 和 dev 中插入相同的数据时,生成失败。那么如何确保 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>

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

【问题讨论】:

    标签: spring-boot liquibase jooq


    【解决方案1】:

    从 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/

    【讨论】:

      猜你喜欢
      • 2016-02-19
      • 2012-03-22
      • 2016-06-12
      • 2017-12-08
      • 2016-07-22
      • 2020-07-26
      • 2014-09-09
      • 2017-12-03
      • 2017-08-13
      相关资源
      最近更新 更多