nbpeth
nbpeth

Reputation: 3148

H2 - Postgres mode support net data type

I have a DAO that is accessing a Postgres database. I want to use H2 for my unit tests - everything works great except that the table I am operating on has a column with inet datatype - so I get the exception

Unknown data type: "inet"; SQL statement: delete from table where id = ? and ip_address = ?::inet [50004-200]

Been a while since I've used H2 - I've set Postgres mode for the connection: url jdbc:h2:mem:test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE

is there anyway I can execute this query in H2 and support this data type?

Upvotes: 1

Views: 2798

Answers (2)

Mark Rotteveel
Mark Rotteveel

Reputation: 109171

Compatibility modes like provided by H2 are not perfect, they don't provide a 100% coverage of all features, nor 100% identical behaviour.

Instead of using a different database in your unit tests, and trying to make it as close as possible (and then getting bitten by the differences in production anyway), I'd recommend using the PostgreSQL module of testcontainers-java. This allows you to spin up PostgreSQL docker images for your unit tests.

Be aware, that this will impact performance, because it will likely be a bit slower than using H2 because of the time to launch the docker image. I'd recommend to carefully consider whether your tests truly needs to go to a database, or can get away with mocking data, or at least limiting the scope of tests that (need to) go to a database.

Upvotes: 3

Evgenij Ryazanov
Evgenij Ryazanov

Reputation: 8188

The biggest question here is how your table with unknown data type was created? Column ip_address should have some other data type known by H2.

Actually you can create a domain like this:

CREATE DOMAIN INET AS VARCHAR;

But usage of the same database system everywhere would be better.

Upvotes: 0

Related Questions