#Postgres for production, H2 to test, rand() function.

20 messages · Page 1 of 1 (latest)

buoyant sequoia
#

I was using h2 for the whole application, but now I want to change the production to postgress. However some of my queries have the rand() function. How can I change it to work in both, pg and h2?

surreal karmaBOT
#

This post has been reserved for your question.

Hey @buoyant sequoia! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

violet swift
#

I think you need to use random() in postgres

buoyant sequoia
#

yeah, but then i'll get an error in the h2 tests

violet swift
#

use random() if you are in prod and rand() if you are in tests?

buoyant sequoia
#

Can I do that?

violet swift
#

or maybe there's a compatibility mode with H2

#

use an if statement

#

chexk if you are in tests?

buoyant sequoia
#

I couldn't do that, because the query is an annotation:

@Query("SELECT um FROM User u " +
            "JOIN u.favoriteMovies um " + 
               "WHERE u.id = :userId AND um.id NOT IN :movieIds " +          "ORDER BY RAND() LIMIT 1")
    Optional<Movie> findRandomMovieByUserIdAndNotInMovieIds(@Param("userId") Long userId,
            @Param("movieIds") List<Long> movieId);
violet swift
#

or maybe add ;MODE=PostgreSQL to the H2 JDBC URL

#

Are you using JPA?

buoyant sequoia
#

Yes

#

Hm, I didn't know this option

#

Ok, how exactly can I use that?
spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL
This way?

violet swift
#

Do you have a different hibernate diakect configured in test and production?

buoyant sequoia
#

Yes, but I can change if it is necessary

surreal karmaBOT
#

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

buoyant sequoia
#

Ok, I think it worked, let me do some more tests