H2和Postgres数组兼容性

Pau*_*lor 9 java postgresql h2

我可以获得h2来支持Postgres数组语法

CREATE TABLE artists
(
release_id integer,
artist_name text,
roles text[]
)
Run Code Online (Sandbox Code Playgroud)

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

yaz*_*ara 4

事实上,您可以使用真正的 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>
Run Code Online (Sandbox Code Playgroud)

然后需要为您的依赖项获取可用端口(例如 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>
Run Code Online (Sandbox Code Playgroud)

然后你应该运行并停止具有依赖服务(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>
Run Code Online (Sandbox Code Playgroud)

这个解决方案帮助我解决了之前同样的问题。我希望,它会对你有所帮助。