Use mysql in dev / prod and H2 in test

Using playback platform 2.1, I am trying to find the best way to have two different database configurations:

  • One to run my mysql based application
  • One for testing my H2 based application

While it is very easy to do one or the other, I encounter the following problems when I try to do one or the other:

  • I cannot have the same database changes because there are some mysql-specific commands that do not work with H2 even in mysql mode: this means two sets of evolutions and two separate database names
  • I am not sure how to override the main application.conf file with another one intended for testing in test mode. What I tried (passing the file name or overriding keys from the command line) seems to be reserved for prod mode.

My question is: can anyone recommend a good way to do both (mysql all the time and only H2 in the test) without complicating the launch of the application too much? Google did not help me.

Thank you for your help.

+5
source share
3 answers

There are some tricks that may come in handy.

Firstly, the MySQL notation /*! */allows you to add code that will obey MySQL, but other DBs ignore it, for example:

create table Users (
  id bigint not null auto_increment,
  name varchar(40)
) /*! engine=InnoDB */

, MySQL H2. MySQL-ism, , , MySQL, , , - MySQL PostgreSQL, /*! */, PostgreSQL H2, .

dev prod, , , prod. , , , dev- play run prod- play stage; target/start. target/start -Dconfig.resource. , prod.conf prod, :

include "application.conf"

# Extra config for prod - this will override the dev values in application.conf
db.default.driver=...
db.default.url=...
...

start_prod script, :

#!/bin/sh

# Optional - you might want to do this as part of the build/deploy process instead
#play stage
target/start -Dconfig.resource=prod.conf

, application.conf prod conf dev.conf, , , , script prod (, , JVM/memory/GC rc.d - ).

+5

, , , : , .. , , . , .

, , , ... id application.conf - ...)

+2

, :

public class ApplicationTest extends WithApplication {
    @Before
    public void setup() {
        start(fakeApplication(inMemoryDatabase("default-test"), fakeGlobal()));
    }

    /// skipped ....
}

inMemoryDatabase() H2.

+1

All Articles