java 如何使用 Hibernate 在 Spring Boot 中处理数据库迁移?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44352870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle database migrations in Spring Boot with Hibernate?
提问by Ville Miekk-oja
My database background comes from the Django framework (python). In Django, getting started with database migrations was easy: Django migrations. The Django framework provided tool for creating the migrations based on your models, and also a tool to apply the migrations on your database. I think this way of doing worked in both development and in production. You did not have to write migrations by yourself, the framework created them for you.
我的数据库背景来自 Django 框架(python)。在 Django 中,开始使用数据库迁移很容易:Django 迁移。Django 框架提供了基于模型创建迁移的工具,以及在数据库上应用迁移的工具。我认为这种做法在开发和生产中都有效。您不必自己编写迁移,框架为您创建了它们。
Now I have started a project with Spring Boot and Hibernate. I configure my application to use hibernate with JPA. With these settings, I now would need to know how does my framework handle database migrations? I mean if I change a column, either it's type, or even might remove it, then how do I migrate the database to the change? I know that spring boot will automatically detect column changes on startup, and create columns that do not exist based on the models (Entity's). I guess it has something to do with variable
现在我已经开始了一个使用 Spring Boot 和 Hibernate 的项目。我将我的应用程序配置为将 Hibernate 与 JPA 结合使用。有了这些设置,我现在需要知道我的框架如何处理数据库迁移?我的意思是,如果我更改一列,无论是它的类型,还是可能将其删除,那么我如何将数据库迁移到更改中?我知道 spring boot 会在启动时自动检测列更改,并根据模型(实体)创建不存在的列。我猜它与变量有关
spring.jpa.hibernate.ddl-auto
spring.jpa.hibernate.ddl-auto
But how does it handle the existing database objects? Does it add the column to them too, and with what value? The default value I set? What if I change the column type? Can it then handle the change? These settings and spring-boot automated database management probably are not enough in the long run?
但是它如何处理现有的数据库对象呢?它是否也将列添加到它们中,并且具有什么价值?我设置的默认值?如果我更改列类型会怎样?然后它可以处理变化吗?从长远来看,这些设置和 spring-boot 自动化数据库管理可能还不够吗?
What I want to know is, that what are the best practices on how to handle database migrations with Spring Boot and hibernate combination?I believe there is a standard how most of the people with this combination handle the migrations? I hope it is as easy as with Django... I know about flyway, but don't know if I really need it, or if it is used much with this combination of mine (including spring boot and hibernate).
我想知道的是,关于如何使用 Spring Boot 和 hibernate 组合处理数据库迁移的最佳实践是什么?我相信有一个标准,大多数拥有这种组合的人如何处理迁移?我希望它和 Django 一样简单......我知道 flyway,但不知道我是否真的需要它,或者它是否与我的这个组合(包括 spring boot 和 hibernate)一起使用很多。
采纳答案by Darren Forsythe
Liquibase or Flyway are the two main options for versioning/handling database migrations. ddl-auto
is quick and dirty, but it does not, nor can it, take into account everything that needs to be handled. THere's also the chance of race-conditions (two instances trying to update the DDL at the same time).
Liquibase 或 Flyway 是版本控制/处理数据库迁移的两个主要选项。ddl-auto
又快又脏,但它没有,也不能,考虑到所有需要处理的事情。还有可能出现竞争条件(两个实例同时尝试更新 DDL)。
This answer goes into more detail about ddl-auto
in a production environment, and why you shouldn't.
这个答案更详细地介绍了ddl-auto
在生产环境中的情况,以及为什么不应该这样做。
Hibernate: hbm2ddl.auto=update in production?
https://www.credera.com/blog/technology-insights/java/liquibase-fed-inconsistent-schemas/has bit more info on the why/concepts.
https://www.credera.com/blog/technology-insights/java/liquibase-fed-inconsistent-schemas/有更多关于原因/概念的信息。