Domanda

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

alter table ac_host rename column base_url to baseurl;

The h2 equivalent is:

alter table ac_host alter column base_url rename to baseurl;

I'd like to use h2 in some of my tests, but attempting to do so fails on application initialisation because of the h2 incompatible evolutions. Is there a way around this, e.g. by specifying alternative evolutions depending on the database type?

È stato utile?

Soluzione

Testing purists and Cake Pattern fans will probably not like this answer, but we encountered the same problem as you have, and are doing the following:

We're using the default database for running the app (on test on production systems), but for automated tests (using play test) we use a separate test db configuration, which has its own evolutions, and runs on H2 instead of PostgreSQL.

In Play, you can check if you're currently running test mode and can switch 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 has other advantages too: you can populate the database with some basic test data which can be different from your other testing systems.

Hope that helps.

Altri suggerimenti

Shortly: only solution for you is i.e. git and several branches.

BTW, although Play supports many database engines it's rather not supposed, that the same product goes to life with such flexibility. You are showing us the reason yourself - databases differs a lot and writing one evolution applicable for different DBs most often just fails (many of engines even hasn't such thing like compatibility mode.

In real life it can be described as: Mercedes-Benz produces cars with petrol engines and also Diesel, I bought the last option, but want to use it with petrol.

From our (general webdevs) experience we found that working on different engines or even different versions of the same engine can bring tones of unexpected bugs, therefore the rule of the thumb is that all developers in the project uses the same version as on production. That works.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top