【问题标题】:H2 and Postgres array compatibilityH2 和 Postgres 数组兼容性
【发布时间】:2014-07-19 20:37:12
【问题描述】:

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

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

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

【问题讨论】:

  • 在 postgres 中,您可以使用这些语法 f.ex。 text[]text[3]text ARRAYtext ARRAY[3]。但在 H2 中,我只能在他们的文档中看到 ARRAY 关键字h2database.com/html/datatypes.html#array_type
  • 啊,好吧,所以我可以使用 ARRAY 进行定义,但无论它是创建为 ARRAY 还是 [] thx,都可以使用相同的 Java 代码进行访问。
  • mmh 没有。在 H2 var ARRAY 中,在 Postgres var TEXT ARRAY 中,而不是单独的 ARRAY。好像不兼容

标签: java postgresql h2


【解决方案1】:

事实上,您可以使用真正的 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>

这个解决方案帮助我早先解决同样的问题。 希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2019-06-10
    • 2023-03-21
    • 2013-12-11
    • 2014-07-28
    • 2018-03-03
    • 1970-01-01
    • 2019-11-04
    • 2015-02-14
    • 2011-09-01
    相关资源
    最近更新 更多