gpt4 book ai didi

java - H2 和 Postgres 阵列兼容性

转载 作者:行者123 更新时间:2023-12-03 10:03:52 24 4
gpt4 key购买 nike

我可以让 h2 支持 Postgres 数组语法吗

CREATE TABLE artists
(
release_id integer,
artist_name text,
roles text[]
)

我在我的单元测试中使用 h2 来模拟 Postgres,但由于角色的定义,它不喜欢上面的 DDL(如果我注释掉该列它有效)。 H2 确实有一个 ARRAY 数据类型,我可以编写一种方法,以便我的代码可以与 h2 或 postgres 一起使用

最佳答案

事实上,您可以使用真正的 postgres DB 而不是 h2 来定义集成测试。
它会更有用。
主要思想是在集成测试之前运行具有依赖项(postgres DB)的 docker 实例,然后关闭。
下面是一个 maven 的例子:
首先定义规则:

                <plugin>
<!-- define Integration tests -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemPropertiesFile>${ports.env.file}</systemPropertiesFile>
<includes>
<include>**/*IT.*</include>
</includes>
<additionalClasspathElements>
<additionalClasspathElement>resources</additionalClasspathElement>
</additionalClasspathElements>
<systemPropertiesFile>${it.ports.env.file}</systemPropertiesFile>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
然后需要为您的依赖项获取免费端口(例如 postgres DB)
                <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<portNames>
<portName>DB_PORT</portName>
</portNames>
<outputFile>${it.ports.env.file}</outputFile>
</configuration>
</execution>
</executions>
</plugin>
然后你应该使用依赖服务(postgres)运行和停止 docker 容器:
               <plugin>
<groupId>com.dkanejs.maven.plugins</groupId>
<artifactId>docker-compose-maven-plugin</artifactId>
<version>4.0.0</version>
<configuration>
<envFile>${it.ports.env.file}</envFile>
<envVars>
<COMPOSE_HTTP_TIMEOUT>120</COMPOSE_HTTP_TIMEOUT>
</envVars>
<services>
<service>db-postgres-test</service>
</services>
<composeFiles>
<composeFile>${session.executionRootDirectory}/docker-compose.db-only.yml
</composeFile>
</composeFiles>
<detachedMode>true</detachedMode>
</configuration>
<executions>
<execution>
<id>up</id>
<phase>pre-integration-test</phase>
<goals>
<goal>up</goal>
</goals>
</execution>
<execution>
<id>down</id>
<phase>post-integration-test</phase>
<goals>
<goal>down</goal>
</goals>
<configuration>
<removeVolumes>true</removeVolumes>
<removeOrphans>true</removeOrphans>
</configuration>
</execution>
</executions>
</plugin>
这个解决方案帮助我更早地解决了同样的问题。
我希望,它会帮助你。

关于java - H2 和 Postgres 阵列兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23948593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com