DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Designing a New Framework for Ephemeral Resources
  • Front-End: Cache Strategies You Should Know
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • Implementing RBAC in Quarkus

Trending

  • Designing a New Framework for Ephemeral Resources
  • Front-End: Cache Strategies You Should Know
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • Implementing RBAC in Quarkus
  1. DZone
  2. Data Engineering
  3. Databases
  4. Database Initialization With Play

Database Initialization With Play

In this post, we take a look how to get your database up and running in your new Play framework-based application while also managing schema changes.

Emmanouil Gkatziouras user avatar by
Emmanouil Gkatziouras
CORE ·
Apr. 13, 17 · Tutorial
Like (5)
Save
Tweet
Share
7.10K Views

Join the DZone community and get the full member experience.

Join For Free

Once you start a Play prototype application, one of the priorities is to initialize your database and also manage database schema changes.

Play provides us with evolutions. By utilizing evolutions, we are able to create our database and manage any futures changes to the schema.

To get started, we need to add the JDBC dependency and the evolutions dependency.

libraryDependencies += evolutions
libraryDependencies += jdbc


Then we shall use a simple H2 database persisted on a disk as our Play application's default database. We'll edit the conf/application.conf file and add the following lines.

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:/tmp/defaultdatabase"


Pay extra attention that our database location is the tmp directory, thus all changes shall be deleted once we reboot our workstation.

Once we have configured our database, we are ready to create our first SQL statement. Our scripts should be located in the conf/evolutions/{your database name} directory, in our case
/conf/evolutions/default.

Our first script ‘1.sql’, shall create the users table.

# Users schema

# --- !Ups

CREATE TABLE users (
    id bigint(20) NOT NULL AUTO_INCREMENT,
    email varchar(255) NOT NULL,
    first_name varchar(255) NOT NULL,
    last_name varchar(255) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY (email)
);

# --- !Downs

DROP TABLE users;


As we can see, we have ups and downs. What do they stand for? As you might have guessed, ups describe the transformations while downs describe how to revert them. So the next question would be how this functionality comes into use.

Suppose you have two developers working on 2.sql. Locally, they have successfully migrated their database. Once they are done, however, the merge result is far different than the file they executed on their database.

Evolutions detect whether the file is different and reverts the old revision by applying downs and then applying the up-to-date revision.

Now we are all set to run our application.

sbt run


Once we navigate to localhost:9000, we shall be presented with a screen that forces us to run the evolutions detected.

Let us go one step further and see what has been done to our database schema. We can easily explore an H2 database using dbeaver or your IDE. Upon issuing the show tables command, the results contain one extra table.

>SHOW TABLES;

TABLE_NAME,TABLE_SCHEMA
PLAY_EVOLUTIONS,PUBLIC
USERS,PUBLIC


The PLAY_EVOLUTIONS table keeps track of our changes.

Id is the number of the evolution script that we created. The apply and revert fields are the ups and downs of the SQL statements we created previously.

The hash field is used in order to detect changes to our file. In case of an evolution that has a different hash from the one applied, the previous evolution is reverted and applies the new script.

For example, let’s enhance our previous script and add one more field: username.

# Users schema

# --- !Ups

CREATE TABLE users (
    id bigint(20) NOT NULL AUTO_INCREMENT,
    email varchar(255) NOT NULL,
    username varchar(255) NOT NULL,
    first_name varchar(255) NOT NULL,
    last_name varchar(255) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY (email)
);

# --- !Downs

DROP TABLE users;


Once we start our application, we will be presented with a screen that forces us to issue an evolution for our different revision. If we hit apply, the users table shall contain the username field.

So the process of a new revision is pretty straightforward. The hash from the new 1.sql file is extracted. Then, a query checks whether the 1.sql file has already been applied. If it has been applied, a check is issued in case the hashes are the same. If they are not, then the downs script from the current database entry is executed. Once that's finished, the new script is applied.

Database

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Designing a New Framework for Ephemeral Resources
  • Front-End: Cache Strategies You Should Know
  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • Implementing RBAC in Quarkus

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: