Evolustions is a Play Framework DB migration tool. You can easily update and share your DB schema among multiple developers.
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.
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
)
Click Refresh project in the upper right
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
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
);
Name the files 1.sql for the first script, 2.sql for the second script, and so on.
Run locally with $ activator run
and jump tohttp: // localhost: 9000 /
ʻApply this script now!` Pressing the button will execute the described Evolution Script
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.
Evolutions - 2.1.x The source code is here
Recommended Posts