Practice: Isolated Schema Changes
Barrier: Combined Schema and Codes Changes
Do schema changes and code changes happen at the same time?
- Versioned, Roll-Backable, etc
- Long running schema changes don't impact runtime
- Minimum Two Phase Migrations: First DB then Code
Setup Database
project/Build.scala
"postgresql" % "postgresql" % "9.1-901-1.jdbc4"
conf/application.conf
db.default.driver=org.postgresql.Driver
db.default.url=${?DATABASE_URL}
ebean.default="models.*"
applyEvolutions.default=true
Create local Postgres Database
$ sudo -u postgres createuser -P f
$ createdb -U f -W -h localhost f
$ export DATABASE_URL=postgres://f:f@localhost/f
Create Schema
conf/evolutions/default/1.sql
# --- !Ups
create table bar (
id varchar(255) not null,
name varchar(255),
constraint pk_bar primary key (id));
create sequence bar_seq;
# --- !Downs
drop table if exists bar cascade;
drop sequence if exists bar_seq;
Apply Schema Changes
Apply Schema Changes Locally and Test
$ psql -U f -W -h localhost f
Apply Schema Changes on Heroku and Test
$ git add conf/evolutions/default/1.sql
$ git commit -am "add schema v1"
$ git push prod master
$ heroku pg:psql
Use the New Schema
app/models/Bar.java
package models;
import play.db.ebean.Model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Bar extends Model {
@Id public String id;
public String name;
}
Test then Deploy
$ git commit -am "add Bar entity"
$ git push prod master
Add a Column
Update the Schema (conf/evolutions/default/2.sql)
# --- !Ups
alter table bar add column rating integer;
# --- !Downs
alter table bar delete column rating;
Test then Deploy
Update the Code (app/models/Bar.java)
public Integer rating;
Test then Deploy