How to handle Play Framework 2 database evolutions in production
Asked Answered
A

2

16

It seems that whenever I change my models, Play Framework asks me to run a script that deletes my entire schema and recreates it. Obviously this won't work for production, so what is the proper way to handle this in production?

Note, I'm using ebean and Postgres, and hosting on heroku.

Amalamalbena answered 30/1, 2013 at 23:45 Comment(0)
P
31

Unfortunately Ebean can create only CREATE DDL (and not UPDATE DDL) (as answered on their group), therefore you need to switch to manual evolutions ASAP.

some rules:

  1. Always backup your live DB before implementing any changes :)
  2. ebean plugin recreates whole DDL if it has only 1.sql evolution created by it
  3. You need to remove two first comments from 1.sql and start to writing own evolutions with next numbers 2.sql, 3.sql etc. Try to place as many models/fields as possible before switching to manual evolutions. The biggest part will be done automatically by plugin.
  4. manual evolutions should contain ALTERS to existing tables/columns instead of DROP/CREATE, they should have both: Ups and Downs for each change.
  5. try to place as many changes in each evolution as possible, it's easier to manage then writing separate evolution for each small change.

De facto sometimes it's just easier to modify DB structure with DB gui, anyway it works mainly for the single developer... when you need to share your code with other developers writing evolutions will be better option.

If after some time you'll add next 'big' portion of new models you can enable temporary auto DDL again and using local git just to copy new parts. Then revert to own revolution and paste new parts generated by Ebean plugin.

Pay answered 31/1, 2013 at 0:31 Comment(0)
G
12

Biesior basically summed it up quite well. However, as a beginner in Play, I find that a bit more clarification with a concrete example might be helpful.

First, the following example is for Java.

Suppose you added a new field

public String dum_str;

in your model Dum. Then, you will need a 2.sql under conf/evolutions/ like this:

# --- !Ups
ALTER TABLE dum ADD COLUMN dum_str VARCHAR(255);

# --- !Downs
ALTER TABLE dum DROP dum_str;

I hope this would be helpful.

Gem answered 13/5, 2013 at 23:40 Comment(1)
Good answer, for the further readers, it is ALTER TABLE dum DROP COLUMN dum_str; (please note the COLUMN).Legibility

© 2022 - 2024 — McMap. All rights reserved.