[JAVA] A story about introducing Evolutions into the Play Framework

Operating environment

What is Evolutions?

Evolustions is a Play Framework DB migration tool. You can easily update and share your DB schema among multiple developers.

Introduction background

In local development, multiple developers used their respective DBs to add columns and tables. I used the same source code, but the DB schema was different, so I had to update it manually often. Matching the DB schema across multiple developers is easy with Evolutions.

How to use Evolutions

Evolutions setup

Added ʻevolutions` to the list of dependent libraries in build.sbt

build.sbt


name := """play-evolutions-sample"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  "mysql" % "mysql-connector-java" % "5.1.38",
  javaJdbc,
  cache,
  javaWs,
  evolutions
)

https://gyazo.com/c2fb3c3cd5e366c8b9e0e604334dfc26 Click Refresh project in the upper right

Check the settings in application.conf

It is commented out by default, but make sure it is not false just in case

application.conf


play.evolutions {
  # You can disable evolutions for a specific datasource if necessary
  #db.default.enabled = false
}

Or explicitly write db.default.enabled = true

Description of Evolution Script

Create a directory $ mkdir -p conf/evolutions/default Create and describe a SQL file that modifies the database under conf / evolutions / default

It consists of two parts, Ups and Downs, and the contents to be described are as follows.

# --- !Ups

Description of the required schema conversion method(SQL statement)


# --- !Downs 

Description of how to undo the above conversion(SQL statement)

If you want to add a new dummy table

1.sql


# --- !Ups
create table dummy(
    id integer not null,
    name varchar(255),
    created_at datetime not null,
    updated_at datetime not null
);

# --- !Downs 
drop table dummy

If you want to add a column to the dummy table

2.sql


# --- !Ups
ALTER TABLE dummy ADD(
    deleted_at datetime not null
);

# --- !Downs
ALTER TABLE dummy DROP(
    deleted_at
);

About file name

Name the files 1.sql for the first script, 2.sql for the second script, and so on.

About execution

Run locally with $ activator run and jump tohttp: // localhost: 9000 / ʻApply this script now!` Pressing the button will execute the described Evolution Script https://gyazo.com/ad537b57cd684f733522cfe19addce03

About Downs

Downs are not run manually, they are run when the contents of the Ups change. Downs is executed before Ups is executed, and as a result, the schema of the database is updated.

References

Evolutions - 2.1.x The source code is here

Recommended Posts

A story about introducing Evolutions into the Play Framework
The story of introducing a very Rails-like serverless framework "Ruby on Jets" into the production environment
A story about the JDK in the Java 11 era
A story about making a Builder that inherits the Builder
A note about the scope
A story about hitting the League Of Legends API with JAVA
A murmur about the utility class
A story about having a hard time aligning a testing framework with Java 6
A story about making a calculator to calculate the shell mound rate
A story about using the CoreImage framework to erase stains with Swift and implement a blur erase function
A story stuck with log output in Docker + Play framework environment
A story about sending a pull request to MinGW to update the libgr version
Guess about the 2017 Java Persistence Framework (3) Reladomo
About the initial display of Spring Framework
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)
About the official start guide of Spring Framework
A story about Java 11 support for Web services
The story that docker had a hard time
The story of introducing Ajax communication to ruby
A story about trying to operate JAVA File
Introducing Rspec, a Ruby on Rails test framework
A note about the Rails and Vue process
A story about a new engineer reading a passion programmer
[Jackson] A story about converting the return value of BigDecimal type with a custom serializer.