简体   繁体   English

玩! 框架 - Evolutions的数据库问题

[英]Play! framework - database issue with Evolutions

I'm using Play! 我正在玩Play! framework 2.0 and I'm stuck on an annoying issue involving the database. 框架2.0,我陷入了涉及数据库的恼人问题。

Suppose I have a User (extends Model ) class which has few attributes ( first_name , last_name , email , password etc). 假设我有一个User (extends Model )类,它具有很少的属性( first_namelast_nameemailpassword等)。

At some point I want to add a new attribute, lets say last_ip (it doesn't really matter what it is). 在某些时候我想添加一个新属性,让我们说last_ip (它并不重要)。 So, I add the attribute to the User class, compile and run. 所以,我将属性添加到User类,编译并运行。

The thing is: I get this red alert about database changes (obviously) which asks me to press "APPLY CHANGES" (if I remember correctly). 问题是:我得到关于数据库更改的红色警报(显然),这要求我按“应用更改 (如果我没记错的话)。 That's fine BUT! 那很好但是! all the database records are erased ! 所有数据库记录都被删除了

In conclusion: I want to a new field but I don't want to lose all the records I already added to the database. 总结:我想要一个新的领域,但我不想丢失我已经添加到数据库的所有记录。 Is this possible? 这可能吗?

First you need to disable automatic generation of Evolution files by deleting the first 2 commented lines of the conf/evolutions/default/1.sql : 首先,您需要通过删除conf/evolutions/default/1.sql的前2个注释行来禁用Evolution文件的自动生成:

# --- Created by Ebean DDL
# To stop Ebean DDL generation, remove this comment and start using Evolutions

# --- !Ups
...

Then, you need to create a second file, called conf/evolutions/default/2.sql containing your update on the database schema with an Ups and a Downs section: 然后,您需要使用Ups和Downs部分创建第二个文件,名为conf/evolutions/default/2.sql其中包含数据库模式的更新:

# --- !Ups
ALTER TABLE USER ADD COLUMN last_ip varchar(30) DEFAULT NULL;

# --- !Downs

ALTER TABLE USER DELETE COLUMN last_ip;

What you are probably doing is applying destructive evolutions. 你可能正在做的是应用破坏性的演变。 If you look in 1.sql (or whatever your evolutions file is), under DOWNS you have statemtnts like "DROP DATABASE X". 如果你查看1.sql(或你的evolutions文件),在DOWNS下你有像“DROP DATABASE X”这样的statemtnts。 Whenever Play detects changes in the evolution file, it runs all the down evolutions, then reapplies the up evolutions, resulting in all your data being lost. 每当Play检测到evolution文件中的更改时,它都会运行所有向下演变,然后重新应用向上演变,从而导致所有数据丢失。

Here is more info: http://www.playframework.org/documentation/2.0.2/Evolutions 以下是更多信息: http//www.playframework.org/documentation/2.0.2/Evolutions

I suggest you take a look at Liquibase . 我建议你看看Liquibase Liquibase handles database changes, is super flexible and is database independent. Liquibase处理数据库更改,超级灵活且与数据库无关。 I use it in my applications to make sure when I apply a database change things don't get deleted or whatever. 我在我的应用程序中使用它来确保在应用数据库更改时不会删除任何内容。

您可以通过在application.conf中设置evolutionplugin = disabled来禁用Evolutions

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

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