In Play, can I apply different options depending on the type of database?

My Play application uses postgres. It includes some postgrace-specific evolutions that prevent me from using the h2 database in memory for testing. For example, the following evolution is fine in Postgres, but it crashes in h2 (even with MODE=PostgreSQL):

alter table ac_host rename column base_url to baseurl;

h2 equivalent:

alter table ac_host alter column base_url rename to baseurl;

I would like to use h2 in some of my tests, but trying to do this fails when initializing the application due to incompatible changes to h2. Is there any way around this, for example. by indicating alternative development options depending on the type of database?

+4
source share
2 answers

Testing purists and fans of Cake Pattern probably won't like this answer, but we run into the same problem you do and do the following:

We use the database defaultto run the application (when testing on production systems), but for automatic tests (using play test) we use a separate testdb configuration , which has its own evolutions, and runs on H2 instead of PostgreSQL.

On Play, you can check if the test mode is currently running, and you can modify the database accordingly:

lazy val default = Database.forDataSource {
    val defaultSource = current.configuration.getString("db.test.url").fold("default")(_ => "test")
    new play.api.db.DB.getDataSource(defaultSource)
}

Having separate evolutions for automated tests, there are other advantages: you can populate the database with some basic test data that may differ from other test systems.

, .

+3

: - git .

, Play , , . - , , , ( , compatibility mode.

: Mercedes-Benz , Diesel, , .

( webdevs) , , , , . .

+1

All Articles