繁体   English   中英

H2 和 Postgres 阵列兼容性

[英]H2 and Postgres array compatibility

我可以让 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>

这个解决方案帮助我更早地解决了同样的问题。 我希望,它会帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM