-1

I have spring boot application with pgsql as db. I am writing test cases for the api's and for the test cases i am using h2 db. I have multiple entities where i have multiple enums. For the test cases we have

spring.jpa.hibernate.ddl-auto = create-drop

When hibernate is creating the tables from entity it is giving Unknown data type: "enum_type1".

I took a reference from this question: How to fake ENUM columns in the H2 database for play unit testing?

So i updated my property as follows:

spring.datasource.url= jdbc:h2:mem:test;MODE=PostgreSQL;INIT=CREATE DOMAIN IF NOT EXISTS enum_type1 as VARCHAR(255),CREATE DOMAIN IF NOT EXISTS enum_type2 as VARCHAR(255);DB_CLOSE_ON_EXIT=FALSE

But it is giving following error:

org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "CREATE DOMAIN IF NOT EXISTS enum_type1 AS VARCHAR(255),[*]CREATE DOMAIN IF NOT EXISTS enum_type2 AS VARCHAR(255)"; SQL statement:

So how can we create multiple enum/domain before hibernate is scanning the entities? Any help will be appreciated, Thanks.

jagga
  • 163
  • 3
  • 5
  • 18
  • 1
    https://phauer.com/2017/dont-use-in-memory-databases-tests-h2/ –  Jan 13 '21 at 06:57
  • @a_horse_with_no_name If we use h2 DB. Test cases will take less time for execution , correct me if i am wrong. – jagga Jan 13 '21 at 07:31

1 Answers1

0

You can't use a comma as a separator between statements. If you want to specify multiple statements in INIT parameter, they should be separated with \;. Note that INIT=something parameter should be separated from other parameters, such as DB_CLOSE_ON_EXIT with ; (without the \).

jdbc:h2:mem:test;MODE=PostgreSQL;INIT=CREATE DOMAIN IF NOT EXISTS enum_type1 as VARCHAR(255)\;CREATE DOMAIN IF NOT EXISTS enum_type2 as VARCHAR(255);DB_CLOSE_ON_EXIT=FALSE

H2 also has a built-in ENUM data type, it should be better to use it instead of VARCHAR.

PostgreSQL compatibility mode should be normally used with DATABASE_TO_LOWER=TRUE.

And the whole idea to use different DBMS for tests and production doesn't look good. Normally you should use multiple DBMS only when your application is initially designed to work with them all.

Evgenij Ryazanov
  • 6,960
  • 2
  • 10
  • 18