简体   繁体   English

如何在生产中处理Play Framework 2数据库演变

[英]How to handle Play Framework 2 database evolutions in production

It seems that whenever I change my models, Play Framework asks me to run a script that deletes my entire schema and recreates it. 似乎每当我更改模型时,Play Framework都会要求我运行一个删除整个模式并重新创建它的脚本。 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. 注意,我正在使用ebean和Postgres,并在heroku上托管。

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. 不幸的是, Ebean只能创建CREATE DDL (而不是UPDATE DDL )(正如他们的小组回答的那样),因此您需要尽快切换到手动演进

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 如果ebean插件只有1.sql evolution,那么它将重新创建整个DDL
  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. 您需要从1.sql删除两个第一个注释,并开始使用下一个数字2.sql3.sql等编写自己的进化。尝试在切换到手动进化之前放置尽可能多的模型/字段。 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. 手动演进应该包含现有表/列的ALTERS而不是DROP / CREATE,它们应该具有:每次更改的UpsDowns
  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. 事实上,有时用DB gui修改数据库结构会更容易,无论如何它主要适用于单个开发人员...当你需要与其他开发人员共享代码时,编写演进将是更好的选择。

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. 如果在一段时间后你将添加下一个“大”部分的新模型,你可以再次启用临时自动DDL并使用本地git来复制新部件。 Then revert to own revolution and paste new parts generated by Ebean plugin. 然后恢复自己的革命并粘贴Ebean插件生成的新部件。

Biesior basically summed it up quite well. Biesior基本上总结得很好。 However, as a beginner in Play, I find that a bit more clarification with a concrete example might be helpful. 但是,作为Play的初学者,我发现通过具体示例进行更多澄清可能会有所帮助。

First, the following example is for Java. 首先,以下示例适用于Java。

Suppose you added a new field 假设您添加了一个新字段

public String dum_str;

in your model Dum. 在你的模型Dum。 Then, you will need a 2.sql under conf/evolutions/ like this: 然后,你需要一个2.sqlconf/evolutions/像这样:

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

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

I hope this would be helpful. 我希望这会有所帮助。

Backup as a script the current db. 备份为当前db的脚本。 Make your changes in the Java model. 在Java模型中进行更改。 Let Play apply the changes and recreate the DB Then Restore your data. 让Play应用更改并重新创建数据库然后恢复数据。

Attn: Do not change existing fieldnames otherwise it won't work. Attn:不要改变现有的字段名,否则它将无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM