Additional documentation

Books

When you use a relational database, you need a way to track and organize your database schema evolutions. Typically there are several situation where you need a more sophisticated way to track your database schema changes:

When you work within a team of developers, each person needs to know about any schema change.

When you deploy on a production server, you need to have a robust way to upgrade your database schema.

If you work on several machines, you need to keep all database schemas synchronized.

Play tracks your database evolutions using several evolutions script. These scripts are written in plain old SQL and should be located in the conf/evolutions/{database name} directory of your application. If the evolutions apply to your default database, this path is conf/evolutions/default.

The first script is named 1.sql, the second script 2.sql, and so on…

Each script contains two parts:

The Ups part the describe the required transformations.

The Downs part that describe how to revert them.

For example, take a look at this first evolution script that bootstrap a basic application:

As you see you have to delimitate the both Ups and Downs section by using comments in your SQL script.

Evolutions are automatically activated if a database is configured in application.conf and evolution scripts are present. You can disable them by setting evolutionplugin=disabled. For example when tests set up their own database you can disable evolutions for the test environment.

When evolutions are activated, Play will check your database schema state before each request in DEV mode, or before starting the application in PROD mode. In DEV mode, if your database schema is not up to date, an error page will suggest that you synchronise your database schema by running the appropriate SQL script.

If you agree with the SQL script, you can apply it directly by clicking on the ‘Apply evolutions’ button.

Developer B finishes his feature and commits (let’s say they are using Git). Now developer A has to merge the his colleague’s work before continuing, so he runs git pull, and the merge has a conflict, like:

Sometimes you will make a mistake in your evolution scripts, and they will fail. In this case, Play will mark your database schema as being in an inconsistent state and will ask you to manually resolve the problem before continuing.