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?
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.